diff --git a/src/tinuous/base.py b/src/tinuous/base.py index d57139c..da3321e 100644 --- a/src/tinuous/base.py +++ b/src/tinuous/base.py @@ -7,6 +7,7 @@ import heapq import os from pathlib import Path, PurePosixPath +import platform import re from shutil import rmtree import sys @@ -20,6 +21,7 @@ from requests.exceptions import ChunkedEncodingError from requests.exceptions import ConnectionError as ReqConError +from . import __url__, __version__ from .util import ( delay_until, expand_template, @@ -33,6 +35,14 @@ else: from typing_extensions import Annotated +USER_AGENT = "tinuous/{} ({}) requests/{} {}/{}".format( + __version__, + __url__, + requests.__version__, + platform.python_implementation(), + platform.python_version(), +) + class CommonStatus(Enum): SUCCESS = "success" @@ -104,6 +114,7 @@ class APIClient: def __init__(self, base_url: str, headers: dict[str, str], is_github: bool = False): self.base_url = base_url self.session = requests.Session() + self.session.headers["User-Agent"] = USER_AGENT self.session.headers.update(headers) self.is_github = is_github @@ -148,12 +159,14 @@ def get(self, path: str, **kwargs: Any) -> requests.Response: r.raise_for_status() return r - def download(self, path: str, filepath: Path) -> None: + def download( + self, path: str, filepath: Path, headers: dict[str, str] | None = None + ) -> None: i = 0 while True: try: try: - r = self.get(path, stream=True) + r = self.get(path, stream=True, headers=headers) with filepath.open("wb") as fp: for chunk in r.iter_content(chunk_size=8192): fp.write(chunk) @@ -181,7 +194,7 @@ def download_zipfile(self, path: str, target_dir: Path) -> None: zippath = Path(fpath) i = 0 while True: - self.download(path, zippath) + self.download(path, zippath, headers={"Accept": "*/*"}) try: with ZipFile(zippath) as zf: zf.extractall(target_dir) diff --git a/src/tinuous/github.py b/src/tinuous/github.py index b8244b6..567b57d 100644 --- a/src/tinuous/github.py +++ b/src/tinuous/github.py @@ -35,7 +35,11 @@ def get_auth_tokens() -> dict[str, str]: def client(self) -> APIClient: return APIClient( "https://api.github.com", - {"Authorization": f"token {self.token}"}, + { + "Accept": "application/vnd.github+json", + "Authorization": f"Bearer {self.token}", + "X-GitHub-Api-Version": "2022-11-28", + }, is_github=True, ) @@ -440,7 +444,7 @@ def download(self, path: Path) -> list[Path]: self.tag_name, target, ) - self.client.download(self.download_url, target) + self.client.download(self.download_url, target, headers={"Accept": "*/*"}) return [target]