Skip to content

Commit 073a775

Browse files
authored
feat(async): Add asyncio and FastAPI support (#19)
* chore(test): Code cleaning. * chore(client): Refactor using httpx client. * feat(client): Add asyncio client support * feat(framework): Add FastAPI framework support * chore(test): Fixing tox configs * feat(fastapi): Add error handlers for iam init * fix(setup): Remove FastAPI version * chore(docs): Cleaning docs * chore(changelog): Update changelog. * Bump version: 1.0.0 → 1.1.0
1 parent f14d945 commit 073a775

19 files changed

Lines changed: 1741 additions & 19 deletions

‎CHANGELOG.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Changelog
33
=========
44

5+
1.1.0 (2022-03-28)
6+
------------------
7+
8+
* Add asyncio client support.
9+
* Add FastAPI frameworks support.
10+
511
1.0.0 (2021-11-25)
612
------------------
713

‎docs/asyncio.rst‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=======
2+
Asyncio
3+
=======
4+
5+
iam-python-sdk support concurrency with asyncio client.
6+
All default client functions have asyncio coroutine versions.
7+
You can use async and await on the async functions.
8+
9+
Here is sample of how you can use the iam-python-sdk asyncio client:
10+
11+
.. code-block:: python
12+
13+
import asyncio
14+
15+
from iam_python_sdk import Config, NewAsyncClient
16+
from iam_python_sdk.errors import ClientTokenGrantError
17+
18+
config = Config(
19+
BaseURL="<Base IAM URL>", # e.g.: https://demo.accelbyte.io/iam
20+
ClientID="<Client ID>",
21+
ClientSecret="<Client Secret>",
22+
)
23+
client = NewAsyncClient(config)
24+
25+
async def grant_token():
26+
try:
27+
await client.ClientTokenGrant()
28+
# Client token grant success
29+
except ClientTokenGrantError as e:
30+
# Cant get access token from IAM
31+
pass
32+
33+
loop = asyncio.get_event_loop()
34+
coro = asyncio.ensure_future(main(), loop=loop)
35+
loop.run_until_complete(coro)

‎docs/frameworks.rst‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,98 @@ You can also set the default CORS headers for all endpoints with Flask applicati
119119
120120
.. note::
121121
These default configs will be overridden by the decorator *cors_options* for specific endpoints.
122+
123+
124+
FastAPI
125+
=======
126+
127+
Usage
128+
-----
129+
130+
To use iam-python-sdk on FastAPI frameworks, you have to init the iam-python-sdk when FastAPI app started:
131+
132+
.. code-block:: python
133+
134+
from fastapi import FastAPI
135+
from iam_python_sdk.fastapi import IAM, Settings
136+
137+
app = FastAPI()
138+
139+
@app.on_event("startup")
140+
async def startup_event():
141+
config = Settings(
142+
iam_base_url="<Base IAM URL>",
143+
iam_client_id="<Client ID>",
144+
iam_client_secret="<Client Secret>",
145+
)
146+
app.state.iam = IAM(app, config)
147+
148+
Then you can protect your endpoint with *permission_required* dependency from unauthorized access:
149+
150+
.. code-block:: python
151+
152+
from iam-python-sdk.fastapi import permission_required
153+
154+
@app.get('/protected', dependencies=[
155+
Depends(
156+
permission_required(
157+
{"resource": "ADMIN:NAMESPACE:{namespace}:CLIENT", "action": 2},
158+
{"{namespace}": "sdktest"},
159+
csrf_protect=True
160+
)
161+
)
162+
])
163+
def get_protected_endpoint():
164+
return 'You have authorized access!'
165+
166+
By default, *permission_required* dependency will check the access token on the Authorization header with Bearer type.
167+
You can customize these default configurations according to your service/apps needs:
168+
169+
.. code-block:: python
170+
171+
settings.iam_base_url = ""
172+
settings.iam_client_id = ""
173+
settings.iam_client_secret = ""
174+
settings.iam_token_locations = ["headers", "cookies"]
175+
settings.iam_token_header_name = "Authorization"
176+
settings.iam_token_header_type = "Bearer"
177+
settings.iam_token_cookie_name = "access_token"
178+
settings.iam_token_cookie_path = "/"
179+
settings.iam_csrf_protection = True
180+
settings.iam_strict_referer = True
181+
182+
.. note::
183+
This module has been tested with FastAPI default uvicorn server for development.
184+
For production use, this module has been tested with *Gunicorn*.
185+
You can use Gunicorn with ``uvicorn.workers.UvicornWorker`` class worker.
186+
187+
For more information about FastAPI deployment, please read more information `here <https://fastapi.tiangolo.com/deployment/server-workers/>`_
188+
189+
CORS Middleware
190+
---------------
191+
192+
This module support CORS middleware to set CORS header response. You can set the CORS headers with these settings.
193+
194+
.. code-block:: python
195+
196+
settings.iam_cors_enable = False
197+
settings.iam_cors_origin = "*"
198+
settings.iam_cors_headers = "*"
199+
settings.iam_cors_methods = "*"
200+
settings.iam_cors_credentials = True
201+
202+
The sample response of this endpoint would be like:
203+
204+
.. code-block:: console
205+
206+
HTTP/1.1 200 OK
207+
Date: Fri, 12 Nov 2021 01:15:39 GMT
208+
Server: Nginx
209+
Access-Control-Allow-Origin: *
210+
Access-Control-Allow-Methods: GET, POST, OPTIONS
211+
Access-Control-Allow-Headers: Device-Id, Device-Os, Device-Type
212+
Access-Control-Allow-Credentials: true
213+
.......
214+
215+
.. note::
216+
You can read more about CORS specification `here <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>`_

‎docs/iam_python_sdk.rst‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ iam\_python\_sdk package
44
Submodules
55
----------
66

7+
iam\_python\_sdk\.async\_client module
8+
--------------------------------------
9+
10+
.. automodule:: iam_python_sdk.async_client
11+
:members:
12+
:undoc-members:
13+
:show-inheritance:
14+
715
iam\_python\_sdk\.bloom module
816
------------------------------
917

@@ -52,6 +60,14 @@ iam\_python\_sdk\.errors module
5260
:undoc-members:
5361
:show-inheritance:
5462

63+
iam\_python\_sdk\.fastapi module
64+
--------------------------------
65+
66+
.. automodule:: iam_python_sdk.fastapi
67+
:members:
68+
:undoc-members:
69+
:show-inheritance:
70+
5571
iam\_python\_sdk\.flask module
5672
------------------------------
5773

‎docs/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
installation
88
usage
9+
asyncio
910
frameworks
1011
modules
1112
contributing

‎docs/installation.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ To install iam-python-sdk with *Flask* frameworks support, run this command in y
4242
4343
This will install flask frameworks with the latest stable version.
4444

45+
To install iam-python-sdk with *FastAPI* frameworks support, run this command in your terminal:
46+
47+
.. code-block:: console
48+
49+
$ pip install 'iam-python-sdk[fastapi]'
50+
51+
This will install fastapi frameworks with the latest stable version.
52+
4553

4654
From sources
4755
------------

‎iam_python_sdk/__init__.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
# limitations under the License.
1414

1515
from iam_python_sdk.client import NewDefaultClient
16+
from iam_python_sdk.async_client import NewAsyncClient
1617
from iam_python_sdk.config import Config
1718

1819
__author__ = """Analytics AccelByte"""
1920
__email__ = 'justice-analytics-team@accelbyte.net'
20-
__version__ = '1.0.0'
21+
__version__ = '1.1.0'
2122
__all__ = [
2223
'NewDefaultClient',
24+
'NewAsyncClient',
2325
'Config'
2426
]

0 commit comments

Comments
 (0)