Skip to content

Commit

Permalink
Merge pull request #8 from tomasvotava/emcpow2/master
Browse files Browse the repository at this point in the history
Use httpx instead of aiohttp based on @emcpow2
  • Loading branch information
tomasvotava authored Nov 15, 2021
2 parents d49ab3c + ac6ea2c commit ac49507
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 232 deletions.
38 changes: 19 additions & 19 deletions docs/sso/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ <h1 class="title">Module <code>fastapi_sso.sso.base</code></h1>

from typing import Dict, List, Optional
from uuid import uuid4
import aiohttp
from oauthlib.oauth2 import WebApplicationClient
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import RedirectResponse
import httpx
import pydantic


Expand Down Expand Up @@ -192,15 +192,15 @@ <h1 class="title">Module <code>fastapi_sso.sso.base</code></h1>
if token_url is None:
return None

auth = aiohttp.BasicAuth(self.client_id, self.client_secret)
async with aiohttp.ClientSession() as session:
async with session.post(token_url, headers=headers, data=body, auth=auth) as response:
content = await response.json()
auth = httpx.BasicAuth(self.client_id, self.client_secret)
async with httpx.AsyncClient() as session:
response = await session.post(token_url, headers=headers, content=body, auth=auth)
content = response.json()
self.oauth_client.parse_request_body_response(json.dumps(content))

uri, headers, body = self.oauth_client.add_token(await self.userinfo_endpoint)
async with session.get(uri, headers=headers, data=body) as response:
content = await response.json()
response = await session.get(uri, headers=headers, content=body)
content = response.json()

return await self.openid_from_response(content)</code></pre>
</details>
Expand Down Expand Up @@ -415,15 +415,15 @@ <h3>Class variables</h3>
if token_url is None:
return None

auth = aiohttp.BasicAuth(self.client_id, self.client_secret)
async with aiohttp.ClientSession() as session:
async with session.post(token_url, headers=headers, data=body, auth=auth) as response:
content = await response.json()
auth = httpx.BasicAuth(self.client_id, self.client_secret)
async with httpx.AsyncClient() as session:
response = await session.post(token_url, headers=headers, content=body, auth=auth)
content = response.json()
self.oauth_client.parse_request_body_response(json.dumps(content))

uri, headers, body = self.oauth_client.add_token(await self.userinfo_endpoint)
async with session.get(uri, headers=headers, data=body) as response:
content = await response.json()
response = await session.get(uri, headers=headers, content=body)
content = response.json()

return await self.openid_from_response(content)</code></pre>
</details>
Expand Down Expand Up @@ -644,15 +644,15 @@ <h2 id="returns">Returns</h2>
if token_url is None:
return None

auth = aiohttp.BasicAuth(self.client_id, self.client_secret)
async with aiohttp.ClientSession() as session:
async with session.post(token_url, headers=headers, data=body, auth=auth) as response:
content = await response.json()
auth = httpx.BasicAuth(self.client_id, self.client_secret)
async with httpx.AsyncClient() as session:
response = await session.post(token_url, headers=headers, content=body, auth=auth)
content = response.json()
self.oauth_client.parse_request_body_response(json.dumps(content))

uri, headers, body = self.oauth_client.add_token(await self.userinfo_endpoint)
async with session.get(uri, headers=headers, data=body) as response:
content = await response.json()
response = await session.get(uri, headers=headers, content=body)
content = response.json()

return await self.openid_from_response(content)</code></pre>
</details>
Expand Down
26 changes: 13 additions & 13 deletions docs/sso/google.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1 class="title">Module <code>fastapi_sso.sso.google</code></h1>
&#34;&#34;&#34;

from typing import Dict
import aiohttp
import httpx

from fastapi_sso.sso.base import OpenID, SSOBase, SSOLoginError

Expand Down Expand Up @@ -61,10 +61,10 @@ <h1 class="title">Module <code>fastapi_sso.sso.google</code></h1>
@classmethod
async def get_discovery_document(cls) -&gt; Dict[str, str]:
&#34;&#34;&#34;Get document containing handy urls&#34;&#34;&#34;
async with aiohttp.ClientSession() as session:
async with session.get(cls.discovery_url) as response:
content = await response.json()
return content</code></pre>
async with httpx.AsyncClient() as session:
response = await session.get(cls.discovery_url)
content = response.json()
return content</code></pre>
</details>
</section>
<section>
Expand Down Expand Up @@ -111,10 +111,10 @@ <h2 class="section-title" id="header-classes">Classes</h2>
@classmethod
async def get_discovery_document(cls) -&gt; Dict[str, str]:
&#34;&#34;&#34;Get document containing handy urls&#34;&#34;&#34;
async with aiohttp.ClientSession() as session:
async with session.get(cls.discovery_url) as response:
content = await response.json()
return content</code></pre>
async with httpx.AsyncClient() as session:
response = await session.get(cls.discovery_url)
content = response.json()
return content</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
Expand Down Expand Up @@ -165,10 +165,10 @@ <h3>Static methods</h3>
<pre><code class="python">@classmethod
async def get_discovery_document(cls) -&gt; Dict[str, str]:
&#34;&#34;&#34;Get document containing handy urls&#34;&#34;&#34;
async with aiohttp.ClientSession() as session:
async with session.get(cls.discovery_url) as response:
content = await response.json()
return content</code></pre>
async with httpx.AsyncClient() as session:
response = await session.get(cls.discovery_url)
content = response.json()
return content</code></pre>
</details>
</dd>
<dt id="fastapi_sso.sso.google.GoogleSSO.openid_from_response"><code class="name flex">
Expand Down
14 changes: 7 additions & 7 deletions fastapi_sso/sso/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

from typing import Dict, List, Optional
from uuid import uuid4
import aiohttp
from oauthlib.oauth2 import WebApplicationClient
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import RedirectResponse
import httpx
import pydantic


Expand Down Expand Up @@ -163,14 +163,14 @@ async def process_login(self, code: str, request: Request) -> Optional[OpenID]:
if token_url is None:
return None

auth = aiohttp.BasicAuth(self.client_id, self.client_secret)
async with aiohttp.ClientSession() as session:
async with session.post(token_url, headers=headers, data=body, auth=auth) as response:
content = await response.json()
auth = httpx.BasicAuth(self.client_id, self.client_secret)
async with httpx.AsyncClient() as session:
response = await session.post(token_url, headers=headers, content=body, auth=auth)
content = response.json()
self.oauth_client.parse_request_body_response(json.dumps(content))

uri, headers, body = self.oauth_client.add_token(await self.userinfo_endpoint)
async with session.get(uri, headers=headers, data=body) as response:
content = await response.json()
response = await session.get(uri, headers=headers, content=body)
content = response.json()

return await self.openid_from_response(content)
10 changes: 5 additions & 5 deletions fastapi_sso/sso/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""

from typing import Dict
import aiohttp
import httpx

from fastapi_sso.sso.base import OpenID, SSOBase, SSOLoginError

Expand Down Expand Up @@ -32,7 +32,7 @@ async def openid_from_response(cls, response: dict) -> OpenID:
@classmethod
async def get_discovery_document(cls) -> Dict[str, str]:
"""Get document containing handy urls"""
async with aiohttp.ClientSession() as session:
async with session.get(cls.discovery_url) as response:
content = await response.json()
return content
async with httpx.AsyncClient() as session:
response = await session.get(cls.discovery_url)
content = response.json()
return content
Loading

0 comments on commit ac49507

Please sign in to comment.