Skip to content

Commit d839245

Browse files
author
Val Brodsky
committed
Replace direct http connection with requests.Session()
Using connection pool
1 parent bab9746 commit d839245

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,25 @@ def __init__(self,
115115
self.app_url = app_url
116116
self.endpoint = endpoint
117117
self.rest_endpoint = rest_endpoint
118+
self._data_row_metadata_ontology = None
119+
self._adv_client = AdvClient.factory(rest_endpoint, api_key)
120+
self._connection: requests.Session = self._init_connection()
121+
122+
def _init_connection(self) -> requests.Session:
123+
connection = requests.Session(
124+
) # using default connection pool size of 10
125+
connection.headers.update(self._default_headers())
118126

119-
self.headers = {
127+
return connection
128+
129+
def _default_headers(self):
130+
return {
120131
'Accept': 'application/json',
121132
'Content-Type': 'application/json',
122-
'Authorization': 'Bearer %s' % api_key,
133+
'Authorization': 'Bearer %s' % self.api_key,
123134
'X-User-Agent': f"python-sdk {SDK_VERSION}",
124135
'X-Python-Version': f"{python_version_info()}",
125136
}
126-
self._data_row_metadata_ontology = None
127-
self._adv_client = AdvClient.factory(rest_endpoint, api_key)
128137

129138
@retry.Retry(predicate=retry.if_exception_type(
130139
labelbox.exceptions.InternalServerError,
@@ -202,9 +211,9 @@ def convert_value(value):
202211
if files:
203212
request.update({'files': files})
204213
request['headers'] = {
205-
'Authorization': self.headers['Authorization']
214+
'Authorization': self._connection.headers['Authorization']
206215
}
207-
response = requests.post(**request)
216+
response = self._connection.post(**request)
208217
logger.debug("Response: %s", response.text)
209218
except requests.exceptions.Timeout as e:
210219
raise labelbox.exceptions.TimeoutError(str(e))
@@ -409,7 +418,7 @@ def upload_data(self,
409418
"map": (None, json.dumps({"1": ["variables.file"]})),
410419
}
411420

412-
response = requests.post(
421+
response = self._connection.post(
413422
self.endpoint,
414423
headers={"authorization": "Bearer %s" % self.api_key},
415424
data=request_data,
@@ -1195,7 +1204,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
11951204

11961205
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
11971206
feature_schema_id)
1198-
response = requests.delete(
1207+
response = self._connection.delete(
11991208
endpoint,
12001209
headers=self.headers,
12011210
)
@@ -1215,7 +1224,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
12151224
"""
12161225
endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
12171226
ontology_id)
1218-
response = requests.delete(
1227+
response = self._connection.delete(
12191228
endpoint,
12201229
headers=self.headers,
12211230
)
@@ -1240,7 +1249,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
12401249

12411250
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
12421251
feature_schema_id) + '/definition'
1243-
response = requests.patch(
1252+
response = self._connection.patch(
12441253
endpoint,
12451254
headers=self.headers,
12461255
json={"title": title},
@@ -1273,7 +1282,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
12731282
"featureSchemaId") or "new_feature_schema_id"
12741283
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
12751284
feature_schema_id)
1276-
response = requests.put(
1285+
response = self._connection.put(
12771286
endpoint,
12781287
headers=self.headers,
12791288
json={"normalized": json.dumps(feature_schema)},
@@ -1303,7 +1312,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
13031312
endpoint = self.rest_endpoint + '/ontologies/' + urllib.parse.quote(
13041313
ontology_id) + "/feature-schemas/" + urllib.parse.quote(
13051314
feature_schema_id)
1306-
response = requests.post(
1315+
response = self._connection.post(
13071316
endpoint,
13081317
headers=self.headers,
13091318
json={"position": position},
@@ -1328,7 +1337,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
13281337
"""
13291338

13301339
endpoint = self.rest_endpoint + "/ontologies/unused"
1331-
response = requests.get(
1340+
response = self._connection.get(
13321341
endpoint,
13331342
headers=self.headers,
13341343
json={"after": after},
@@ -1356,7 +1365,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
13561365
"""
13571366

13581367
endpoint = self.rest_endpoint + "/feature-schemas/unused"
1359-
response = requests.get(
1368+
response = self._connection.get(
13601369
endpoint,
13611370
headers=self.headers,
13621371
json={"after": after},
@@ -1881,7 +1890,7 @@ def is_feature_schema_archived(self, ontology_id: str,
18811890

18821891
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
18831892
ontology_id)
1884-
response = requests.get(
1893+
response = self._connection.get(
18851894
ontology_endpoint,
18861895
headers=self.headers,
18871896
)
@@ -1960,7 +1969,7 @@ def delete_feature_schema_from_ontology(
19601969
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
19611970
ontology_id) + "/feature-schemas/" + urllib.parse.quote(
19621971
feature_schema_id)
1963-
response = requests.delete(
1972+
response = self._connection.delete(
19641973
ontology_endpoint,
19651974
headers=self.headers,
19661975
)
@@ -1997,7 +2006,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
19972006
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
19982007
ontology_id) + '/feature-schemas/' + urllib.parse.quote(
19992008
root_feature_schema_id) + '/unarchive'
2000-
response = requests.patch(
2009+
response = self._connection.patch(
20012010
ontology_endpoint,
20022011
headers=self.headers,
20032012
)

0 commit comments

Comments
 (0)