Skip to content

Commit 2a335c1

Browse files
Add Accept header (#954)
Fedora infra needs it (otherwise packit is treated like any other AI bot and it is given back the Anubis page).
2 parents 95aee6a + 7a1cbe9 commit 2a335c1

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

ogr/abstract/git_project.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,12 @@ def change_token(self, new_token: str) -> None:
618618
"""
619619
raise NotImplementedError
620620

621-
def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
621+
def get_file_content(
622+
self,
623+
path: str,
624+
ref: Optional[str] = None,
625+
headers: Optional[dict[str, str]] = None,
626+
) -> str:
622627
"""
623628
Get a content of the file in the repo.
624629
@@ -627,6 +632,9 @@ def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
627632
ref: Branch or commit.
628633
629634
Defaults to repo's default branch.
635+
headers: Additional headers to be sent with the request.
636+
637+
Defaults to `None`, which means no headers.
630638
631639
Returns:
632640
Contents of the file as string.

ogr/services/forgejo/project.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,12 @@ def change_token(self, new_token: str) -> None:
493493
"Not possible; requires recreation of the httpx client",
494494
)
495495

496-
def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
496+
def get_file_content(
497+
self,
498+
path: str,
499+
ref: Optional[str] = None,
500+
headers: Optional[dict[str, str]] = None,
501+
) -> str:
497502
try:
498503
remote_file: types.ContentsResponse = self.partial_api(
499504
self.api.repo_get_contents,

ogr/services/github/project.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,12 @@ def fork_create(self, namespace: Optional[str] = None) -> "GithubProject":
451451
def change_token(self, new_token: str):
452452
raise OperationNotSupported
453453

454-
def get_file_content(self, path: str, ref=None) -> str:
454+
def get_file_content(
455+
self,
456+
path: str,
457+
ref: Optional[str] = None,
458+
headers: Optional[dict[str, str]] = None,
459+
) -> str:
455460
ref = ref or self.default_branch
456461
try:
457462
return self.github_repo.get_contents(

ogr/services/gitlab/project.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,12 @@ def get_commits(self, ref: Optional[str] = None) -> list[str]:
405405
for commit in self.gitlab_repo.commits.list(ref_name=ref, all=True)
406406
]
407407

408-
def get_file_content(self, path, ref=None) -> str:
408+
def get_file_content(
409+
self,
410+
path: str,
411+
ref: Optional[str] = None,
412+
headers: Optional[dict[str, str]] = None,
413+
) -> str:
409414
ref = ref or self.default_branch
410415
# GitLab cannot resolve './'
411416
path = os.path.normpath(path)

ogr/services/pagure/project.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def _call_project_api_raw(
137137
method: Optional[str] = None,
138138
params: Optional[dict] = None,
139139
data: Optional[dict] = None,
140+
header: Optional[dict] = None,
140141
) -> RequestResponse:
141142
"""
142143
Call project API endpoint.
@@ -152,6 +153,7 @@ def _call_project_api_raw(
152153
method: Method of the HTTP request, e.g. `"GET"`, `"POST"`, etc.
153154
params: HTTP(S) query parameters in form of a dictionary.
154155
data: Data to be sent in form of a dictionary.
156+
header: HTTP request header, dict that will update default headers
155157
156158
Returns:
157159
`RequestResponse` object containing response.
@@ -167,6 +169,7 @@ def _call_project_api_raw(
167169
method=method,
168170
params=params,
169171
data=data,
172+
header=header,
170173
)
171174

172175
def _get_project_url(self, *args, add_fork_part=True, add_api_endpoint_part=True):
@@ -462,14 +465,21 @@ def add_user_or_group(
462465
def change_token(self, new_token: str) -> None:
463466
self.service.change_token(new_token)
464467

465-
def get_file_content(self, path: str, ref=None) -> str:
468+
def get_file_content(
469+
self,
470+
path: str,
471+
ref: Optional[str] = None,
472+
headers: Optional[dict[str, str]] = None,
473+
) -> str:
466474
ref = ref or self.default_branch
475+
headers = {"Accept": "text/plain", **(headers or {})}
467476
result = self._call_project_api_raw(
468477
"raw",
469478
ref,
470479
"f",
471480
path,
472481
add_api_endpoint_part=False,
482+
header=headers,
473483
)
474484

475485
if not result or result.status_code == HTTPStatus.NOT_FOUND:

ogr/services/pagure/service.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ def __init__(
5959
self.session.mount("https://", adapter)
6060

6161
self.header = {"Authorization": "token " + self._token} if self._token else {}
62+
# By default ogr deals with Pagure API -> json
63+
self.header["Accept"] = "application/json"
6264

6365
if user_agent:
6466
self.header |= {"User-Agent": user_agent}
67+
else:
68+
try:
69+
from ogr import __version__ as ogr_version
70+
except ImportError:
71+
ogr_version = "dev"
72+
self.header |= {"User-Agent": f"ogr/{ogr_version} ([email protected])"}
6573

6674
if kwargs:
6775
logger.warning(f"Ignored keyword arguments: {kwargs}")
@@ -193,6 +201,7 @@ def call_api_raw(
193201
url: str,
194202
method: Optional[str] = None,
195203
params: Optional[dict] = None,
204+
header: Optional[dict] = None,
196205
data=None,
197206
):
198207
"""
@@ -202,6 +211,7 @@ def call_api_raw(
202211
url: URL to be called.
203212
method: Method of the HTTP request, e.g. `"GET"`, `"POST"`, etc.
204213
params: HTTP(S) query parameters in form of a dictionary.
214+
header: HTTP request header, dict that will update default headers
205215
data: Data to be sent in form of a dictionary.
206216
207217
Returns:
@@ -216,6 +226,7 @@ def call_api_raw(
216226
url=url,
217227
params=params,
218228
data=data,
229+
header=header,
219230
)
220231

221232
except requests.exceptions.ConnectionError as er:
@@ -257,14 +268,19 @@ def get_raw_request(
257268
ValueError, if JSON cannot be retrieved.
258269
"""
259270

271+
headers = self.header | (header or {})
272+
260273
response = self.session.request(
261274
method=method,
262275
url=url,
263276
params=params,
264-
headers=header or self.header,
277+
headers=headers,
265278
data=data,
266279
verify=not self.insecure,
267280
)
281+
logger.debug(
282+
f"Ogr sent request with following headers: {headers | {'Authorization': '<redacted>'}}",
283+
)
268284

269285
json_output = None
270286
try:

0 commit comments

Comments
 (0)