Skip to content

Commit 1d6a8d9

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

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 27 additions & 23 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())
126+
127+
return connection
118128

119-
self.headers = {
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,
@@ -193,18 +202,13 @@ def convert_value(value):
193202
"/graphql", "/_gql")
194203

195204
try:
196-
request = {
197-
'url': endpoint,
198-
'data': data,
199-
'headers': self.headers,
200-
'timeout': timeout
201-
}
205+
request = {'url': endpoint, 'data': data, 'timeout': timeout}
202206
if files:
203207
request.update({'files': files})
204208
request['headers'] = {
205-
'Authorization': self.headers['Authorization']
209+
'Authorization': self._connection.headers['Authorization']
206210
}
207-
response = requests.post(**request)
211+
response = self._connection.post(**request)
208212
logger.debug("Response: %s", response.text)
209213
except requests.exceptions.Timeout as e:
210214
raise labelbox.exceptions.TimeoutError(str(e))
@@ -409,7 +413,7 @@ def upload_data(self,
409413
"map": (None, json.dumps({"1": ["variables.file"]})),
410414
}
411415

412-
response = requests.post(
416+
response = self._connection.post(
413417
self.endpoint,
414418
headers={"authorization": "Bearer %s" % self.api_key},
415419
data=request_data,
@@ -1195,7 +1199,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
11951199

11961200
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
11971201
feature_schema_id)
1198-
response = requests.delete(
1202+
response = self._connection.delete(
11991203
endpoint,
12001204
headers=self.headers,
12011205
)
@@ -1215,7 +1219,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
12151219
"""
12161220
endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
12171221
ontology_id)
1218-
response = requests.delete(
1222+
response = self._connection.delete(
12191223
endpoint,
12201224
headers=self.headers,
12211225
)
@@ -1240,7 +1244,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
12401244

12411245
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
12421246
feature_schema_id) + '/definition'
1243-
response = requests.patch(
1247+
response = self._connection.patch(
12441248
endpoint,
12451249
headers=self.headers,
12461250
json={"title": title},
@@ -1273,7 +1277,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
12731277
"featureSchemaId") or "new_feature_schema_id"
12741278
endpoint = self.rest_endpoint + "/feature-schemas/" + urllib.parse.quote(
12751279
feature_schema_id)
1276-
response = requests.put(
1280+
response = self._connection.put(
12771281
endpoint,
12781282
headers=self.headers,
12791283
json={"normalized": json.dumps(feature_schema)},
@@ -1303,7 +1307,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
13031307
endpoint = self.rest_endpoint + '/ontologies/' + urllib.parse.quote(
13041308
ontology_id) + "/feature-schemas/" + urllib.parse.quote(
13051309
feature_schema_id)
1306-
response = requests.post(
1310+
response = self._connection.post(
13071311
endpoint,
13081312
headers=self.headers,
13091313
json={"position": position},
@@ -1328,7 +1332,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
13281332
"""
13291333

13301334
endpoint = self.rest_endpoint + "/ontologies/unused"
1331-
response = requests.get(
1335+
response = self._connection.get(
13321336
endpoint,
13331337
headers=self.headers,
13341338
json={"after": after},
@@ -1356,7 +1360,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
13561360
"""
13571361

13581362
endpoint = self.rest_endpoint + "/feature-schemas/unused"
1359-
response = requests.get(
1363+
response = self._connection.get(
13601364
endpoint,
13611365
headers=self.headers,
13621366
json={"after": after},
@@ -1881,7 +1885,7 @@ def is_feature_schema_archived(self, ontology_id: str,
18811885

18821886
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
18831887
ontology_id)
1884-
response = requests.get(
1888+
response = self._connection.get(
18851889
ontology_endpoint,
18861890
headers=self.headers,
18871891
)
@@ -1960,7 +1964,7 @@ def delete_feature_schema_from_ontology(
19601964
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
19611965
ontology_id) + "/feature-schemas/" + urllib.parse.quote(
19621966
feature_schema_id)
1963-
response = requests.delete(
1967+
response = self._connection.delete(
19641968
ontology_endpoint,
19651969
headers=self.headers,
19661970
)
@@ -1997,7 +2001,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
19972001
ontology_endpoint = self.rest_endpoint + "/ontologies/" + urllib.parse.quote(
19982002
ontology_id) + '/feature-schemas/' + urllib.parse.quote(
19992003
root_feature_schema_id) + '/unarchive'
2000-
response = requests.patch(
2004+
response = self._connection.patch(
20012005
ontology_endpoint,
20022006
headers=self.headers,
20032007
)

0 commit comments

Comments
 (0)