Skip to content

Commit

Permalink
Replaced requests with httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
somehybrid committed Sep 5, 2023
1 parent 28ae550 commit 311b19e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 187 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ which will invoke `docker compose` for you.
We use `pytest` to run our tests. Tests go in the `tests/` directory.
The tests for each python module should go in a separate tests file.

We use `requests` for making requests to the API. Use the fixture `api_url` for the URL to make requests to.
We use `httpx` for making requests to the API. Use the fixture `api_url` for the URL to make requests to.
For example:

```py
def test_root(api_url: str):
r = requests.get(api_url)
r = httpx.get(api_url)
assert r.status_code == 200
```

Expand All @@ -91,7 +91,7 @@ For example:

```py
def test_query(api_url: str, db_session: Session):
r = requests.get(api_url + "/package?name=a&version=0.1.0")
r = httpx.get(api_url + "/package?name=a&version=0.1.0")
data = r.json()
assert r["name"] == "a"
```
Expand Down
295 changes: 130 additions & 165 deletions pdm.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ dependencies = [
"SQLAlchemy>=2.0.12",
"python-dotenv>=1.0.0",
"alembic>=1.10.4",
"letsbuilda-pypi==5.0.0b3",
"letsbuilda-pypi>=5.0.0",
"msgraph-core>=0.2.2",
"azure-identity>=1.13.0",
"structlog>=23.1.0",
"asgi-correlation-id>=4.2.0",
"sentry-sdk[fastapi]>=1.25.1",
"structlog-sentry>=2.0.3",
"requests>=2.30.0",
"httpx>=0.24.1",
"psycopg2-binary",
]
Expand Down
7 changes: 4 additions & 3 deletions src/mainframe/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import cache
from typing import Annotated

import requests
import httpx
from fastapi import Depends, Request
from letsbuilda.pypi import PyPIServices
from msgraph.core import GraphClient
Expand All @@ -21,7 +21,7 @@ def get_ms_graph_client() -> GraphClient: # type: ignore

@cache
def get_pypi_client() -> PyPIServices:
session = requests.Session()
session = httpx.Client()
return PyPIServices(session)


Expand Down Expand Up @@ -53,5 +53,6 @@ def __call__(self, data: Annotated[AuthenticationData, Depends(validate_token)])
token_permissions_set = set(token_permissions) # type: ignore
required_permissions_set = set(self.required_permissions)

if not required_permissions_set.issubset(token_permissions_set): # type: ignore
# type: ignore
if not required_permissions_set.issubset(token_permissions_set):
raise PermissionDeniedException
21 changes: 11 additions & 10 deletions src/mainframe/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Final
from zipfile import ZipFile

from requests import Session
from httpx import Client

from mainframe.constants import mainframe_settings

Expand All @@ -21,14 +21,15 @@ def build_auth_header(access_token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {access_token}"}


def fetch_commit_hash(http_session: Session, *, repository: str, access_token: str) -> str:
def fetch_commit_hash(http_session: Client, *, repository: str, access_token: str) -> str:
"""Fetch the top commit hash of the given repository"""
url = f"https://api.github.com/repos/{repository}/commits/main"
authentication_headers = build_auth_header(access_token)
json_headers = {"Accept": "application/vnd.github.VERSION.sha"}
headers = authentication_headers | json_headers
with http_session.get(url, headers=headers) as res:
return res.text

res = http_session.get(url, headers=headers)
return res.text


def parse_zipfile(zipfile: ZipFile) -> dict[str, str]:
Expand All @@ -45,20 +46,20 @@ def parse_zipfile(zipfile: ZipFile) -> dict[str, str]:
return rules


def fetch_zipfile(http_session: Session, *, repository: str, access_token: str) -> ZipFile:
def fetch_zipfile(http_session: Client, *, repository: str, access_token: str) -> ZipFile:
"""Download the source zipfile from GitHub for the given repository"""
url = f"https://api.github.com/repos/{repository}/zipball/"
headers = build_auth_header(access_token)
buffer = BytesIO()
with http_session.get(url, headers=headers) as res:
res.raise_for_status()
bytes = res.content
buffer.write(bytes)
res = http_session.get(url, headers=headers)
res.raise_for_status()
bytes = res.content
buffer.write(bytes)

return ZipFile(buffer)


def fetch_rules(http_session: Session) -> Rules:
def fetch_rules(http_session: Client) -> Rules:
"""Return the commit hash and all the rules"""

access_token = mainframe_settings.dragonfly_github_token
Expand Down
4 changes: 2 additions & 2 deletions src/mainframe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from asgi_correlation_id import CorrelationIdMiddleware
from asgi_correlation_id.context import correlation_id
from fastapi import FastAPI, Request, Response
from httpx import Client
from letsbuilda.pypi import PyPIServices
from requests import Session
from sentry_sdk.integrations.logging import LoggingIntegration
from structlog_sentry import SentryProcessor

Expand Down Expand Up @@ -99,7 +99,7 @@ def configure_logger():
async def lifespan(app_: FastAPI):
"""Load the state for the app"""

http_session = Session()
http_session = Client()
pypi_client = PyPIServices(http_session)
rules = fetch_rules(http_session=http_session)

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Generator
from unittest.mock import MagicMock

import httpx
import pytest
import requests
from letsbuilda.pypi import PyPIServices
from letsbuilda.pypi.models import Package
from letsbuilda.pypi.models.models_package import Distribution, Release
Expand Down Expand Up @@ -83,7 +83,7 @@ def rules_state() -> Rules:

@pytest.fixture(scope="session")
def pypi_client() -> PyPIServices:
session = requests.Session()
session = httpx.Client()
pypi_client = PyPIServices(session)

def side_effect(name: str, version: str) -> Package:
Expand Down

0 comments on commit 311b19e

Please sign in to comment.