Skip to content

Commit be668ba

Browse files
committed
Fixed http client
1 parent 9344770 commit be668ba

File tree

2 files changed

+59
-35
lines changed

2 files changed

+59
-35
lines changed

src/superannotate/lib/infrastructure/services/http_client.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,34 @@ class AIOHttpSession(aiohttp.ClientSession):
229229
RETRY_LIMIT = 3
230230
BACKOFF_FACTOR = 0.3
231231

232-
class AIOHttpSession(aiohttp.ClientSession):
233-
RETRY_STATUS_CODES = [401, 403, 502, 503, 504]
234-
RETRY_LIMIT = 3
235-
BACKOFF_FACTOR = 0.3
236-
237-
@staticmethod
238-
def _copy_form_data(data: aiohttp.FormData) -> aiohttp.FormData:
239-
form_data = aiohttp.FormData(quote_fields=False)
240-
for field in data._fields: # noqa
241-
if isinstance(field[2], io.IOBase):
242-
field[2].seek(0)
243-
form_data.add_field(
244-
value=field[2],
245-
content_type=field[1].get("Content-Type", ""),
246-
**field[0],
247-
)
248-
return form_data
232+
@staticmethod
233+
def _copy_form_data(data: aiohttp.FormData) -> aiohttp.FormData:
234+
form_data = aiohttp.FormData(quote_fields=False)
235+
for field in data._fields: # noqa
236+
if isinstance(field[2], io.IOBase):
237+
field[2].seek(0)
238+
form_data.add_field(
239+
value=field[2],
240+
content_type=field[1].get("Content-Type", ""),
241+
**field[0],
242+
)
243+
return form_data
249244

250-
async def request(self, *args, **kwargs) -> aiohttp.ClientResponse:
251-
attempts = self.RETRY_LIMIT
252-
delay = 0
253-
for _ in range(attempts):
254-
delay += self.BACKOFF_FACTOR
255-
try:
256-
response = await super()._request(*args, **kwargs)
257-
if attempts <= 1 or response.status not in self.RETRY_STATUS_CODES:
258-
return response
259-
except (aiohttp.ClientError, RuntimeError) as e:
260-
if attempts <= 1:
261-
raise
262-
if isinstance(e, RuntimeError):
263-
data = kwargs["data"]
264-
if isinstance(data, aiohttp.FormData):
265-
kwargs["data"] = self._copy_form_data(data)
266-
attempts -= 1
267-
await asyncio.sleep(delay)
245+
async def request(self, *args, **kwargs) -> aiohttp.ClientResponse:
246+
attempts = self.RETRY_LIMIT
247+
delay = 0
248+
for _ in range(attempts):
249+
delay += self.BACKOFF_FACTOR
250+
try:
251+
response = await super()._request(*args, **kwargs)
252+
if attempts <= 1 or response.status not in self.RETRY_STATUS_CODES:
253+
return response
254+
except (aiohttp.ClientError, RuntimeError) as e:
255+
if attempts <= 1:
256+
raise
257+
if isinstance(e, RuntimeError):
258+
data = kwargs["data"]
259+
if isinstance(data, aiohttp.FormData):
260+
kwargs["data"] = self._copy_form_data(data)
261+
attempts -= 1
262+
await asyncio.sleep(delay)

tests/integration/projects/test_basic_project.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,40 @@ class TestGenAIProjectBasic(BaseTestCase):
1515
PROJECT_NAME = "TestGenAICreate"
1616
PROJECT_TYPE = "GenAI"
1717
PROJECT_DESCRIPTION = "DESCRIPTION"
18+
ANNOTATION_PATH = (
19+
"data_set/sample_project_vector/example_image_1.jpg___objects.json"
20+
)
21+
22+
@property
23+
def annotation_path(self):
24+
return os.path.join(Path(__file__).parent.parent.parent, self.ANNOTATION_PATH)
1825

1926
def test_search(self):
2027
projects = sa.search_projects(self.PROJECT_NAME, return_metadata=True)
2128
assert projects
2229

30+
sa.create_annotation_class(
31+
self.PROJECT_NAME,
32+
"class1",
33+
"#FFAAFF",
34+
[
35+
{
36+
"name": "Human",
37+
"attributes": [{"name": "yes"}, {"name": "no"}],
38+
},
39+
{
40+
"name": "age",
41+
"attributes": [{"name": "young"}, {"name": "old"}],
42+
},
43+
],
44+
)
45+
sa.attach_items(self.PROJECT_NAME, attachments=[{"url": "", "name": "name"}])
46+
annotation = json.load(open(self.annotation_path))
47+
annotation["metadata"]["name"] = "name"
48+
sa.upload_annotations(self.PROJECT_NAME, annotations=[annotation])
49+
data = sa.get_annotations(self.PROJECT_NAME)
50+
assert data
51+
2352

2453
class TestProjectBasic(BaseTestCase):
2554
PROJECT_NAME = "TestWorkflowGet"

0 commit comments

Comments
 (0)