Skip to content

Commit 710d276

Browse files
author
Val Brodsky
committed
Use prepped request
1 parent 1d6a8d9 commit 710d276

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ def _init_connection(self) -> requests.Session:
128128

129129
def _default_headers(self):
130130
return {
131-
'Accept': 'application/json',
132-
'Content-Type': 'application/json',
133131
'Authorization': 'Bearer %s' % self.api_key,
134132
'X-User-Agent': f"python-sdk {SDK_VERSION}",
135133
'X-Python-Version': f"{python_version_info()}",
@@ -202,13 +200,21 @@ def convert_value(value):
202200
"/graphql", "/_gql")
203201

204202
try:
205-
request = {'url': endpoint, 'data': data, 'timeout': timeout}
206-
if files:
207-
request.update({'files': files})
208-
request['headers'] = {
209-
'Authorization': self._connection.headers['Authorization']
210-
}
211-
response = self._connection.post(**request)
203+
request = requests.Request('POST',
204+
endpoint,
205+
headers=self._connection.headers,
206+
data=data,
207+
files=files if files else None)
208+
209+
prepped: requests.PreparedRequest = request.prepare()
210+
211+
if not files:
212+
prepped.headers.update({
213+
'Accept': 'application/json',
214+
'Content-Type': 'application/json',
215+
})
216+
217+
response = self._connection.send(prepped, timeout=timeout)
212218
logger.debug("Response: %s", response.text)
213219
except requests.exceptions.Timeout as e:
214220
raise labelbox.exceptions.TimeoutError(str(e))
@@ -415,7 +421,6 @@ def upload_data(self,
415421

416422
response = self._connection.post(
417423
self.endpoint,
418-
headers={"authorization": "Bearer %s" % self.api_key},
419424
data=request_data,
420425
files={
421426
"1": (filename, content, content_type) if

libs/labelbox/tests/integration/test_project.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def test_attach_instructions(client, project):
144144
assert str(
145145
execinfo.value
146146
) == "Cannot attach instructions to a project that has not been set up."
147-
148147
editor = list(
149148
client.get_labeling_frontends(
150149
where=LabelingFrontend.name == "editor"))[0]
@@ -218,7 +217,7 @@ def test_create_batch_with_global_keys_sync(project: Project, data_rows):
218217
global_keys = [dr.global_key for dr in data_rows]
219218
batch_name = f'batch {uuid.uuid4()}'
220219
batch = project.create_batch(batch_name, global_keys=global_keys)
221-
220+
222221
assert batch.size == len(set(data_rows))
223222

224223

@@ -227,7 +226,7 @@ def test_create_batch_with_global_keys_async(project: Project, data_rows):
227226
global_keys = [dr.global_key for dr in data_rows]
228227
batch_name = f'batch {uuid.uuid4()}'
229228
batch = project._create_batch_async(batch_name, global_keys=global_keys)
230-
229+
231230
assert batch.size == len(set(data_rows))
232231

233232

@@ -245,8 +244,7 @@ def test_media_type(client, project: Project, rand_gen):
245244
# Exclude LLM media types for now, as they are not supported
246245
if MediaType[media_type] in [
247246
MediaType.LLMPromptCreation,
248-
MediaType.LLMPromptResponseCreation,
249-
MediaType.LLM
247+
MediaType.LLMPromptResponseCreation, MediaType.LLM
250248
]:
251249
continue
252250

@@ -284,7 +282,8 @@ def test_label_count(client, configured_batch_project_with_label):
284282

285283
def test_clone(client, project, rand_gen):
286284
# cannot clone unknown project media type
287-
project = client.create_project(name=rand_gen(str), media_type=MediaType.Image)
285+
project = client.create_project(name=rand_gen(str),
286+
media_type=MediaType.Image)
288287
cloned_project = project.clone()
289288

290289
assert cloned_project.description == project.description
@@ -295,4 +294,4 @@ def test_clone(client, project, rand_gen):
295294
assert cloned_project.get_label_count() == 0
296295

297296
project.delete()
298-
cloned_project.delete()
297+
cloned_project.delete()

0 commit comments

Comments
 (0)