Skip to content

AnnofabApiクラスにaccount_idプロパティを追加 #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README_for_developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ $ make lint

## Test

### テストの実行
1. AnnoFabの認証情報を`.netrc`に記載する。
2. `pytest.ini`にテスト対象の`project_id`を指定する。**【注意】テストを実行すると、AnnoFabプロジェクトの内容が変更される**
### テストの実行方法
1. AnnoFabの認証情報を、`.netrc`ファイルまたは環境変数に設定する。
2. `pytest.ini`に、テスト対象の`project_id`と`task_id`を指定する。
* `task_id`はプロジェクト`project_id`配下であること
* **【注意】テストを実行すると、AnnoFabプロジェクトの内容が変更される**
3. `$ make test`コマンドを実行する。

#### 直接pytestを実行する場合
#### タスクの前提条件
* タスクの先頭画像にアノテーションが1個以上付与されている
* タスクの先頭画像に検査コメントが1個以上付与されている


#### テストメソッドを指定してテストする方法

```
$ poetry run pytest tests
$ poetry run pytest tests/test_api.py::TestLogin::test_login
```

annofabapiでは、pytestのカスタムオプションを定義しています。
Expand Down
3 changes: 1 addition & 2 deletions annofabapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from annofabapi.api import AnnofabApi
from annofabapi.api2 import AnnofabApi2
from annofabapi.wrapper import Wrapper
from annofabapi.resource import build, build_from_netrc, build_from_env, build_from_netrc_or_env, Resource
from annofabapi.resource import build, build_from_netrc, build_from_env, Resource

from .__version__ import __version__

Expand All @@ -11,7 +11,6 @@
"Wrapper",
"build",
"build_from_netrc",
"build_from_netrc_or_env",
"build_from_env",
"Resource",
"__version__",
Expand Down
18 changes: 18 additions & 0 deletions annofabapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def __init__(self, login_user_id: str, login_password: str, endpoint_url: str =
#: Signed Cookie情報
cookies: Optional[RequestsCookieJar] = None

__account_id: Optional[str] = None

class __MyToken(AuthBase):
"""
requestsモジュールのauthに渡す情報。
Expand Down Expand Up @@ -413,3 +415,19 @@ def delete_labor_control(self, data_id: str) -> Tuple[Any, requests.Response]:
http_method = "DELETE"
keyword_params: Dict[str, Any] = {}
return self._request_wrapper(http_method, url_path, **keyword_params)

#########################################
# Public Method : Other
#########################################
@property
def account_id(self) -> str:
"""
AnnoFabにログインするユーザのaccount_id
"""
if self.__account_id is not None:
return self.__account_id
else:
content, _ = self.get_my_account()
account_id = content["account_id"]
self.__account_id = account_id
return account_id
55 changes: 27 additions & 28 deletions annofabapi/resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import netrc
import os
from typing import Optional
from urllib.parse import urlparse

from annofabapi import AnnofabApi, AnnofabApi2, Wrapper
Expand Down Expand Up @@ -32,10 +33,17 @@ def __init__(self, login_user_id: str, login_password: str, endpoint_url: str =
self.api2 = AnnofabApi2(self.api)


def build(login_user_id: str, login_password: str, endpoint_url: str = DEFAULT_ENDPOINT_URL) -> Resource:
def build(
login_user_id: Optional[str] = None, login_password: Optional[str] = None, endpoint_url: str = DEFAULT_ENDPOINT_URL
) -> Resource:
"""
AnnofabApi, Wrapperのインスタンスを保持するインスタンスを生成する。

``login_user_id``と``login_password`の両方がNoneの場合は、`.netrc`ファイルまたは環境変数から認証情報を取得する。
認証情報は、`.netrc`ファイル、環境変数の順に読み込む。

環境変数は``ANNOFAB_USER_ID`` , ``ANNOFAB_PASSWORD`` を参照する。

Args:
login_user_id: AnnoFabにログインするときのユーザID
login_password: AnnoFabにログインするときのパスワード
Expand All @@ -45,7 +53,24 @@ def build(login_user_id: str, login_password: str, endpoint_url: str = DEFAULT_E
AnnofabApi, Wrapperのインスタンスを保持するインスタンス

"""
return Resource(login_user_id, login_password, endpoint_url=endpoint_url)
if login_user_id is not None and login_password is not None:
return Resource(login_user_id, login_password, endpoint_url=endpoint_url)

elif login_user_id is None and login_password is None:
try:
return build_from_netrc(endpoint_url)
except AnnofabApiException:
pass

try:
return build_from_env(endpoint_url)
except AnnofabApiException:
pass

raise AnnofabApiException("`.netrc`ファイルまたは環境変数にAnnoFab認証情報はありませんでした。")

else:
raise ValueError()


def build_from_netrc(endpoint_url: str = DEFAULT_ENDPOINT_URL) -> Resource:
Expand Down Expand Up @@ -97,29 +122,3 @@ def build_from_env(endpoint_url: str = DEFAULT_ENDPOINT_URL) -> Resource:

logger.debug("環境変数からAnnoFab認証情報を読み込みました。")
return Resource(login_user_id, login_password, endpoint_url=endpoint_url)


def build_from_netrc_or_env(endpoint_url: str = DEFAULT_ENDPOINT_URL) -> Resource:
"""
`.netrc`ファイルまたは環境変数からAnnoFab認証情報を取得し、annnofabapi.Resourceインスタンスを生成します。
netrc, 環境変数の順に認証情報を読み込みます。

Args:
endpoint_url:

Returns:

"""
# '.netrc'ファイルから認証情報を取得する
try:
return build_from_netrc(endpoint_url)
except AnnofabApiException:
pass

# 環境変数から認証情報を取得する
try:
return build_from_env(endpoint_url)
except AnnofabApiException:
pass

raise AnnofabApiException("`.netrc`ファイルまたは環境変数にAnnoFab認証情報はありませんでした。")
4 changes: 2 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
addopts = --verbose --capture=no -rs

[annofab]
;endpoint_url = "https://annofab.com"

# Caution : Annofab project may be changed!!
# Specify AnnoFab project that has owner role assigned to you.
project_id = 58a2a621-7d4b-41e7-927b-cdc570c1114a

task_id = sample_423

input_data_id = c2a3223b-90db-4416-8c7c-55bf140af1cd
Binary file removed tests/data/simple-annotation-v2.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/data/simple-annotation.zip
Binary file not shown.

This file was deleted.

This file was deleted.

Loading