Skip to content

Commit d9fc976

Browse files
author
Val Brodsky
committed
Fix default header handling
1 parent 710d276 commit d9fc976

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def _init_connection(self) -> requests.Session:
129129
def _default_headers(self):
130130
return {
131131
'Authorization': 'Bearer %s' % self.api_key,
132+
'Accept': 'application/json',
133+
'Content-Type': 'application/json',
132134
'X-User-Agent': f"python-sdk {SDK_VERSION}",
133135
'X-Python-Version': f"{python_version_info()}",
134136
}
@@ -200,20 +202,18 @@ def convert_value(value):
200202
"/graphql", "/_gql")
201203

202204
try:
205+
headers = self._connection.headers.copy()
206+
if files:
207+
del headers['Content-Type']
208+
del headers['Accept']
203209
request = requests.Request('POST',
204210
endpoint,
205-
headers=self._connection.headers,
211+
headers=headers,
206212
data=data,
207213
files=files if files else None)
208214

209215
prepped: requests.PreparedRequest = request.prepare()
210216

211-
if not files:
212-
prepped.headers.update({
213-
'Accept': 'application/json',
214-
'Content-Type': 'application/json',
215-
})
216-
217217
response = self._connection.send(prepped, timeout=timeout)
218218
logger.debug("Response: %s", response.text)
219219
except requests.exceptions.Timeout as e:
@@ -419,13 +419,21 @@ def upload_data(self,
419419
"map": (None, json.dumps({"1": ["variables.file"]})),
420420
}
421421

422-
response = self._connection.post(
423-
self.endpoint,
424-
data=request_data,
425-
files={
426-
"1": (filename, content, content_type) if
427-
(filename and content_type) else content
428-
})
422+
files = {
423+
"1": (filename, content, content_type) if
424+
(filename and content_type) else content
425+
}
426+
headers = self._connection.headers.copy()
427+
headers.pop("Content-Type", None)
428+
request = requests.Request('POST',
429+
self.endpoint,
430+
headers=headers,
431+
data=request_data,
432+
files=files)
433+
434+
prepped: requests.PreparedRequest = request.prepare()
435+
436+
response = self._connection.send(prepped)
429437

430438
if response.status_code == 502:
431439
error_502 = '502 Bad Gateway'
@@ -1094,6 +1102,7 @@ def get_feature_schema(self, feature_schema_id):
10941102
query_str = """query rootSchemaNodePyApi($rootSchemaNodeWhere: RootSchemaNodeWhere!){
10951103
rootSchemaNode(where: $rootSchemaNodeWhere){%s}
10961104
}""" % query.results_query_part(Entity.FeatureSchema)
1105+
10971106
res = self.execute(
10981107
query_str,
10991108
{'rootSchemaNodeWhere': {
@@ -1204,10 +1213,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
12041213

12051214
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
12061215
feature_schema_id)
1207-
response = self._connection.delete(
1208-
endpoint,
1209-
headers=self.headers,
1210-
)
1216+
response = self._connection.delete(endpoint,)
12111217

12121218
if response.status_code != requests.codes.no_content:
12131219
raise labelbox.exceptions.LabelboxError(
@@ -1224,10 +1230,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
12241230
"""
12251231
endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
12261232
ontology_id)
1227-
response = self._connection.delete(
1228-
endpoint,
1229-
headers=self.headers,
1230-
)
1233+
response = self._connection.delete(endpoint,)
12311234

12321235
if response.status_code != requests.codes.no_content:
12331236
raise labelbox.exceptions.LabelboxError(
@@ -1251,7 +1254,6 @@ def update_feature_schema_title(self, feature_schema_id: str,
12511254
feature_schema_id) + '/definition'
12521255
response = self._connection.patch(
12531256
endpoint,
1254-
headers=self.headers,
12551257
json={"title": title},
12561258
)
12571259

@@ -1284,7 +1286,6 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
12841286
feature_schema_id)
12851287
response = self._connection.put(
12861288
endpoint,
1287-
headers=self.headers,
12881289
json={"normalized": json.dumps(feature_schema)},
12891290
)
12901291

@@ -1314,7 +1315,6 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
13141315
feature_schema_id)
13151316
response = self._connection.post(
13161317
endpoint,
1317-
headers=self.headers,
13181318
json={"position": position},
13191319
)
13201320
if response.status_code != requests.codes.created:
@@ -1339,7 +1339,6 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
13391339
endpoint = self.rest_endpoint + "/ontologies/unused"
13401340
response = self._connection.get(
13411341
endpoint,
1342-
headers=self.headers,
13431342
json={"after": after},
13441343
)
13451344

@@ -1367,7 +1366,6 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
13671366
endpoint = self.rest_endpoint + "/feature-schemas/unused"
13681367
response = self._connection.get(
13691368
endpoint,
1370-
headers=self.headers,
13711369
json={"after": after},
13721370
)
13731371

@@ -1890,10 +1888,7 @@ def is_feature_schema_archived(self, ontology_id: str,
18901888

18911889
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
18921890
ontology_id)
1893-
response = self._connection.get(
1894-
ontology_endpoint,
1895-
headers=self.headers,
1896-
)
1891+
response = self._connection.get(ontology_endpoint,)
18971892

18981893
if response.status_code == requests.codes.ok:
18991894
feature_schema_nodes = response.json()['featureSchemaNodes']
@@ -1969,10 +1964,7 @@ def delete_feature_schema_from_ontology(
19691964
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
19701965
ontology_id) + "/feature-schemas/" + urllib.parse.quote(
19711966
feature_schema_id)
1972-
response = self._connection.delete(
1973-
ontology_endpoint,
1974-
headers=self.headers,
1975-
)
1967+
response = self._connection.delete(ontology_endpoint,)
19761968

19771969
if response.status_code == requests.codes.ok:
19781970
response_json = response.json()
@@ -2006,10 +1998,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
20061998
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
20071999
ontology_id) + '/feature-schemas/' + urllib.parse.quote(
20082000
root_feature_schema_id) + '/unarchive'
2009-
response = self._connection.patch(
2010-
ontology_endpoint,
2011-
headers=self.headers,
2012-
)
2001+
response = self._connection.patch(ontology_endpoint,)
20132002
if response.status_code == requests.codes.ok:
20142003
if not bool(response.json()['unarchived']):
20152004
raise labelbox.exceptions.LabelboxError(

0 commit comments

Comments
 (0)