Skip to content

Commit

Permalink
feat: migrate types to pydantic models
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed Nov 5, 2021
1 parent 9df4927 commit a0e4008
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 172 deletions.
16 changes: 8 additions & 8 deletions gotrue/_async/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def sign_in_with_email(
data = {"email": email, "password": password}
url = f"{self.url}/token{query_string}"
response = await self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

async def sign_up_with_phone(
self,
Expand Down Expand Up @@ -175,7 +175,7 @@ async def sign_in_with_phone(
data = {"phone": phone, "password": password}
url = f"{self.url}/token{query_string}"
response = await self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

async def send_magic_link_email(
self,
Expand Down Expand Up @@ -304,7 +304,7 @@ async def invite_user_by_email(
data = {"email": email, "data": data}
url = f"{self.url}/invite{query_string}"
response = await self.http_client.post(url, json=data, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

async def reset_password_for_email(
self,
Expand Down Expand Up @@ -426,7 +426,7 @@ async def get_user(self, *, jwt: str) -> User:
headers = self._create_request_headers(jwt=jwt)
url = f"{self.url}/user"
response = await self.http_client.get(url, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

async def update_user(
self,
Expand Down Expand Up @@ -455,10 +455,10 @@ async def update_user(
If an error occurs
"""
headers = self._create_request_headers(jwt=jwt)
data = attributes.to_dict()
data = attributes.dict()
url = f"{self.url}/user"
response = await self.http_client.put(url, json=data, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

async def delete_user(self, *, uid: str, jwt: str) -> User:
"""Delete a user. Requires a `service_role` key.
Expand Down Expand Up @@ -486,7 +486,7 @@ async def delete_user(self, *, uid: str, jwt: str) -> User:
headers = self._create_request_headers(jwt=jwt)
url = f"{self.url}/admin/users/${uid}"
response = await self.http_client.delete(url, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

async def refresh_access_token(self, *, refresh_token: str) -> Session:
"""Generates a new JWT.
Expand All @@ -511,7 +511,7 @@ async def refresh_access_token(self, *, refresh_token: str) -> Session:
data = {"refresh_token": refresh_token}
url = f"{self.url}/token{query_string}"
response = await self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

async def generate_link(
self,
Expand Down
6 changes: 3 additions & 3 deletions gotrue/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
auto_refresh_token: bool = True,
persist_session: bool = True,
local_storage: AsyncSupportedStorage = AsyncMemoryStorage(),
cookie_options: CookieOptions = CookieOptions.from_dict(COOKIE_OPTIONS),
cookie_options: CookieOptions = CookieOptions(**COOKIE_OPTIONS),
) -> None:
"""Create a new client
Expand Down Expand Up @@ -536,7 +536,7 @@ async def _recover_common(self) -> Optional[tuple[Session, int, int]]:
and session_raw
and isinstance(session_raw, dict)
):
session = Session.from_dict(session_raw)
session = Session(**session_raw)
expires_at = int(expires_at_raw)
time_now = round(time())
return session, expires_at, time_now
Expand Down Expand Up @@ -606,7 +606,7 @@ async def _save_session(self, *, session: Session) -> None:
await self._persist_session(session=session)

async def _persist_session(self, *, session: Session) -> None:
data = {"session": session.to_dict(), "expires_at": session.expires_at}
data = {"session": session.dict(), "expires_at": session.expires_at}
await self.local_storage.set_item(STORAGE_KEY, dumps(data))

async def _remove_session(self) -> None:
Expand Down
16 changes: 8 additions & 8 deletions gotrue/_sync/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def sign_in_with_email(
data = {"email": email, "password": password}
url = f"{self.url}/token{query_string}"
response = self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

def sign_up_with_phone(
self,
Expand Down Expand Up @@ -175,7 +175,7 @@ def sign_in_with_phone(
data = {"phone": phone, "password": password}
url = f"{self.url}/token{query_string}"
response = self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

def send_magic_link_email(
self,
Expand Down Expand Up @@ -304,7 +304,7 @@ def invite_user_by_email(
data = {"email": email, "data": data}
url = f"{self.url}/invite{query_string}"
response = self.http_client.post(url, json=data, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

def reset_password_for_email(
self,
Expand Down Expand Up @@ -426,7 +426,7 @@ def get_user(self, *, jwt: str) -> User:
headers = self._create_request_headers(jwt=jwt)
url = f"{self.url}/user"
response = self.http_client.get(url, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

def update_user(
self,
Expand Down Expand Up @@ -455,10 +455,10 @@ def update_user(
If an error occurs
"""
headers = self._create_request_headers(jwt=jwt)
data = attributes.to_dict()
data = attributes.dict()
url = f"{self.url}/user"
response = self.http_client.put(url, json=data, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

def delete_user(self, *, uid: str, jwt: str) -> User:
"""Delete a user. Requires a `service_role` key.
Expand Down Expand Up @@ -486,7 +486,7 @@ def delete_user(self, *, uid: str, jwt: str) -> User:
headers = self._create_request_headers(jwt=jwt)
url = f"{self.url}/admin/users/${uid}"
response = self.http_client.delete(url, headers=headers)
return parse_response(response, User.from_dict)
return parse_response(response, lambda arg: User(**arg))

def refresh_access_token(self, *, refresh_token: str) -> Session:
"""Generates a new JWT.
Expand All @@ -511,7 +511,7 @@ def refresh_access_token(self, *, refresh_token: str) -> Session:
data = {"refresh_token": refresh_token}
url = f"{self.url}/token{query_string}"
response = self.http_client.post(url, json=data, headers=headers)
return parse_response(response, Session.from_dict)
return parse_response(response, lambda arg: Session(**arg))

def generate_link(
self,
Expand Down
6 changes: 3 additions & 3 deletions gotrue/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
auto_refresh_token: bool = True,
persist_session: bool = True,
local_storage: SyncSupportedStorage = SyncMemoryStorage(),
cookie_options: CookieOptions = CookieOptions.from_dict(COOKIE_OPTIONS),
cookie_options: CookieOptions = CookieOptions(**COOKIE_OPTIONS),
) -> None:
"""Create a new client
Expand Down Expand Up @@ -534,7 +534,7 @@ def _recover_common(self) -> Optional[tuple[Session, int, int]]:
and session_raw
and isinstance(session_raw, dict)
):
session = Session.from_dict(session_raw)
session = Session(**session_raw)
expires_at = int(expires_at_raw)
time_now = round(time())
return session, expires_at, time_now
Expand Down Expand Up @@ -600,7 +600,7 @@ def _save_session(self, *, session: Session) -> None:
self._persist_session(session=session)

def _persist_session(self, *, session: Session) -> None:
data = {"session": session.to_dict(), "expires_at": session.expires_at}
data = {"session": session.dict(), "expires_at": session.expires_at}
self.local_storage.set_item(STORAGE_KEY, dumps(data))

def _remove_session(self) -> None:
Expand Down
10 changes: 7 additions & 3 deletions gotrue/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from time import time
from typing import Any, Callable, TypeVar, Union
from urllib.parse import quote

Expand All @@ -16,13 +17,16 @@ def parse_response(response: Response, func: Callable[[Any], T]) -> T:
try:
response.raise_for_status()
json = response.json()
return func(json)
result = func(json)
if isinstance(result, Session) and result.expires_in and not result.expires_at:
result.expires_at = round(time()) + result.expires_in
return result
except HTTPError:
json = response.json()
raise APIError.from_dict(json)


def parse_session_or_user(arg: Any) -> Union[Session, User]:
if "access_token" in arg:
return Session.from_dict(arg)
return User.from_dict(arg)
return Session(**arg)
return User(**arg)

0 comments on commit a0e4008

Please sign in to comment.