Skip to content

Commit

Permalink
Merge pull request #7 from upwork/v3.1.0
Browse files Browse the repository at this point in the history
v3.1.0
  • Loading branch information
mnovozhylov committed Jun 22, 2023
2 parents 00664b5 + 33d1265 commit b2b4bf3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python: [ '3.8', '3.9' ]
python: [ '3.8', '3.9', '3.10' ]

name: Python ${{ matrix.python }}
steps:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release History

## 3.1.0
* Add support for Client Credentials Grant

## 3.0.4
* Add GraphQL support

Expand Down
5 changes: 5 additions & 0 deletions example/myapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def get_desktop_client():
# token = {'access_token': 'xxxxxxxxxxxxxxxxxx', 'expires_at': 1590479276.547947, 'expires_in': '86400', 'refresh_token': 'xxxxxxxxxxxxxxxxxxxxxxxx', 'token_type': 'Bearer'}
# config = upwork.Config({'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'client_secret': 'xxxxxxxxxxxxx', 'token': token})

# For Client Credentials Grant the following config must be used, no other parameters are needed
# config = upwork.Config({'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'client_secret': 'xxxxxxxxxxxxx', 'grant_type': 'client_credentials'})

client = upwork.Client(config)

try:
config.token
except AttributeError:
# remove client.get_authorization_url and authz_code input in case Client Credentials Grant is used
authorization_url, state = client.get_authorization_url()
# cover "state" flow if needed
authz_code = input(
Expand All @@ -48,6 +52,7 @@ def get_desktop_client():

print("Retrieving access and refresh tokens.... ")
token = client.get_access_token(authz_code)
# token = client.get_access_token() # for Client Credentials Grant
# WARNING: the access token will be refreshed automatically for you
# in case it's expired, i.e. expires_at < time(). Make sure you replace the
# old token accordingly in your security storage. Call client.get_actual_config
Expand Down
8 changes: 4 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

nox.options.sessions = ["tests", "lint", "build"]

python = ["3.9"]
python = ["3.10"]


lint_dependencies = [
Expand Down Expand Up @@ -35,7 +35,7 @@ def cover(session):
session.run("coverage", "erase")


@nox.session(python="3.9")
@nox.session(python="3.10")
def lint(session):
session.install(*lint_dependencies)
files = ["tests"] + [str(p) for p in Path(".").glob("*.py")]
Expand All @@ -49,7 +49,7 @@ def lint(session):
session.run("check-manifest")


@nox.session(python="3.9")
@nox.session(python="3.10")
def build(session):
session.install("setuptools")
session.install("wheel")
Expand All @@ -58,7 +58,7 @@ def build(session):
session.run("python", "setup.py", "--quiet", "sdist", "bdist_wheel")


@nox.session(python="3.9")
@nox.session(python="3.10")
def publish(session):
build(session)
print("REMINDER: Has the changelog been updated?")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
packages=find_packages(),
setup_requires=[],
url="https://github.com/upwork/python-upwork-oauth2",
version="3.0.4",
version="3.1.0",
zip_safe=False,
)
2 changes: 1 addition & 1 deletion upwork/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

__author__ = """Maksym Novozhylov"""
__email__ = "mnovozhilov@upwork.com"
__version__ = "3.0.4"
__version__ = "3.1.0"

__all__ = ("Config", "Client", "routers")
17 changes: 12 additions & 5 deletions upwork/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License:: See LICENSE.txt and TOS - https://developers.upwork.com/api-tos.html

from . import upwork
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session # type: ignore
from urllib.parse import parse_qsl, urlencode

Expand Down Expand Up @@ -48,10 +49,16 @@ def __init__(self, config):
token_updater=self.refresh_config_from_access_token,
)
except AttributeError as e:
# start from authorization step
self.__oauth = OAuth2Session(
self.config.client_id, redirect_uri=self.config.redirect_uri
)
if self.config.grant_type == "client_credentials":
client = BackendApplicationClient(client_id=self.config.client_id)
self.__oauth = OAuth2Session(
client=client
)
else:
# start from authorization step
self.__oauth = OAuth2Session(
self.config.client_id, redirect_uri=self.config.redirect_uri
)

def get_authorization_url(self):
"""Get authorization URL
Expand All @@ -63,7 +70,7 @@ def get_authorization_url(self):
"{0}{1}".format(upwork.BASE_HOST, self.__uri_auth)
)

def get_access_token(self, authorization_response):
def get_access_token(self, authorization_response=None):
"""Finish auth process and get access token
:param authorization_response:
Expand Down
5 changes: 5 additions & 0 deletions upwork/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def __init__(self, config):
config["client_secret"],
)

if "grant_type" in config:
self.grant_type = config["grant_type"]
else:
self.grant_type = None # Authorization Code Grant flow is used by default

if "redirect_uri" in config:
self.redirect_uri = config["redirect_uri"]

Expand Down

0 comments on commit b2b4bf3

Please sign in to comment.