Skip to content

Commit 5647169

Browse files
authored
Merge pull request #116 from kurusugawa-computer/fix/endpoint-url
エンドポイント設定の不具合を修正
2 parents bc9e413 + b874d9f commit 5647169

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.25.0'
1+
__version__ = '0.25.1'

annofabapi/api.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
logger = logging.getLogger(__name__)
1515

16-
DEFAULT_ENDPOINT_URL = "https://annofab.com/api"
16+
DEFAULT_ENDPOINT_URL = "https://annofab.com"
1717
"""AnnoFab WebAPIのデフォルトのエンドポイントURL"""
1818

1919

@@ -81,7 +81,7 @@ def __init__(self, login_user_id: str, login_password: str, endpoint_url: str =
8181
self.login_user_id = login_user_id
8282
self.login_password = login_password
8383
self.endpoint_url = endpoint_url
84-
self.url_prefix = f"{endpoint_url}/v1"
84+
self.url_prefix = f"{endpoint_url}/api/v1"
8585
self.session = requests.Session()
8686

8787
#: login, refresh_tokenで取得したtoken情報
@@ -245,6 +245,8 @@ def login(self) -> Tuple[Dict[str, Any], requests.Response]:
245245

246246
url = f"{self.url_prefix}/login"
247247
response = self.session.post(url, json=login_info)
248+
249+
annofabapi.utils.log_error_response(logger, response)
248250
annofabapi.utils.raise_for_status(response)
249251

250252
json_obj = response.json()

annofabapi/api2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AnnofabApi2(AbstractAnnofabApi2):
2323
"""
2424
def __init__(self, api: AnnofabApi):
2525
self.api = api
26-
self.url_prefix = f"{api.endpoint_url}/v2"
26+
self.url_prefix = f"{api.endpoint_url}/api/v2"
2727

2828
#: Signed Cookie情報
2929
cookies: Optional[Dict[str, Any]] = None

annofabapi/utils.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import copy
77
import datetime
8+
import json
89
import logging
910
from pathlib import Path
10-
from typing import List, Optional
11+
from typing import Any, Dict, List, Optional, Union
1112

1213
import dateutil
1314
import dateutil.tz
1415
import requests
16+
from requests.structures import CaseInsensitiveDict
1517

1618
from annofabapi.models import TaskHistory, TaskPhase
1719

@@ -36,7 +38,7 @@ def raise_for_status(response: requests.Response):
3638
raise e
3739

3840

39-
def log_error_response(arg_logger: logging.Logger, response: requests.Response):
41+
def log_error_response(arg_logger: logging.Logger, response: requests.Response) -> None:
4042
"""
4143
HTTP Statusが400以上ならば、loggerにresponse/request情報を出力する
4244
@@ -45,17 +47,36 @@ def log_error_response(arg_logger: logging.Logger, response: requests.Response):
4547
response: Response
4648
4749
"""
50+
RequestBodyHeader = Union[Dict[str, Any], CaseInsensitiveDict]
51+
52+
def mask_key(d: RequestBodyHeader, key: str) -> RequestBodyHeader:
53+
if key in d:
54+
d[key] = "***"
55+
return d
56+
57+
def mask_password(d: RequestBodyHeader) -> RequestBodyHeader:
58+
d = mask_key(d, "password")
59+
d = mask_key(d, "old_password")
60+
d = mask_key(d, "new_password")
61+
return d
4862

4963
if 400 <= response.status_code < 600:
5064
headers = copy.deepcopy(response.request.headers)
51-
if "Authorization" in headers:
52-
# logにAuthorizationを出力しないようにマスクする
53-
headers["Authorization"] = "***"
5465

5566
arg_logger.debug(f"status_code = %s, response.text = %s", response.status_code, response.text)
5667
arg_logger.debug(f"request.url = %s %s", response.request.method, response.request.url)
68+
69+
# logにAuthorizationを出力しないようにマスクする
70+
mask_key(headers, "Authorization")
5771
arg_logger.debug("request.headers = %s", headers)
58-
arg_logger.debug("request.body = %s", response.request.body)
72+
73+
# request_bodyのpassword関係をマスクして、logに出力する
74+
if response.request.body is None or response.request.body == "":
75+
dict_request_body = {}
76+
else:
77+
dict_request_body = json.loads(response.request.body)
78+
79+
arg_logger.debug("request.body = %s", mask_password(dict_request_body))
5980

6081

6182
def download(url: str, dest_path: str):

tests/test_local_resource.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ def test_build_from_env(self):
3434

3535
def test_build_with_endpoint(self):
3636
resource = annofabapi.build("test_user", "password", "https://localhost:8080")
37-
assert resource.api.url_prefix == "https://localhost:8080/v1"
38-
assert resource.api2.url_prefix == "https://localhost:8080/v2"
37+
assert resource.api.url_prefix == "https://localhost:8080/api/v1"
38+
assert resource.api2.url_prefix == "https://localhost:8080/api/v2"

0 commit comments

Comments
 (0)