Skip to content

Commit 1e5bf48

Browse files
committed
test codeの生成
1 parent d9bd10c commit 1e5bf48

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

annofabapi/api2.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class AnnofabApi2(AbstractAnnofabApi2):
1818
"""
1919
Web API v2に対応したメソッドが存在するクラス。
20+
【注意】 開発途上版のため、互換性のない変更がある可能性があります。
2021
"""
2122

2223
def __init__(self, api: AnnofabApi):
@@ -31,20 +32,12 @@ def __init__(self, api: AnnofabApi):
3132
#: アクセスするURL
3233
URL_PREFIX = "https://annofab.com/api/v2"
3334

35+
#: Signed Cookie情報
3436
cookies: Optional[Dict[str, Any]] = None
3537

3638
#########################################
3739
# Private Method
3840
#########################################
39-
class __NoAuth(AuthBase):
40-
"""
41-
netrcの有無にかかわらず、authorizationヘッダを空にする
42-
http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
43-
"""
44-
45-
def __call__(self, req):
46-
req.headers['Authorization'] = None
47-
return req
4841

4942
@annofabapi.api.my_backoff
5043
def _request_wrapper(self,
@@ -86,7 +79,6 @@ def _request_wrapper(self,
8679

8780
else:
8881
kwargs.update({"cookies": self.cookies})
89-
kwargs.pop("auth", self.__NoAuth())
9082

9183
# HTTP Requestを投げる
9284
response: requests.Response = getattr(self.api.session,
@@ -96,7 +88,8 @@ def _request_wrapper(self,
9688
# CloudFrontから403 Errorが発生したとき
9789
if response.status_code == requests.codes.forbidden and response.headers.get(
9890
"server") == "CloudFront":
99-
self.api.login()
91+
92+
self._get_signed_access_v2(url_path)
10093
return self._request_wrapper(http_method, url_path,
10194
query_params, header_params,
10295
request_body)
@@ -109,11 +102,24 @@ def _request_wrapper(self,
109102
content = self.api._response_to_content(response)
110103
return content, response
111104

105+
def _get_signed_access_v2(self, url_path: str):
106+
query_params = {"url": f"/api/v2{url_path}"}
107+
self.get_signed_access_v2(query_params)
108+
112109
#########################################
113110
# Public Method : Cache
114111
#########################################
115-
def get_signed_access_v2(self, query_params):
112+
def get_signed_access_v2(self, query_params: Dict[str, Any]) -> Tuple[Dict[str, Any], requests.Response]:
116113
"""
114+
Signed Cookieを取得して、インスタンスに保持する。
115+
116+
Args:
117+
query_params (Dict[str, Any]): Query Parameters
118+
url (str): アクセスするページのURL
119+
120+
Returns:
121+
Tuple[SignedCookie, requests.Response]
122+
117123
"""
118124

119125
url_path = f'/sign-url'

tests/test_api2.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
AnnofabApi2のテストメソッド
3+
4+
"""
5+
import configparser
6+
import datetime
7+
import logging
8+
import os
9+
import time
10+
import uuid
11+
from distutils.util import strtobool
12+
13+
import annofabapi
14+
import annofabapi.utils
15+
from tests.utils_for_test import WrapperForTest, create_csv_for_task
16+
17+
# プロジェクトトップに移動する
18+
os.chdir(os.path.dirname(os.path.abspath(__file__)) + "/../")
19+
inifile = configparser.ConfigParser()
20+
inifile.read('./pytest.ini', 'UTF-8')
21+
project_id = inifile.get('annofab', 'project_id')
22+
should_execute_job_api: bool = strtobool(
23+
inifile.get('annofab', 'should_execute_job_api'))
24+
should_print_log_message: bool = strtobool(
25+
inifile.get('annofab', 'should_print_log_message'))
26+
27+
test_dir = './tests/data'
28+
out_dir = './tests/out'
29+
30+
if should_print_log_message:
31+
logging_formatter = '%(levelname)s : %(asctime)s : %(name)s : %(funcName)s : %(message)s'
32+
logging.basicConfig(format=logging_formatter)
33+
logging.getLogger("annofabapi").setLevel(level=logging.DEBUG)
34+
35+
service = annofabapi.build_from_netrc()
36+
test_wrapper = WrapperForTest(service.api)
37+
38+
my_account_id = service.api.get_my_account()[0]['account_id']
39+
organization_name = service.api.get_organization_of_project(
40+
project_id)[0]['organization_name']
41+
42+
annofab_user_id = service.api.login_user_id
43+
44+
45+
def test_project():
46+
assert type(service.api2.get_project_cache_v2(project_id))
47+

0 commit comments

Comments
 (0)