Skip to content

Commit 930f0d0

Browse files
authored
Merge pull request #465 from superannotateai/1206_client
fix init flow
2 parents ab9a894 + 5cee5b5 commit 930f0d0

File tree

8 files changed

+96
-22
lines changed

8 files changed

+96
-22
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ repos:
2121
name: Style Guide Enforcement (flake8)
2222
args:
2323
- '--max-line-length=120'
24-
- --ignore=D100,D203,D405,W503,E203,E501,F841,E126,E712,E123,E131,F821,E121
24+
- --ignore=D100,D203,D405,W503,E203,E501,F841,E126,E712,E123,E131,F821,E121,W605
2525
- repo: 'https://github.com/asottile/pyupgrade'
2626
rev: v2.4.3
2727
hooks:

src/superannotate/lib/app/interface/base_interface.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, token: str = None, config_path: str = None):
2626
version = os.environ.get("SA_VERSION", "v1")
2727
_token, _config_path = None, None
2828
_host = os.environ.get("SA_URL", constants.BACKEND_URL)
29-
_ssl_verify = bool(os.environ.get("SA_SSL", True))
29+
_ssl_verify = os.environ.get("SA_SSL", "True") in ("false", "f", "0")
3030
if token:
3131
_token = Controller.validate_token(token=token)
3232
elif config_path:
@@ -37,6 +37,9 @@ def __init__(self, token: str = None, config_path: str = None):
3737
_token, _host, _ssl_verify = self._retrieve_configs(
3838
constants.CONFIG_PATH
3939
)
40+
_host = _host if _host else constants.BACKEND_URL
41+
_ssl_verify = True if _ssl_verify is None else _ssl_verify
42+
4043
self._token, self._host = _token, _host
4144
self.controller = Controller(_token, _host, _ssl_verify, version)
4245
BaseInterfaceFacade.REGISTRY.append(self)

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,9 +2237,7 @@ def validate_annotations(
22372237
"""
22382238
with open(annotations_json) as file:
22392239
annotation_data = json.loads(file.read())
2240-
response = Controller.validate_annotations(
2241-
project_type, annotation_data
2242-
)
2240+
response = Controller.validate_annotations(project_type, annotation_data)
22432241
if response.errors:
22442242
raise AppException(response.errors)
22452243
is_valid, _ = response.data
@@ -2710,7 +2708,7 @@ def download_annotations(
27102708
:param path: local directory path where the annotations will be downloaded. If none, the current directory is used.
27112709
:type path: Path-like (str or Path)
27122710
2713-
:param items: list of item names whose annotations will be downloaded
2711+
:param items: list of item names whose annotations will be downloaded
27142712
(e.g., ["Image_1.jpeg", "Image_2.jpeg"]). If the value is None, then all the annotations of the given directory will be downloaded.
27152713
27162714
:type items: list of str

src/superannotate/lib/core/usecases/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def validate_arguments(self):
155155
raise AppException(
156156
"The query and subset params cannot have the value None at the same time."
157157
)
158-
if self._subset and not self._folder.is_root:
158+
if self._subset and not self._folder.is_root:
159159
raise AppException(
160160
"The folder name should be specified in the query string."
161161
)

src/superannotate/lib/core/video_convertor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import math
33
from collections import defaultdict
44
from typing import Any
5-
from typing import Dict
65
from typing import List
76
from typing import Optional
87

98
from lib.core.enums import AnnotationTypes
10-
from lib.core.exceptions import AppException
119
from pydantic import BaseModel
1210

1311

src/superannotate/lib/infrastructure/controller.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def __init__(self, token: str, host: str, ssl_verify: bool, version: str):
7575
self._integrations = None
7676
self._ml_models = None
7777
self._user_id = None
78-
self._team_name = None
7978
self._reporter = None
79+
self._team = self._get_team()
8080

8181
@staticmethod
8282
def validate_token(token: str):
@@ -96,12 +96,6 @@ def user_id(self):
9696
self._user_id, _ = self.get_team()
9797
return self._user_id
9898

99-
@property
100-
def team_name(self):
101-
if not self._team_name:
102-
_, self._team_name = self.get_team()
103-
return self._team_name
104-
10599
@property
106100
def projects(self):
107101
if not self._projects:
@@ -114,6 +108,18 @@ def folders(self):
114108
self._folders = FolderRepository(self._backend_client)
115109
return self._folders
116110

111+
@property
112+
def team(self):
113+
return self._team
114+
115+
def _get_team(self):
116+
response = usecases.GetTeamUseCase(
117+
teams=self.teams, team_id=self.team_id
118+
).execute()
119+
if response.errors:
120+
raise AppException(response.errors)
121+
return response.data
122+
117123
def get_team(self):
118124
return usecases.GetTeamUseCase(teams=self.teams, team_id=self.team_id).execute()
119125

@@ -130,7 +136,7 @@ def teams(self):
130136
@property
131137
def team_data(self):
132138
if not self._team_data:
133-
self._team_data = self.get_team().data
139+
self._team_data = self.team
134140
return self._team_data
135141

136142
@property
@@ -1366,13 +1372,12 @@ def validate_annotations(
13661372
return use_case.execute()
13671373

13681374
def add_contributors_to_project(self, project_name: str, emails: list, role: str):
1369-
team = self.get_team()
13701375
project = self.get_project_metadata(
13711376
project_name=project_name, include_contributors=True
13721377
)
13731378
use_case = usecases.AddContributorsToProject(
13741379
reporter=self.get_default_reporter(),
1375-
team=team.data,
1380+
team=self.team,
13761381
project=project.data["project"],
13771382
emails=emails,
13781383
role=role,
@@ -1381,10 +1386,9 @@ def add_contributors_to_project(self, project_name: str, emails: list, role: str
13811386
return use_case.execute()
13821387

13831388
def invite_contributors_to_team(self, emails: list, set_admin: bool):
1384-
team = self.get_team()
13851389
use_case = usecases.InviteContributorsToTeam(
13861390
reporter=self.get_default_reporter(),
1387-
team=team.data,
1391+
team=self.team,
13881392
emails=emails,
13891393
set_admin=set_admin,
13901394
service=self.backend_client,

src/superannotate/lib/infrastructure/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(
5656
auth_token: str,
5757
logger,
5858
paginate_by=None,
59-
verify_ssl=False,
59+
verify_ssl: bool = False,
6060
testing: bool = False,
6161
):
6262
self.api_url = api_url
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import json
2+
import os
3+
import tempfile
4+
from unittest import TestCase
5+
6+
import superannotate.lib.core as constants
7+
from superannotate import SAClient
8+
9+
10+
class TestClientInit(TestCase):
11+
TEST_TOKEN = "test=6085"
12+
TEST_URL = "https://test.com"
13+
14+
def setUp(self) -> None:
15+
os.environ.pop('SA_TOKEN', None)
16+
os.environ.pop('SA_URL', None)
17+
os.environ.pop('SA_SSL', None)
18+
19+
def test_via_token(self):
20+
sa = SAClient(token=self.TEST_TOKEN)
21+
assert sa.controller._token == self.TEST_TOKEN
22+
assert sa.controller._backend_client.api_url == constants.BACKEND_URL
23+
24+
def test_via_env_token(self):
25+
os.environ.update(
26+
{"SA_TOKEN": self.TEST_TOKEN}
27+
)
28+
sa = SAClient()
29+
assert sa.controller._token == self.TEST_TOKEN
30+
assert sa.controller._backend_client.api_url == constants.BACKEND_URL
31+
32+
def test_via_env_vars(self):
33+
os.environ.update(
34+
{
35+
"SA_TOKEN": self.TEST_TOKEN,
36+
"SA_URL": self.TEST_URL,
37+
"SA_SSL": "False"
38+
}
39+
)
40+
sa = SAClient()
41+
assert sa.controller._token == self.TEST_TOKEN
42+
assert sa.controller._backend_client.api_url == self.TEST_URL
43+
assert sa.controller._backend_client._verify_ssl == False
44+
45+
def test_via_config_path_with_url_token(self):
46+
data = {
47+
"token": self.TEST_TOKEN,
48+
"main_endpoint": self.TEST_URL,
49+
"ssl_verify": True
50+
}
51+
with tempfile.TemporaryDirectory() as temp_dir:
52+
file_path = f"{temp_dir}/config.json"
53+
with open(file_path, "w") as file:
54+
json.dump(data, file)
55+
sa = SAClient(config_path=file_path)
56+
assert sa.controller._token == self.TEST_TOKEN
57+
assert sa.controller._backend_client.api_url == self.TEST_URL
58+
assert sa.controller._backend_client._verify_ssl == True
59+
60+
def test_via_config_path_with_token(self):
61+
data = {
62+
"token": self.TEST_TOKEN,
63+
}
64+
with tempfile.TemporaryDirectory() as temp_dir:
65+
file_path = f"{temp_dir}/config.json"
66+
with open(file_path, "w") as file:
67+
json.dump(data, file)
68+
sa = SAClient(config_path=file_path)
69+
assert sa.controller._token == self.TEST_TOKEN
70+
assert sa.controller._backend_client.api_url == constants.BACKEND_URL
71+
assert sa.controller._backend_client._verify_ssl == True

0 commit comments

Comments
 (0)