Skip to content

Commit

Permalink
feat: use httpx instead of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
koterpillar committed Aug 19, 2024
1 parent 32b7e53 commit 037671b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 173 deletions.
4 changes: 2 additions & 2 deletions mybox/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from abc import ABC, abstractmethod
from typing import Annotated, Any

import requests
from bs4 import BeautifulSoup
from jsonpath_ng import JSONPath as JSONPathT # type: ignore
from jsonpath_ng.ext import parse as jsonpath_parse # type: ignore
from pydantic import BaseModel, ConfigDict, field_validator
from pydantic.functional_validators import BeforeValidator

from .filters import Filters, choose
from .utils import http_get


class ValueC(BaseModel, ABC):
Expand Down Expand Up @@ -57,7 +57,7 @@ async def compute(self) -> str:

class URL(Derived):
async def derived_value(self, contents: str) -> str:
return requests.get(contents).text
return await http_get(contents)


class JSONPath(Derived, Filters):
Expand Down
15 changes: 10 additions & 5 deletions mybox/package/github.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import json
import os
from dataclasses import dataclass
from subprocess import CalledProcessError
from typing import Any, Iterator, Optional

import requests
from pydantic import Field

from ..driver import OS, Architecture
from ..filters import Filter, Filters, choose
from ..utils import allow_singular_none, async_cached, async_cached_lock, run_output
from ..utils import (
allow_singular_none,
async_cached,
async_cached_lock,
http_get,
run_output,
)
from .archive import ArchivePackage


Expand All @@ -34,10 +40,9 @@ async def github_api(url: str) -> Any:
if token:
headers["Authorization"] = f"token {token}"

result = requests.get(f"https://api.github.com/{url}", headers=headers)
result.raise_for_status()
result = await http_get(f"https://api.github.com/{url}", headers=headers)

return result.json()
return json.loads(result)


@dataclass
Expand Down
9 changes: 3 additions & 6 deletions mybox/package/installer/brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
from dataclasses import dataclass
from typing import Optional

import requests

from ...driver import RunResultOutput
from ...utils import async_cached, async_cached_lock
from ...utils import async_cached, async_cached_lock, http_get
from .base import PackageCacheInstaller, PackageVersionInfo


Expand Down Expand Up @@ -172,10 +170,9 @@ async def get_package_info(
# If queried for installed packages, check for any casks with the same
# names as installed formulae
if not package:
cask_response = requests.get("https://formulae.brew.sh/api/cask.json")
cask_response.raise_for_status()
cask_response = await http_get("https://formulae.brew.sh/api/cask.json")

all_casks = cask_response.json()
all_casks = json.loads(cask_response)
for cask in all_casks:
name = cask["token"]
if name not in results:
Expand Down
2 changes: 1 addition & 1 deletion mybox/package/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def url(self) -> Optional[str]:

async def get_remote_version(self) -> str:
if url := await self.url():
return url_version(url)
return await url_version(url)
if self.auto_updates:
return "latest"
return await (await self.installer()).latest_version(self.system)
Expand Down
2 changes: 1 addition & 1 deletion mybox/package/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ def derive_name(self) -> str:
return name

async def get_remote_version(self) -> str:
return url_version(await self.url())
return await url_version(await self.url())
15 changes: 12 additions & 3 deletions mybox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Any, Callable, Coroutine, Iterable, Optional, TypeVar, overload

import requests
import httpx
import trio
from pydantic import field_validator

Expand Down Expand Up @@ -79,8 +79,17 @@ def flatten(items: Iterable[Iterable[T]]) -> list[T]:
return [item for sublist in items for item in sublist]


def url_version(url: str) -> str:
head_response = requests.head(url, allow_redirects=True)
http_client = httpx.AsyncClient()


async def http_get(url: str, headers: Optional[dict[str, str]] = None) -> str:
response = await http_client.get(url, headers=headers)
response.raise_for_status()
return response.text


async def url_version(url: str) -> str:
head_response = await http_client.head(url, follow_redirects=True)
return head_response.headers["etag"]


Expand Down
231 changes: 78 additions & 153 deletions poetry.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 @@ -11,12 +11,12 @@ license = "GPL-3.0-or-later"
pydantic = "^2.8.2"
python = "^3.11"
PyYAML = "^6.0.2"
requests = "^2.32.3"
trio = "^0.26.2"
tqdm = "^4.66.5"
typed-argparse = "^0.3.1"
jsonpath-ng = "^1.6.1"
beautifulsoup4 = "^4.12.3"
httpx = "^0.27.0"

[tool.poetry.group.dev.dependencies]
black = "^24.8.0"
Expand All @@ -31,7 +31,6 @@ pytest-trio = "^0.8.0"
trio-typing = {extras = ["mypy"], version = "^0.10.0"}
types-beautifulsoup4 = "^4.12.0.7"
types-PyYAML = "^6.0.12.12"
types-requests = "^2.31.0.10"
types-tqdm = "^4.66.0.4"

[tool.poetry.scripts]
Expand Down

0 comments on commit 037671b

Please sign in to comment.