Skip to content

Commit 42d258d

Browse files
authored
内部リソースにアクセスする処理を変更 (#346)
* 内部リソースにアクセスする処理を変更 * version up * poetry update
1 parent 3ac9aad commit 42d258d

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.46.4"
1+
__version__ = "0.46.5"

annofabapi/api.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def _request_wrapper(
199199
request_body: Optional[Any] = None,
200200
) -> Tuple[Any, requests.Response]:
201201
"""
202-
HTTP Requestを投げて、Responseを返す。
202+
HTTP Requestを投げて、Responseを返す。
203203
204204
Args:
205205
http_method:
@@ -236,7 +236,9 @@ def _request_wrapper(
236236
content = self._response_to_content(response)
237237
return content, response
238238

239-
def _get_signed_cookie(self, project_id) -> Tuple[Dict[str, Any], requests.Response]:
239+
def _get_signed_cookie(
240+
self, project_id, query_params: Optional[Dict[str, Any]] = None
241+
) -> Tuple[Dict[str, Any], requests.Response]:
240242
"""
241243
アノテーション仕様の履歴情報を取得するために、非公開APIにアクセスする。
242244
変更される可能性あり.
@@ -250,7 +252,7 @@ def _get_signed_cookie(self, project_id) -> Tuple[Dict[str, Any], requests.Respo
250252
"""
251253
url_path = f"/internal/projects/{project_id}/sign-headers"
252254
http_method = "GET"
253-
keyword_params: Dict[str, Any] = {}
255+
keyword_params: Dict[str, Any] = {"query_params": query_params}
254256
return self._request_wrapper(http_method, url_path, **keyword_params)
255257

256258
def _request_get_with_cookie(self, project_id: str, url: str) -> requests.Response:
@@ -265,23 +267,22 @@ def _request_get_with_cookie(self, project_id: str, url: str) -> requests.Respon
265267
Response
266268
267269
"""
268-
269-
def request(cookies):
270-
kwargs = {"cookies": cookies}
271-
return self.session.get(url, **kwargs)
272-
273-
if self.cookies is None:
274-
_, r = self._get_signed_cookie(project_id)
275-
self.cookies = r.cookies
276-
277-
response = request(self.cookies)
270+
# Sessionオブジェクトに保存されているCookieを利用して、URLにアクセスする
271+
response = self.session.get(url)
278272

279273
# CloudFrontから403 Errorが発生したときは、別プロジェクトのcookieを渡している可能性があるので、
280274
# Signed Cookieを発行して、再度リクエストを投げる
281275
if response.status_code == requests.codes.forbidden and response.headers.get("server") == "CloudFront":
282-
_, r = self._get_signed_cookie(project_id)
283-
self.cookies = r.cookies
284-
response = request(self.cookies)
276+
query_params = {}
277+
if "/input_data_set/" in url:
278+
query_params.update({"resource": "input_data_set"})
279+
else:
280+
query_params.update({"resource": "project"})
281+
282+
_, r = self._get_signed_cookie(project_id, query_params=query_params)
283+
for cookie in r.cookies:
284+
self.session.cookies.set_cookie(cookie)
285+
response = self.session.get(url)
285286

286287
_log_error_response(logger, response)
287288
_raise_for_status(response)

poetry.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "annofabapi"
3-
version = "0.46.4"
3+
version = "0.46.5"
44
description = "Python Clinet Library of AnnoFab WebAPI (https://annofab.com/docs/api/)"
55
authors = ["yuji38kwmt"]
66
license = "MIT"

tests/test_api.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,20 @@ def test_get_task_or_none(self):
594594

595595

596596
class TestProtectedMethod:
597-
def test__request_get_with_cookie(self):
597+
@classmethod
598+
def setup_class(cls):
599+
cls.input_data_id = test_wrapper.get_first_input_data_id_in_task(project_id, task_id)
600+
601+
def test__request_get_with_cookie_with_project_url(self):
598602
images, _ = api.get_instruction_images(project_id)
599603
url = images[0]["url"]
600604
r = api._request_get_with_cookie(project_id, url)
601-
# エラーがないことを確認する
605+
assert r.status_code == 200
606+
607+
def test__request_get_with_cookie_with_input_data_set_url(self):
608+
input_data, _ = api.get_input_data(project_id, self.input_data_id)
609+
r = api._request_get_with_cookie(project_id, input_data["url"])
610+
assert r.status_code == 200
602611

603612
def test_request_get_with_cookie_failed(self):
604613
# SignedCookieに対応するプロジェクトと、アクセス対象のプロジェクトが異なっているときの対応

0 commit comments

Comments
 (0)