Skip to content

Commit b92b699

Browse files
committed
Merge branch 'release/0.0.4'.
2 parents 0434d80 + cd9c995 commit b92b699

File tree

7 files changed

+274
-50
lines changed

7 files changed

+274
-50
lines changed

NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ideas
2+
3+
- use something like [colander](https://docs.pylonsproject.org/projects/colander/en/latest/basics.html) to deserialize JSON into Python classes (and potentially validate it, too)
4+
- how about _async_?

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="autodesk-forge-sdk",
8-
version="0.0.3",
8+
version="0.0.4",
99
author="Petr Broz",
1010
author_email="[email protected]",
1111
description="Unofficial Autodesk Forge SDK for Python.",

src/autodesk_forge_sdk/auth.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AuthenticationClient(BaseClient):
4848
**Documentation**: https://forge.autodesk.com/en/docs/oauth/v2/reference/http
4949
"""
5050

51-
def __init__(self, base_url=BASE_URL):
51+
def __init__(self, base_url: str=BASE_URL):
5252
"""
5353
Create new instance of the client.
5454
@@ -57,7 +57,7 @@ def __init__(self, base_url=BASE_URL):
5757
"""
5858
BaseClient.__init__(self, base_url)
5959

60-
def authenticate(self, client_id, client_secret, scopes):
60+
def authenticate(self, client_id: str, client_secret: str, scopes: list[Scope]) -> dict:
6161
"""
6262
Generate a two-legged access token for specific set of scopes.
6363
@@ -88,7 +88,7 @@ def authenticate(self, client_id, client_secret, scopes):
8888
}
8989
return self._post('/authenticate', form=form).json()
9090

91-
def get_authorization_url(self, client_id, response_type, redirect_uri, scopes, state=None):
91+
def get_authorization_url(self, client_id: str, response_type: str, redirect_uri: str, scopes: list[Scope], state: str=None) -> str:
9292
"""
9393
Generate a URL to redirect an end user to in order to acquire the user’s consent
9494
for your app to access the specified resources.
@@ -122,7 +122,7 @@ def get_authorization_url(self, client_id, response_type, redirect_uri, scopes,
122122
url += '&state={}'.format(quote(state))
123123
return url
124124

125-
def get_token(self, client_id, client_secret, code, redirect_uri):
125+
def get_token(self, client_id: str, client_secret: str, code: str, redirect_uri: str) -> dict:
126126
"""
127127
Exchange an authorization code extracted from `get_authorization_url` callback for a three-legged access token.
128128
This API will only be used when the 'Authorization Code' grant type is being adopted.
@@ -159,7 +159,7 @@ def get_token(self, client_id, client_secret, code, redirect_uri):
159159
}
160160
return self._post('/gettoken', form=form).json()
161161

162-
def refresh_token(self, client_id, client_secret, refresh_token, scopes):
162+
def refresh_token(self, client_id: str, client_secret: str, refresh_token: str, scopes: list[Scope]) -> dict:
163163
"""
164164
Acquire a new access token by using the refresh token provided by `get_token`.
165165
@@ -193,7 +193,7 @@ def refresh_token(self, client_id, client_secret, refresh_token, scopes):
193193
}
194194
return self._post('/refreshtoken', form=form).json()
195195

196-
def get_user_profile(self, access_token):
196+
def get_user_profile(self, access_token: str) -> dict:
197197
"""
198198
Get the profile information of an authorizing end user in a three-legged context.
199199
@@ -217,7 +217,7 @@ def get_user_profile(self, access_token):
217217
return self._get('/users/@me', headers=headers).json()
218218

219219
class TokenProviderInterface:
220-
def get_token(self, scopes):
220+
def get_token(self, scopes: list[Scope]) -> str:
221221
"""
222222
Generates access token for given set of scopes.
223223
@@ -235,7 +235,7 @@ class SimpleTokenProvider(TokenProviderInterface):
235235
When using this approach, make sure that the hard-coded access token supports all the scopes that may be needed.
236236
"""
237237

238-
def __init__(self, access_token):
238+
def __init__(self, access_token: str):
239239
"""
240240
Create new instance of the provider.
241241
@@ -244,15 +244,15 @@ def __init__(self, access_token):
244244
"""
245245
self.access_token = access_token
246246

247-
def get_token(self, scopes):
247+
def get_token(self, scopes: list[Scope]) -> str:
248248
return self.access_token
249249

250250
class OAuthTokenProvider(TokenProviderInterface):
251251
"""
252252
Helper class that automatically generates (and caches) access tokens using specific app credentials.
253253
"""
254254

255-
def __init__(self, client_id, client_secret):
255+
def __init__(self, client_id: str, client_secret: str):
256256
"""
257257
Create new instance of the provider.
258258
@@ -265,7 +265,7 @@ def __init__(self, client_id, client_secret):
265265
self.auth_client = AuthenticationClient()
266266
self.cache = {}
267267

268-
def get_token(self, scopes):
268+
def get_token(self, scopes: list[Scope]) -> str:
269269
cache_key = '+'.join(map(lambda s: s.value, scopes))
270270
now = datetime.now()
271271
if cache_key in self.cache:
@@ -277,7 +277,7 @@ def get_token(self, scopes):
277277
return auth
278278

279279
class BaseOAuthClient(BaseClient):
280-
def __init__(self, token_provider, base_url):
280+
def __init__(self, token_provider: TokenProviderInterface, base_url: str):
281281
"""
282282
Create new instance of the client.
283283
@@ -288,31 +288,31 @@ def __init__(self, token_provider, base_url):
288288
BaseClient.__init__(self, base_url)
289289
self.token_provider = token_provider
290290

291-
def _get(self, url, scopes, params=None, headers=None):
291+
def _get(self, url: str, scopes: list[Scope], params: dict=None, headers: dict=None):
292292
if not headers:
293293
headers = {}
294294
self._set_auth_headers(headers, scopes)
295295
return BaseClient._get(self, url, params, headers)
296296

297-
def _post(self, url, scopes, form=None, json=None, buff=None, params=None, headers=None):
297+
def _post(self, url: str, scopes: list[Scope], form: dict=None, json: dict=None, buff=None, params: dict=None, headers: dict=None):
298298
if not headers:
299299
headers = {}
300300
self._set_auth_headers(headers, scopes)
301301
return BaseClient._post(self, url, form, json, buff, params, headers)
302302

303-
def _put(self, url, scopes, form=None, json=None, buff=None, params=None, headers=None):
303+
def _put(self, url: str, scopes: list[Scope], form: dict=None, json: dict=None, buff=None, params: dict=None, headers: dict=None):
304304
if not headers:
305305
headers = {}
306306
self._set_auth_headers(headers, scopes)
307307
return BaseClient._put(self, url, form, json, buff, params, headers)
308308

309-
def _delete(self, url, scopes, params=None, headers=None):
309+
def _delete(self, url: str, scopes: list[Scope], params: dict=None, headers: dict=None):
310310
if not headers:
311311
headers = {}
312312
self._set_auth_headers(headers, scopes)
313313
return BaseClient._delete(self, url, params, headers)
314314

315-
def _set_auth_headers(self, headers, scopes):
315+
def _set_auth_headers(self, headers: dict, scopes: list[Scope]):
316316
if not 'Authorization' in headers:
317317
auth = self.token_provider.get_token(scopes)
318318
headers['Authorization'] = 'Bearer {}'.format(auth['access_token'])

src/autodesk_forge_sdk/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import requests
22

33
class BaseClient(object):
4-
def __init__(self, base_url):
4+
def __init__(self, base_url: str):
55
self.base_url = base_url
66

7-
def _resolve_url(self, url):
7+
def _resolve_url(self, url: str) -> str:
88
if url.startswith('/'):
99
url = self.base_url + url
1010
return url
1111

12-
def _get(self, url, params=None, headers=None):
12+
def _get(self, url: str, params: dict=None, headers: dict=None) -> requests.Response:
1313
url = self._resolve_url(url)
1414
response = requests.get(url, params=params, headers=headers)
1515
response.raise_for_status()
1616
return response
1717

18-
def _post(self, url, form=None, json=None, buff=None, params=None, headers=None):
18+
def _post(self, url: str, form: dict=None, json: dict=None, buff=None, params: dict=None, headers: dict=None) -> requests.Response:
1919
url = self._resolve_url(url)
2020
response = None
2121
if form:
@@ -29,7 +29,7 @@ def _post(self, url, form=None, json=None, buff=None, params=None, headers=None)
2929
response.raise_for_status()
3030
return response
3131

32-
def _put(self, url, form=None, json=None, buff=None, params=None, headers=None):
32+
def _put(self, url: str, form: dict=None, json: dict=None, buff=None, params: dict=None, headers: dict=None) -> requests.Response:
3333
url = self._resolve_url(url)
3434
response = None
3535
if form:
@@ -43,7 +43,7 @@ def _put(self, url, form=None, json=None, buff=None, params=None, headers=None):
4343
response.raise_for_status()
4444
return response
4545

46-
def _delete(self, url, params=None, headers=None):
46+
def _delete(self, url: str, params: dict=None, headers: dict=None) -> requests.Response:
4747
url = self._resolve_url(url)
4848
response = requests.delete(url, params=params, headers=headers)
4949
response.raise_for_status()

0 commit comments

Comments
 (0)