Skip to content

Commit

Permalink
feat: replace requests with httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
omercnet committed Nov 10, 2024
1 parent d3305fa commit f404ed5
Show file tree
Hide file tree
Showing 32 changed files with 784 additions and 898 deletions.
34 changes: 34 additions & 0 deletions descope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,37 @@
UserPasswordFirebase,
UserPasswordPbkdf2,
)

__all__ = [
"COOKIE_DATA_NAME",
"REFRESH_SESSION_COOKIE_NAME",
"REFRESH_SESSION_TOKEN_NAME",
"SESSION_COOKIE_NAME",
"SESSION_TOKEN_NAME",
"AccessKeyLoginOptions",
"DeliveryMethod",
"LoginOptions",
"SignUpOptions",
"DescopeClient",
"API_RATE_LIMIT_RETRY_AFTER_HEADER",
"ERROR_TYPE_API_RATE_LIMIT",
"ERROR_TYPE_SERVER_ERROR",
"AuthException",
"RateLimitException",
"AssociatedTenant",
"SAMLIDPAttributeMappingInfo",
"SAMLIDPGroupsMappingInfo",
"SAMLIDPRoleGroupMappingInfo",
"AttributeMapping",
"OIDCAttributeMapping",
"RoleMapping",
"SSOOIDCSettings",
"SSOSAMLSettings",
"SSOSAMLSettingsByMetadata",
"UserObj",
"UserPassword",
"UserPasswordBcrypt",
"UserPasswordDjango",
"UserPasswordFirebase",
"UserPasswordPbkdf2",
]
30 changes: 15 additions & 15 deletions descope/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
except ImportError:
from pkg_resources import get_distribution

import requests
import httpx
from email_validator import EmailNotValidError, validate_email
from jwt import ExpiredSignatureError, ImmatureSignatureError

Expand Down Expand Up @@ -122,14 +122,14 @@ def do_get(
self,
uri: str,
params=None,
allow_redirects=None,
follow_redirects=None,
pswd: str | None = None,
) -> requests.Response:
response = requests.get(
) -> httpx.Response:
response = httpx.get(
f"{self.base_url}{uri}",
headers=self._get_default_headers(pswd),
params=params,
allow_redirects=allow_redirects,
follow_redirects=follow_redirects,
verify=self.secure,
timeout=self.timeout_seconds,
)
Expand All @@ -142,12 +142,12 @@ def do_post(
body: dict | list[dict] | list[str] | None,
params=None,
pswd: str | None = None,
) -> requests.Response:
response = requests.post(
) -> httpx.Response:
response = httpx.post(
f"{self.base_url}{uri}",
headers=self._get_default_headers(pswd),
json=body,
allow_redirects=False,
follow_redirects=False,
verify=self.secure,
params=params,
timeout=self.timeout_seconds,
Expand All @@ -161,12 +161,12 @@ def do_patch(
body: dict | list[dict] | list[str] | None,
params=None,
pswd: str | None = None,
) -> requests.Response:
response = requests.patch(
) -> httpx.Response:
response = httpx.patch(
f"{self.base_url}{uri}",
headers=self._get_default_headers(pswd),
json=body,
allow_redirects=False,
follow_redirects=False,
verify=self.secure,
params=params,
timeout=self.timeout_seconds,
Expand All @@ -176,12 +176,12 @@ def do_patch(

def do_delete(
self, uri: str, params=None, pswd: str | None = None
) -> requests.Response:
response = requests.delete(
) -> httpx.Response:
response = httpx.delete(
f"{self.base_url}{uri}",
params=params,
headers=self._get_default_headers(pswd),
allow_redirects=False,
follow_redirects=False,
verify=self.secure,
timeout=self.timeout_seconds,
)
Expand Down Expand Up @@ -419,7 +419,7 @@ def _raise_from_response(self, response):

def _fetch_public_keys(self) -> None:
# This function called under mutex protection so no need to acquire it once again
response = requests.get(
response = httpx.get(
f"{self.base_url}{EndpointsV2.public_key_path}/{self.project_id}",
headers=self._get_default_headers(),
verify=self.secure,
Expand Down
4 changes: 2 additions & 2 deletions descope/authmethod/enchantedlink.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

import requests
import httpx

from descope._auth_base import AuthBase
from descope.auth import Auth
Expand Down Expand Up @@ -197,5 +197,5 @@ def _compose_get_session_body(pending_ref: str) -> dict:
return {"pendingRef": pending_ref}

@staticmethod
def _get_pending_ref_from_response(response: requests.Response) -> dict:
def _get_pending_ref_from_response(response: httpx.Response) -> dict:
return response.json()
2 changes: 1 addition & 1 deletion descope/authmethod/webauthn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterable, Optional, Union

from requests import Response
from httpx import Response

from descope._auth_base import AuthBase
from descope.common import (
Expand Down
14 changes: 7 additions & 7 deletions descope/descope_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Iterable

import requests
import httpx

from descope.auth import Auth # noqa: F401
from descope.authmethod.enchantedlink import EnchantedLink # noqa: F401
Expand Down Expand Up @@ -362,15 +362,15 @@ def validate_and_refresh_session(
session_token, refresh_token, audience
)

def logout(self, refresh_token: str) -> requests.Response:
def logout(self, refresh_token: str) -> httpx.Response:
"""
Logout user from current session and revoke the refresh_token. After calling this function,
you must invalidate or remove any cookies you have created.
Args:
refresh_token (str): The refresh token
Return value (requests.Response): returns the response from the Descope server
Return value (httpx.Response): returns the response from the Descope server
Raise:
AuthException: Exception is raised if session is not authorized or another error occurs
Expand All @@ -385,15 +385,15 @@ def logout(self, refresh_token: str) -> requests.Response:
uri = EndpointsV1.logout_path
return self._auth.do_post(uri, {}, None, refresh_token)

def logout_all(self, refresh_token: str) -> requests.Response:
def logout_all(self, refresh_token: str) -> httpx.Response:
"""
Logout user from all active sessions and revoke the refresh_token. After calling this function,
you must invalidate or remove any cookies you have created.
Args:
refresh_token (str): The refresh token
Return value (requests.Response): returns the response from the Descope server
Return value (httpx.Response): returns the response from the Descope server
Raise:
AuthException: Exception is raised if session is not authorized or another error occurs
Expand Down Expand Up @@ -431,7 +431,7 @@ def me(self, refresh_token: str) -> dict:

uri = EndpointsV1.me_path
response = self._auth.do_get(
uri=uri, params=None, allow_redirects=None, pswd=refresh_token
uri=uri, params=None, follow_redirects=None, pswd=refresh_token
)
return response.json()

Expand Down Expand Up @@ -513,7 +513,7 @@ def history(self, refresh_token: str) -> list[dict]:

uri = EndpointsV1.history_path
response = self._auth.do_get(
uri=uri, params=None, allow_redirects=None, pswd=refresh_token
uri=uri, params=None, follow_redirects=None, pswd=refresh_token
)
return response.json()

Expand Down
Loading

0 comments on commit f404ed5

Please sign in to comment.