Skip to content

Commit 840b0d5

Browse files
authored
Wrapperクラスにdownload, execute_http_get関数を用意 (#456)
* 公開 * versionup * update wrapper
1 parent d7a803d commit 840b0d5

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

annofabapi/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.58.0"
1+
__version__ = "0.59.0"

annofabapi/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def _execute_http_request(
447447
**kwargs,
448448
)
449449

450-
# リトライすべき場合はExceptionをスローする
450+
# リトライが必要な場合は、backoffがリトライできるようにするため、Exceptionをスローする
451451
if raise_for_status or _should_retry_with_status(response.status_code):
452452
_log_error_response(logger, response)
453453
_raise_for_status(response)

annofabapi/wrapper.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ def _first_true(iterable, default=None, pred=None):
7777
return next(filter(pred, iterable), default)
7878

7979

80-
def _hour_to_millisecond(hour: Optional[float]) -> Optional[int]:
81-
return int(hour * 3600_000) if hour is not None else None
82-
83-
84-
_ORGANIZATION_ID_FOR_AVAILABILITY = "___plannedWorktime___"
85-
"""予定稼働時間用の組織ID"""
86-
8780
_JOB_CONCURRENCY_LIMIT = {
8881
ProjectJobType.COPY_PROJECT: {
8982
ProjectJobType.GEN_INPUTS,
@@ -205,16 +198,49 @@ def _get_all_objects(func_get_list: Callable, limit: int, **kwargs_for_func_get_
205198

206199
return all_objects
207200

208-
def _download(self, url: str, dest_path: Union[str, Path]) -> requests.Response:
201+
def execute_http_get(self, url: str) -> requests.Response:
202+
"""
203+
指定したURLに対してHTTP GETを実行します。
204+
205+
塗りつぶし画像など外部リソースのURLを指定することを想定しています。
206+
207+
Args:
208+
url: HTTP GETでアクセスするURL
209+
210+
Returns:
211+
URLにアクセスしたときの ``requests.Response`` 情報
212+
213+
Note:
214+
``requests.get`` でアクセスすることとの違いは以下の通りです。
215+
216+
* ``requests.Session`` 情報を使ってTCPコネクションを再利用しているため、``requests.get`` を使ってダウンロードするよりも、パフォーマンスが向上する可能性があります。
217+
* 必要に応じてリトライします
218+
* HTTPステータスコードが4XX,5XXならば、HTTPErrorがスローされます
219+
220+
221+
"""
222+
return self.api._execute_http_request(http_method="get", url=url)
223+
224+
def download(self, url: str, dest_path: Union[str, Path]) -> requests.Response:
209225
"""
210226
指定したURLからファイルをダウンロードします。
211227
228+
``getAnnotation`` などダウンロード用のURLを指定することを想定しています。
229+
230+
212231
Args:
213232
url: ダウンロード対象のURL
214233
dest_path: 保存先ファイルのパス
215234
216235
Returns:
217-
URLにアクセスしたときのResponse情報
236+
URLにアクセスしたときの ``requests.Response`` 情報
237+
238+
Note:
239+
``requests.get`` でアクセスすることとの違いは以下の通りです。
240+
241+
* ``requests.Session`` 情報を使ってTCPコネクションを再利用しているため、``requests.get`` を使ってダウンロードするよりも、パフォーマンスが向上する可能性があります。
242+
* 必要に応じてリトライします
243+
* HTTPステータスコードが4XX,5XXならば、HTTPErrorがスローされます
218244
219245
"""
220246
response = self.api._execute_http_request(http_method="get", url=url)
@@ -269,7 +295,7 @@ def download_annotation_archive(self, project_id: str, dest_path: Union[str, Pat
269295
# 2022/01時点でレスポンスのcontent-typeが"text/plain"なので、contentの型がdictにならない。したがって、Locationヘッダを参照する。
270296
_, response = self.api.get_annotation_archive(project_id)
271297
url = response.headers["Location"]
272-
response2 = self._download(url, dest_path)
298+
response2 = self.download(url, dest_path)
273299
logger.info(
274300
"SimpleアノテーションZIPファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
275301
project_id,
@@ -300,7 +326,7 @@ def download_full_annotation_archive(self, project_id: str, dest_path: Union[str
300326
# 2022/01時点でレスポンスのcontent-typeが"text/plain"なので、contentの型がdictにならない。したがって、Locationヘッダを参照する。
301327
_, response = self.api.get_archive_full_with_pro_id(project_id)
302328
url = response.headers["Location"]
303-
response2 = self._download(url, dest_path)
329+
response2 = self.download(url, dest_path)
304330
logger.info(
305331
"FullアノテーションZIPファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
306332
project_id,
@@ -1578,7 +1604,7 @@ def download_project_inputs_url(self, project_id: str, dest_path: Union[str, Pat
15781604
"""
15791605
content, _ = self.api.get_project_inputs_url(project_id)
15801606
url = content["url"]
1581-
response2 = self._download(url, dest_path)
1607+
response2 = self.download(url, dest_path)
15821608
logger.info(
15831609
"入力データ全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
15841610
project_id,
@@ -1603,7 +1629,7 @@ def download_project_tasks_url(self, project_id: str, dest_path: Union[str, Path
16031629

16041630
content, _ = self.api.get_project_tasks_url(project_id)
16051631
url = content["url"]
1606-
response2 = self._download(url, dest_path)
1632+
response2 = self.download(url, dest_path)
16071633
logger.info(
16081634
"タスク全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
16091635
project_id,
@@ -1635,7 +1661,7 @@ def download_project_inspections_url(self, project_id: str, dest_path: Union[str
16351661

16361662
content, _ = self.api.get_project_inspections_url(project_id)
16371663
url = content["url"]
1638-
response2 = self._download(url, dest_path)
1664+
response2 = self.download(url, dest_path)
16391665
logger.info(
16401666
"検査コメント全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
16411667
project_id,
@@ -1659,7 +1685,7 @@ def download_project_comments_url(self, project_id: str, dest_path: Union[str, P
16591685

16601686
content, _ = self.api.get_project_comments_url(project_id)
16611687
url = content["url"]
1662-
response = self._download(url, dest_path)
1688+
response = self.download(url, dest_path)
16631689
logger.info(
16641690
"コメント全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
16651691
project_id,
@@ -1684,7 +1710,7 @@ def download_project_task_history_events_url(self, project_id: str, dest_path: U
16841710

16851711
content, _ = self.api.get_project_task_history_events_url(project_id)
16861712
url = content["url"]
1687-
response2 = self._download(url, dest_path)
1713+
response2 = self.download(url, dest_path)
16881714
logger.info(
16891715
"タスク履歴イベント全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
16901716
project_id,
@@ -1709,7 +1735,7 @@ def download_project_task_histories_url(self, project_id: str, dest_path: Union[
17091735

17101736
content, _ = self.api.get_project_task_histories_url(project_id)
17111737
url = content["url"]
1712-
response2 = self._download(url, dest_path)
1738+
response2 = self.download(url, dest_path)
17131739
logger.info(
17141740
"タスク履歴全件ファイルをダウンロードしました。 :: project_id='%s', Last-Modified='%s', file='%s'",
17151741
project_id,

pyproject.toml

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

0 commit comments

Comments
 (0)