Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve headers sent in HTTP requests #182

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/tinuous/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import heapq
import os
from pathlib import Path, PurePosixPath
import platform
import re
from shutil import rmtree
import sys
Expand All @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/tinuous/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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]


Expand Down