-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper class for PyPI interaction (#18)
* Add wrapper class for PyPI interaction Signed-off-by: GitHub <[email protected]> * Add endpoint to call the echo endpoint Signed-off-by: GitHub <[email protected]> * Remove duplicate route path It's already present in the base URL Signed-off-by: GitHub <[email protected]> --------- Signed-off-by: GitHub <[email protected]>
- Loading branch information
1 parent
9c1bbcd
commit 25c481d
Showing
6 changed files
with
64 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from collections.abc import Generator | ||
from functools import cache | ||
from typing import Annotated | ||
|
||
import httpx | ||
from fastapi import Depends | ||
from fastapi.encoders import jsonable_encoder | ||
|
||
from reporter.constants import PyPI | ||
from reporter.models import Observation | ||
|
||
|
||
class BearerAuthentication(httpx.Auth): | ||
def __init__(self, *, token: str) -> None: | ||
self.token = token | ||
|
||
def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: | ||
request.headers["Authorization"] = f"Bearer {self.token}" | ||
yield request | ||
|
||
|
||
class PyPIClient: | ||
"""PyPI client to interact with the PyPI API.""" | ||
|
||
def __init__(self) -> None: | ||
auth = BearerAuthentication(token=PyPI.api_token) | ||
self.http_client = httpx.AsyncClient(auth=auth, base_url=PyPI.base_url) | ||
|
||
async def echo(self) -> str: | ||
response = await self.http_client.get("/echo") | ||
return response.text | ||
|
||
async def send_observation(self, project_name: str, observation: Observation) -> None: | ||
path = f"/projects/{project_name}/observations" | ||
json = jsonable_encoder(observation) | ||
|
||
await self.http_client.post(path, json=json) | ||
|
||
|
||
@cache | ||
def get_pypi_client() -> PyPIClient: | ||
return PyPIClient() | ||
|
||
|
||
PyPIClientDependency = Annotated[PyPIClient, Depends(get_pypi_client)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters