@@ -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'
@@ -1085,6 +1102,7 @@ def get_feature_schema(self, feature_schema_id):
1085
1102
query_str = """query rootSchemaNodePyApi($rootSchemaNodeWhere: RootSchemaNodeWhere!){
1086
1103
rootSchemaNode(where: $rootSchemaNodeWhere){%s}
1087
1104
}""" % query .results_query_part (Entity .FeatureSchema )
1105
+
1088
1106
res = self .execute (
1089
1107
query_str ,
1090
1108
{'rootSchemaNodeWhere' : {
@@ -1195,10 +1213,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1195
1213
1196
1214
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1197
1215
feature_schema_id )
1198
- response = requests .delete (
1199
- endpoint ,
1200
- headers = self .headers ,
1201
- )
1216
+ response = self ._connection .delete (endpoint )
1202
1217
1203
1218
if response .status_code != requests .codes .no_content :
1204
1219
raise labelbox .exceptions .LabelboxError (
@@ -1215,10 +1230,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1215
1230
"""
1216
1231
endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1217
1232
ontology_id )
1218
- response = requests .delete (
1219
- endpoint ,
1220
- headers = self .headers ,
1221
- )
1233
+ response = self ._connection .delete (endpoint )
1222
1234
1223
1235
if response .status_code != requests .codes .no_content :
1224
1236
raise labelbox .exceptions .LabelboxError (
@@ -1240,11 +1252,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
1240
1252
1241
1253
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1242
1254
feature_schema_id ) + '/definition'
1243
- response = requests .patch (
1244
- endpoint ,
1245
- headers = self .headers ,
1246
- json = {"title" : title },
1247
- )
1255
+ response = self ._connection .patch (endpoint , json = {"title" : title })
1248
1256
1249
1257
if response .status_code == requests .codes .ok :
1250
1258
return self .get_feature_schema (feature_schema_id )
@@ -1273,11 +1281,8 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1273
1281
"featureSchemaId" ) or "new_feature_schema_id"
1274
1282
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1275
1283
feature_schema_id )
1276
- response = requests .put (
1277
- endpoint ,
1278
- headers = self .headers ,
1279
- json = {"normalized" : json .dumps (feature_schema )},
1280
- )
1284
+ response = self ._connection .put (
1285
+ endpoint , json = {"normalized" : json .dumps (feature_schema )})
1281
1286
1282
1287
if response .status_code == requests .codes .ok :
1283
1288
return self .get_feature_schema (response .json ()['schemaId' ])
@@ -1303,11 +1308,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
1303
1308
endpoint = self .rest_endpoint + '/ontologies/' + urllib .parse .quote (
1304
1309
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1305
1310
feature_schema_id )
1306
- response = requests .post (
1307
- endpoint ,
1308
- headers = self .headers ,
1309
- json = {"position" : position },
1310
- )
1311
+ response = self ._connection .post (endpoint , json = {"position" : position })
1311
1312
if response .status_code != requests .codes .created :
1312
1313
raise labelbox .exceptions .LabelboxError (
1313
1314
"Failed to insert the feature schema into the ontology, message: "
@@ -1328,11 +1329,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1328
1329
"""
1329
1330
1330
1331
endpoint = self .rest_endpoint + "/ontologies/unused"
1331
- response = requests .get (
1332
- endpoint ,
1333
- headers = self .headers ,
1334
- json = {"after" : after },
1335
- )
1332
+ response = self ._connection .get (endpoint , json = {"after" : after })
1336
1333
1337
1334
if response .status_code == requests .codes .ok :
1338
1335
return response .json ()
@@ -1356,11 +1353,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1356
1353
"""
1357
1354
1358
1355
endpoint = self .rest_endpoint + "/feature-schemas/unused"
1359
- response = requests .get (
1360
- endpoint ,
1361
- headers = self .headers ,
1362
- json = {"after" : after },
1363
- )
1356
+ response = self ._connection .get (endpoint , json = {"after" : after })
1364
1357
1365
1358
if response .status_code == requests .codes .ok :
1366
1359
return response .json ()
@@ -1881,10 +1874,7 @@ def is_feature_schema_archived(self, ontology_id: str,
1881
1874
1882
1875
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1883
1876
ontology_id )
1884
- response = requests .get (
1885
- ontology_endpoint ,
1886
- headers = self .headers ,
1887
- )
1877
+ response = self ._connection .get (ontology_endpoint )
1888
1878
1889
1879
if response .status_code == requests .codes .ok :
1890
1880
feature_schema_nodes = response .json ()['featureSchemaNodes' ]
@@ -1960,10 +1950,7 @@ def delete_feature_schema_from_ontology(
1960
1950
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1961
1951
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1962
1952
feature_schema_id )
1963
- response = requests .delete (
1964
- ontology_endpoint ,
1965
- headers = self .headers ,
1966
- )
1953
+ response = self ._connection .delete (ontology_endpoint )
1967
1954
1968
1955
if response .status_code == requests .codes .ok :
1969
1956
response_json = response .json ()
@@ -1997,10 +1984,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
1997
1984
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1998
1985
ontology_id ) + '/feature-schemas/' + urllib .parse .quote (
1999
1986
root_feature_schema_id ) + '/unarchive'
2000
- response = requests .patch (
2001
- ontology_endpoint ,
2002
- headers = self .headers ,
2003
- )
1987
+ response = self ._connection .patch (ontology_endpoint )
2004
1988
if response .status_code == requests .codes .ok :
2005
1989
if not bool (response .json ()['unarchived' ]):
2006
1990
raise labelbox .exceptions .LabelboxError (
0 commit comments