@@ -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 ())
126
+
127
+ return connection
118
128
119
- self .headers = {
129
+ def _default_headers (self ):
130
+ return {
131
+ 'Authorization' : 'Bearer %s' % self .api_key ,
120
132
'Accept' : 'application/json' ,
121
133
'Content-Type' : 'application/json' ,
122
- 'Authorization' : 'Bearer %s' % 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 ,
@@ -193,18 +202,19 @@ def convert_value(value):
193
202
"/graphql" , "/_gql" )
194
203
195
204
try :
196
- request = {
197
- 'url' : endpoint ,
198
- 'data' : data ,
199
- 'headers' : self .headers ,
200
- 'timeout' : timeout
201
- }
205
+ headers = self ._connection .headers .copy ()
202
206
if files :
203
- request .update ({'files' : files })
204
- request ['headers' ] = {
205
- 'Authorization' : self .headers ['Authorization' ]
206
- }
207
- response = requests .post (** request )
207
+ del headers ['Content-Type' ]
208
+ del headers ['Accept' ]
209
+ request = requests .Request ('POST' ,
210
+ endpoint ,
211
+ headers = headers ,
212
+ data = data ,
213
+ files = files if files else None )
214
+
215
+ prepped : requests .PreparedRequest = request .prepare ()
216
+
217
+ response = self ._connection .send (prepped , timeout = timeout )
208
218
logger .debug ("Response: %s" , response .text )
209
219
except requests .exceptions .Timeout as e :
210
220
raise labelbox .exceptions .TimeoutError (str (e ))
@@ -409,14 +419,21 @@ def upload_data(self,
409
419
"map" : (None , json .dumps ({"1" : ["variables.file" ]})),
410
420
}
411
421
412
- response = requests .post (
413
- self .endpoint ,
414
- headers = {"authorization" : "Bearer %s" % self .api_key },
415
- data = request_data ,
416
- files = {
417
- "1" : (filename , content , content_type ) if
418
- (filename and content_type ) else content
419
- })
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 )
420
437
421
438
if response .status_code == 502 :
422
439
error_502 = '502 Bad Gateway'
@@ -1178,6 +1195,7 @@ def get_feature_schema(self, feature_schema_id):
1178
1195
query_str = """query rootSchemaNodePyApi($rootSchemaNodeWhere: RootSchemaNodeWhere!){
1179
1196
rootSchemaNode(where: $rootSchemaNodeWhere){%s}
1180
1197
}""" % query .results_query_part (Entity .FeatureSchema )
1198
+
1181
1199
res = self .execute (
1182
1200
query_str ,
1183
1201
{'rootSchemaNodeWhere' : {
@@ -1285,10 +1303,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1285
1303
1286
1304
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1287
1305
feature_schema_id )
1288
- response = requests .delete (
1289
- endpoint ,
1290
- headers = self .headers ,
1291
- )
1306
+ response = self ._connection .delete (endpoint )
1292
1307
1293
1308
if response .status_code != requests .codes .no_content :
1294
1309
raise labelbox .exceptions .LabelboxError (
@@ -1305,10 +1320,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1305
1320
"""
1306
1321
endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1307
1322
ontology_id )
1308
- response = requests .delete (
1309
- endpoint ,
1310
- headers = self .headers ,
1311
- )
1323
+ response = self ._connection .delete (endpoint )
1312
1324
1313
1325
if response .status_code != requests .codes .no_content :
1314
1326
raise labelbox .exceptions .LabelboxError (
@@ -1330,11 +1342,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
1330
1342
1331
1343
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1332
1344
feature_schema_id ) + '/definition'
1333
- response = requests .patch (
1334
- endpoint ,
1335
- headers = self .headers ,
1336
- json = {"title" : title },
1337
- )
1345
+ response = self ._connection .patch (endpoint , json = {"title" : title })
1338
1346
1339
1347
if response .status_code == requests .codes .ok :
1340
1348
return self .get_feature_schema (feature_schema_id )
@@ -1363,11 +1371,8 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1363
1371
"featureSchemaId" ) or "new_feature_schema_id"
1364
1372
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1365
1373
feature_schema_id )
1366
- response = requests .put (
1367
- endpoint ,
1368
- headers = self .headers ,
1369
- json = {"normalized" : json .dumps (feature_schema )},
1370
- )
1374
+ response = self ._connection .put (
1375
+ endpoint , json = {"normalized" : json .dumps (feature_schema )})
1371
1376
1372
1377
if response .status_code == requests .codes .ok :
1373
1378
return self .get_feature_schema (response .json ()['schemaId' ])
@@ -1393,11 +1398,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
1393
1398
endpoint = self .rest_endpoint + '/ontologies/' + urllib .parse .quote (
1394
1399
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1395
1400
feature_schema_id )
1396
- response = requests .post (
1397
- endpoint ,
1398
- headers = self .headers ,
1399
- json = {"position" : position },
1400
- )
1401
+ response = self ._connection .post (endpoint , json = {"position" : position })
1401
1402
if response .status_code != requests .codes .created :
1402
1403
raise labelbox .exceptions .LabelboxError (
1403
1404
"Failed to insert the feature schema into the ontology, message: "
@@ -1418,11 +1419,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1418
1419
"""
1419
1420
1420
1421
endpoint = self .rest_endpoint + "/ontologies/unused"
1421
- response = requests .get (
1422
- endpoint ,
1423
- headers = self .headers ,
1424
- json = {"after" : after },
1425
- )
1422
+ response = self ._connection .get (endpoint , json = {"after" : after })
1426
1423
1427
1424
if response .status_code == requests .codes .ok :
1428
1425
return response .json ()
@@ -1446,11 +1443,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1446
1443
"""
1447
1444
1448
1445
endpoint = self .rest_endpoint + "/feature-schemas/unused"
1449
- response = requests .get (
1450
- endpoint ,
1451
- headers = self .headers ,
1452
- json = {"after" : after },
1453
- )
1446
+ response = self ._connection .get (endpoint , json = {"after" : after })
1454
1447
1455
1448
if response .status_code == requests .codes .ok :
1456
1449
return response .json ()
@@ -1972,10 +1965,7 @@ def is_feature_schema_archived(self, ontology_id: str,
1972
1965
1973
1966
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1974
1967
ontology_id )
1975
- response = requests .get (
1976
- ontology_endpoint ,
1977
- headers = self .headers ,
1978
- )
1968
+ response = self ._connection .get (ontology_endpoint )
1979
1969
1980
1970
if response .status_code == requests .codes .ok :
1981
1971
feature_schema_nodes = response .json ()['featureSchemaNodes' ]
@@ -2051,10 +2041,7 @@ def delete_feature_schema_from_ontology(
2051
2041
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
2052
2042
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
2053
2043
feature_schema_id )
2054
- response = requests .delete (
2055
- ontology_endpoint ,
2056
- headers = self .headers ,
2057
- )
2044
+ response = self ._connection .delete (ontology_endpoint )
2058
2045
2059
2046
if response .status_code == requests .codes .ok :
2060
2047
response_json = response .json ()
@@ -2088,10 +2075,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
2088
2075
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
2089
2076
ontology_id ) + '/feature-schemas/' + urllib .parse .quote (
2090
2077
root_feature_schema_id ) + '/unarchive'
2091
- response = requests .patch (
2092
- ontology_endpoint ,
2093
- headers = self .headers ,
2094
- )
2078
+ response = self ._connection .patch (ontology_endpoint )
2095
2079
if response .status_code == requests .codes .ok :
2096
2080
if not bool (response .json ()['unarchived' ]):
2097
2081
raise labelbox .exceptions .LabelboxError (
0 commit comments