Skip to content

Commit 0e2d156

Browse files
committed
Add Accept header
Fedora infra needs it (otherwise packit is treated like any other AI bot and it is given back the Anubis page).
1 parent 95aee6a commit 0e2d156

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

ogr/services/pagure/project.py

Lines changed: 7 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,17 @@ 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(self, path: str, ref=None, header=None) -> str:
466469
ref = ref or self.default_branch
470+
# This method is used for retrieving packit.yaml
471+
header = {"Accept": "application/yaml"} if header is None else header
467472
result = self._call_project_api_raw(
468473
"raw",
469474
ref,
470475
"f",
471476
path,
472477
add_api_endpoint_part=False,
478+
header=header,
473479
)
474480

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

ogr/services/pagure/service.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ 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}
@@ -193,6 +195,7 @@ def call_api_raw(
193195
url: str,
194196
method: Optional[str] = None,
195197
params: Optional[dict] = None,
198+
header: Optional[dict] = None,
196199
data=None,
197200
):
198201
"""
@@ -202,6 +205,7 @@ def call_api_raw(
202205
url: URL to be called.
203206
method: Method of the HTTP request, e.g. `"GET"`, `"POST"`, etc.
204207
params: HTTP(S) query parameters in form of a dictionary.
208+
header: HTTP request header, dict that will update default headers
205209
data: Data to be sent in form of a dictionary.
206210
207211
Returns:
@@ -216,6 +220,7 @@ def call_api_raw(
216220
url=url,
217221
params=params,
218222
data=data,
223+
header=header,
219224
)
220225

221226
except requests.exceptions.ConnectionError as er:
@@ -257,14 +262,22 @@ def get_raw_request(
257262
ValueError, if JSON cannot be retrieved.
258263
"""
259264

265+
updated_header = (
266+
{**self.header, **header} if header is not None else self.header
267+
)
268+
260269
response = self.session.request(
261270
method=method,
262271
url=url,
263272
params=params,
264-
headers=header or self.header,
273+
headers=updated_header,
265274
data=data,
266275
verify=not self.insecure,
267276
)
277+
headers_to_log = updated_header.copy()
278+
if "Authorization" in headers_to_log:
279+
headers_to_log["Authorization"] = "<redacted>"
280+
logger.debug(f"Ogr sent request with following headers: {headers_to_log}")
268281

269282
json_output = None
270283
try:

0 commit comments

Comments
 (0)