Make user_agent a parameter of GitHubSession

So it can be set when used by other packages
This commit is contained in:
Valentin Lorentz 2022-04-25 15:53:41 +02:00
parent 2d04244cc9
commit d715aaf903
2 changed files with 8 additions and 5 deletions

View file

@ -14,6 +14,7 @@ import iso8601
from swh.scheduler.interface import SchedulerInterface
from swh.scheduler.model import ListedOrigin
from .. import USER_AGENT
from ..pattern import CredentialsType, Lister
from .utils import GitHubSession, MissingRateLimitReset
@ -85,7 +86,9 @@ class GitHubLister(Lister[GitHubListerState, List[Dict[str, Any]]]):
self.relisting = self.first_id is not None or self.last_id is not None
self.github_session = GitHubSession(credentials=self.credentials)
self.github_session = GitHubSession(
credentials=self.credentials, user_agent=USER_AGENT
)
def state_from_dict(self, d: Dict[str, Any]) -> GitHubListerState:
return GitHubListerState(**d)

View file

@ -17,8 +17,6 @@ from tenacity import (
wait_exponential,
)
from .. import USER_AGENT
logger = logging.getLogger(__name__)
@ -52,7 +50,9 @@ class GitHubSession:
"""Manages a :class:`requests.Session` with (optionally) multiple credentials,
and cycles through them when reaching rate-limits."""
def __init__(self, credentials: Optional[List[Dict[str, str]]] = None) -> None:
def __init__(
self, user_agent: str, credentials: Optional[List[Dict[str, str]]] = None
) -> None:
"""Initialize a requests session with the proper headers for requests to
GitHub."""
self.credentials = credentials
@ -62,7 +62,7 @@ class GitHubSession:
self.session = requests.Session()
self.session.headers.update(
{"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT}
{"Accept": "application/vnd.github.v3+json", "User-Agent": user_agent}
)
self.anonymous = not self.credentials