@@ -115,16 +115,25 @@ def __init__(self,
115
115
self .app_url = app_url
116
116
self .endpoint = endpoint
117
117
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 ())
118
126
119
- self .headers = {
127
+ return connection
128
+
129
+ def _default_headers (self ):
130
+ return {
120
131
'Accept' : 'application/json' ,
121
132
'Content-Type' : 'application/json' ,
122
- 'Authorization' : 'Bearer %s' % api_key ,
133
+ 'Authorization' : 'Bearer %s' % self . api_key ,
123
134
'X-User-Agent' : f"python-sdk { SDK_VERSION } " ,
124
135
'X-Python-Version' : f"{ python_version_info ()} " ,
125
136
}
126
- self ._data_row_metadata_ontology = None
127
- self ._adv_client = AdvClient .factory (rest_endpoint , api_key )
128
137
129
138
@retry .Retry (predicate = retry .if_exception_type (
130
139
labelbox .exceptions .InternalServerError ,
@@ -202,9 +211,9 @@ def convert_value(value):
202
211
if files :
203
212
request .update ({'files' : files })
204
213
request ['headers' ] = {
205
- 'Authorization' : self .headers ['Authorization' ]
214
+ 'Authorization' : self ._connection . headers ['Authorization' ]
206
215
}
207
- response = requests .post (** request )
216
+ response = self . _connection .post (** request )
208
217
logger .debug ("Response: %s" , response .text )
209
218
except requests .exceptions .Timeout as e :
210
219
raise labelbox .exceptions .TimeoutError (str (e ))
@@ -409,7 +418,7 @@ def upload_data(self,
409
418
"map" : (None , json .dumps ({"1" : ["variables.file" ]})),
410
419
}
411
420
412
- response = requests .post (
421
+ response = self . _connection .post (
413
422
self .endpoint ,
414
423
headers = {"authorization" : "Bearer %s" % self .api_key },
415
424
data = request_data ,
@@ -1195,7 +1204,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1195
1204
1196
1205
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1197
1206
feature_schema_id )
1198
- response = requests .delete (
1207
+ response = self . _connection .delete (
1199
1208
endpoint ,
1200
1209
headers = self .headers ,
1201
1210
)
@@ -1215,7 +1224,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1215
1224
"""
1216
1225
endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1217
1226
ontology_id )
1218
- response = requests .delete (
1227
+ response = self . _connection .delete (
1219
1228
endpoint ,
1220
1229
headers = self .headers ,
1221
1230
)
@@ -1240,7 +1249,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
1240
1249
1241
1250
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1242
1251
feature_schema_id ) + '/definition'
1243
- response = requests .patch (
1252
+ response = self . _connection .patch (
1244
1253
endpoint ,
1245
1254
headers = self .headers ,
1246
1255
json = {"title" : title },
@@ -1273,7 +1282,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1273
1282
"featureSchemaId" ) or "new_feature_schema_id"
1274
1283
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1275
1284
feature_schema_id )
1276
- response = requests .put (
1285
+ response = self . _connection .put (
1277
1286
endpoint ,
1278
1287
headers = self .headers ,
1279
1288
json = {"normalized" : json .dumps (feature_schema )},
@@ -1303,7 +1312,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
1303
1312
endpoint = self .rest_endpoint + '/ontologies/' + urllib .parse .quote (
1304
1313
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1305
1314
feature_schema_id )
1306
- response = requests .post (
1315
+ response = self . _connection .post (
1307
1316
endpoint ,
1308
1317
headers = self .headers ,
1309
1318
json = {"position" : position },
@@ -1328,7 +1337,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1328
1337
"""
1329
1338
1330
1339
endpoint = self .rest_endpoint + "/ontologies/unused"
1331
- response = requests .get (
1340
+ response = self . _connection .get (
1332
1341
endpoint ,
1333
1342
headers = self .headers ,
1334
1343
json = {"after" : after },
@@ -1356,7 +1365,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1356
1365
"""
1357
1366
1358
1367
endpoint = self .rest_endpoint + "/feature-schemas/unused"
1359
- response = requests .get (
1368
+ response = self . _connection .get (
1360
1369
endpoint ,
1361
1370
headers = self .headers ,
1362
1371
json = {"after" : after },
@@ -1881,7 +1890,7 @@ def is_feature_schema_archived(self, ontology_id: str,
1881
1890
1882
1891
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1883
1892
ontology_id )
1884
- response = requests .get (
1893
+ response = self . _connection .get (
1885
1894
ontology_endpoint ,
1886
1895
headers = self .headers ,
1887
1896
)
@@ -1960,7 +1969,7 @@ def delete_feature_schema_from_ontology(
1960
1969
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1961
1970
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1962
1971
feature_schema_id )
1963
- response = requests .delete (
1972
+ response = self . _connection .delete (
1964
1973
ontology_endpoint ,
1965
1974
headers = self .headers ,
1966
1975
)
@@ -1997,7 +2006,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
1997
2006
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1998
2007
ontology_id ) + '/feature-schemas/' + urllib .parse .quote (
1999
2008
root_feature_schema_id ) + '/unarchive'
2000
- response = requests .patch (
2009
+ response = self . _connection .patch (
2001
2010
ontology_endpoint ,
2002
2011
headers = self .headers ,
2003
2012
)
0 commit comments