Skip to content

Commit a1c5439

Browse files
dshabinVaghinak Basentsyan
authored andcommitted
Fix session
1 parent a2747df commit a1c5439

File tree

4 files changed

+78
-64
lines changed

4 files changed

+78
-64
lines changed

src/superannotate/lib/core/usecases.py

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,14 +2485,21 @@ def __init__(
24852485
self._annotation_status_code = annotation_status_code
24862486
self._image_quality_in_editor = image_quality_in_editor
24872487
self._limit = limit
2488+
self._auth_data = None
24882489

2489-
@property
2490-
def upload_auth_data(self):
2491-
return self._backend_service.get_s3_upload_auth_token(
2492-
project_id=self._project.uuid,
2490+
def validate_auth_data(self):
2491+
response = self._backend_service.get_s3_upload_auth_token(
24932492
team_id=self._project.team_id,
24942493
folder_id=self._folder.uuid,
2494+
project_id=self._project.uuid,
24952495
)
2496+
if "error" in response:
2497+
raise AppException(response.get("error"))
2498+
self._auth_data = response
2499+
2500+
@property
2501+
def upload_auth_data(self):
2502+
return self._auth_data
24962503

24972504
@property
24982505
def limit(self):
@@ -2501,15 +2508,16 @@ def limit(self):
25012508
return self._limit
25022509

25032510
def execute(self):
2504-
extracted_paths = VideoPlugin.extract_frames(
2505-
video_path=self._video_path,
2506-
start_time=self._start_time,
2507-
end_time=self._end_time,
2508-
extract_path=self._extract_path,
2509-
limit=self.limit,
2510-
target_fps=self._target_fps,
2511-
)
2512-
self._response.data = extracted_paths
2511+
if self.is_valid():
2512+
extracted_paths = VideoPlugin.extract_frames(
2513+
video_path=self._video_path,
2514+
start_time=self._start_time,
2515+
end_time=self._end_time,
2516+
extract_path=self._extract_path,
2517+
limit=self.limit,
2518+
target_fps=self._target_fps,
2519+
)
2520+
self._response.data = extracted_paths
25132521
return self._response
25142522

25152523

@@ -4172,14 +4180,18 @@ def validate_project_type(self):
41724180
"The function does not support projects containing videos attached with URLs"
41734181
)
41744182

4183+
def validate_auth_data(self):
4184+
response = self._backend_client.get_s3_upload_auth_token(
4185+
team_id=self._project.team_id,
4186+
folder_id=self._folder.uuid,
4187+
project_id=self._project.uuid,
4188+
)
4189+
if "error" in response:
4190+
raise AppException(response.get("error"))
4191+
self._auth_data = response
4192+
41754193
@property
41764194
def auth_data(self):
4177-
if not self._auth_data:
4178-
self._auth_data = self._backend_client.get_s3_upload_auth_token(
4179-
team_id=self._project.team_id,
4180-
folder_id=self._folder.uuid,
4181-
project_id=self._project.uuid,
4182-
)
41834195
return self._auth_data
41844196

41854197
@property
@@ -4308,39 +4320,41 @@ def images_to_upload(self):
43084320
return self._images_to_upload
43094321

43104322
def execute(self):
4311-
images_to_upload, duplications = self.images_to_upload
4312-
images_to_upload = images_to_upload[: self.auth_data["availableImageCount"]]
4313-
uploaded_images = []
4314-
failed_images = []
4315-
with concurrent.futures.ThreadPoolExecutor(
4316-
max_workers=self.MAX_WORKERS
4317-
) as executor:
4318-
results = [
4319-
executor.submit(self._upload_image, image_path)
4320-
for image_path in images_to_upload
4321-
]
4322-
for future in concurrent.futures.as_completed(results):
4323-
processed_image = future.result()
4324-
if processed_image.uploaded and processed_image.entity:
4325-
uploaded_images.append(processed_image)
4326-
else:
4327-
failed_images.append(processed_image.path)
4328-
yield
4329-
4330-
uploaded = []
4331-
for i in range(0, len(uploaded_images), 100):
4332-
response = AttachFileUrlsUseCase(
4333-
project=self._project,
4334-
folder=self._folder,
4335-
limit=self.auth_data["availableImageCount"],
4336-
backend_service_provider=self._backend_client,
4337-
attachments=[image.entity for image in uploaded_images[i : i + 100]],
4338-
annotation_status=self._annotation_status,
4339-
).execute()
4340-
4341-
attachments, duplications = response.data
4342-
uploaded.extend(attachments)
4343-
uploaded = [image["name"] for image in uploaded]
4344-
failed_images = [image.split("/")[-1] for image in failed_images]
4345-
4346-
self._response.data = uploaded, failed_images, duplications
4323+
if self.is_valid():
4324+
images_to_upload, duplications = self.images_to_upload
4325+
images_to_upload = images_to_upload[: self.auth_data["availableImageCount"]]
4326+
uploaded_images = []
4327+
failed_images = []
4328+
with concurrent.futures.ThreadPoolExecutor(
4329+
max_workers=self.MAX_WORKERS
4330+
) as executor:
4331+
results = [
4332+
executor.submit(self._upload_image, image_path)
4333+
for image_path in images_to_upload
4334+
]
4335+
for future in concurrent.futures.as_completed(results):
4336+
processed_image = future.result()
4337+
if processed_image.uploaded and processed_image.entity:
4338+
uploaded_images.append(processed_image)
4339+
else:
4340+
failed_images.append(processed_image.path)
4341+
yield
4342+
4343+
uploaded = []
4344+
for i in range(0, len(uploaded_images), 100):
4345+
response = AttachFileUrlsUseCase(
4346+
project=self._project,
4347+
folder=self._folder,
4348+
limit=self.auth_data["availableImageCount"],
4349+
backend_service_provider=self._backend_client,
4350+
attachments=[image.entity for image in uploaded_images[i : i + 100]],
4351+
annotation_status=self._annotation_status,
4352+
).execute()
4353+
4354+
attachments, duplications = response.data
4355+
uploaded.extend(attachments)
4356+
uploaded = [image["name"] for image in uploaded]
4357+
failed_images = [image.split("/")[-1] for image in failed_images]
4358+
4359+
self._response.data = uploaded, failed_images, duplications
4360+
return self._response

src/superannotate/lib/infrastructure/controller.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ def team_id(self) -> int:
107107

108108
@timed_lru_cache(seconds=3600)
109109
def get_auth_data(self, project_id: int, team_id: int, folder_id: int):
110-
return self._backend_client.get_s3_upload_auth_token(
110+
response = self._backend_client.get_s3_upload_auth_token(
111111
team_id, folder_id, project_id
112112
)
113+
if "error" in response:
114+
raise AppException(response.get("error"))
115+
return response
113116

114117
def get_s3_repository(self, team_id: int, project_id: int, folder_id: int):
115118
auth_data = self.get_auth_data(project_id, team_id, folder_id)

src/superannotate/lib/infrastructure/services.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ def __init__(self, api_url: str, auth_token: str, logger, paginate_by=None):
3131
self.logger = logger
3232
self._paginate_by = paginate_by
3333
self.team_id = auth_token.split("=")[-1]
34-
self._session = None
3534

3635
@timed_lru_cache(seconds=360)
3736
def get_session(self):
38-
if not self._session:
39-
self._session = requests.Session()
40-
self._session.headers.update(self.default_headers)
41-
return self._session
37+
session = requests.Session()
38+
session.headers.update(self.default_headers)
39+
return session
4240

4341
@property
4442
def default_headers(self):
@@ -82,8 +80,8 @@ def _request(
8280
session.headers.update(headers if headers else {})
8381
with self.safe_api():
8482
req = requests.Request(method=method, url=url, **kwargs, params=params)
85-
prepared = self._session.prepare_request(req)
86-
response = self._session.send(request=prepared)
83+
prepared = session.prepare_request(req)
84+
response = session.send(request=prepared)
8785
if response.status_code == 404 and retried < 3:
8886
return self._request(
8987
url,

tests/integration/test_interface.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from tests.integration.base import BaseTestCase
88

99

10-
1110
class TestInterface(BaseTestCase):
1211
PROJECT_NAME = "Interface test"
1312
TEST_FOLDER_PATH = "data_set/sample_project_vector"

0 commit comments

Comments
 (0)