Skip to content

Commit

Permalink
chore: refactor InNoHassle Accounts client
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Mar 28, 2024
1 parent 751ccf3 commit 7901639
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/repositories/innohassle_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserSchema(BaseModel):
# innohassle_admin: bool = False


class InNoHassleAcounts:
class InNoHassleAccounts:
api_url: str
api_jwt_token: str
PUBLIC_KID = "public"
Expand All @@ -43,14 +43,13 @@ async def get_key_set(self) -> KeySet:
return JsonWebKey.import_key_set(jwks_json)

def get_authorized_client(self) -> httpx.AsyncClient:
return httpx.AsyncClient(headers={"Authorization": f"Bearer {self.api_jwt_token}"})
return httpx.AsyncClient(headers={"Authorization": f"Bearer {self.api_jwt_token}"}, base_url=self.api_url)

async def get_user_by_id(self, user_id: str) -> UserSchema | None:
async with self.get_authorized_client() as client:
response = await client.get(f"{self.api_url}/users/by-id/{user_id}")
response = await client.get(f"/users/by-id/{user_id}")
try:
response.raise_for_status()

return UserSchema.model_validate(response.json())
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
Expand All @@ -59,17 +58,16 @@ async def get_user_by_id(self, user_id: str) -> UserSchema | None:

async def get_user_by_email(self, email: str) -> UserSchema | None:
async with self.get_authorized_client() as client:
response = await client.get(f"{self.api_url}/users/by-innomail/{email}")
response = await client.get(f"/users/by-innomail/{email}")
try:
response.raise_for_status()

return UserSchema.model_validate(response.json())
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
return None
raise e


innohassle_accounts = InNoHassleAcounts(
innohassle_accounts = InNoHassleAccounts(
api_url=settings.accounts.api_url, api_jwt_token=settings.accounts.api_jwt_token.get_secret_value()
)

0 comments on commit 7901639

Please sign in to comment.