@@ -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 {
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 ,
@@ -193,18 +202,13 @@ 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
+ request = {'url' : endpoint , 'data' : data , 'timeout' : timeout }
202
206
if files :
203
207
request .update ({'files' : files })
204
208
request ['headers' ] = {
205
- 'Authorization' : self .headers ['Authorization' ]
209
+ 'Authorization' : self ._connection . headers ['Authorization' ]
206
210
}
207
- response = requests .post (** request )
211
+ response = self . _connection .post (** request )
208
212
logger .debug ("Response: %s" , response .text )
209
213
except requests .exceptions .Timeout as e :
210
214
raise labelbox .exceptions .TimeoutError (str (e ))
@@ -409,7 +413,7 @@ def upload_data(self,
409
413
"map" : (None , json .dumps ({"1" : ["variables.file" ]})),
410
414
}
411
415
412
- response = requests .post (
416
+ response = self . _connection .post (
413
417
self .endpoint ,
414
418
headers = {"authorization" : "Bearer %s" % self .api_key },
415
419
data = request_data ,
@@ -1195,7 +1199,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1195
1199
1196
1200
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1197
1201
feature_schema_id )
1198
- response = requests .delete (
1202
+ response = self . _connection .delete (
1199
1203
endpoint ,
1200
1204
headers = self .headers ,
1201
1205
)
@@ -1215,7 +1219,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1215
1219
"""
1216
1220
endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1217
1221
ontology_id )
1218
- response = requests .delete (
1222
+ response = self . _connection .delete (
1219
1223
endpoint ,
1220
1224
headers = self .headers ,
1221
1225
)
@@ -1240,7 +1244,7 @@ def update_feature_schema_title(self, feature_schema_id: str,
1240
1244
1241
1245
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1242
1246
feature_schema_id ) + '/definition'
1243
- response = requests .patch (
1247
+ response = self . _connection .patch (
1244
1248
endpoint ,
1245
1249
headers = self .headers ,
1246
1250
json = {"title" : title },
@@ -1273,7 +1277,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1273
1277
"featureSchemaId" ) or "new_feature_schema_id"
1274
1278
endpoint = self .rest_endpoint + "/feature-schemas/" + urllib .parse .quote (
1275
1279
feature_schema_id )
1276
- response = requests .put (
1280
+ response = self . _connection .put (
1277
1281
endpoint ,
1278
1282
headers = self .headers ,
1279
1283
json = {"normalized" : json .dumps (feature_schema )},
@@ -1303,7 +1307,7 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
1303
1307
endpoint = self .rest_endpoint + '/ontologies/' + urllib .parse .quote (
1304
1308
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1305
1309
feature_schema_id )
1306
- response = requests .post (
1310
+ response = self . _connection .post (
1307
1311
endpoint ,
1308
1312
headers = self .headers ,
1309
1313
json = {"position" : position },
@@ -1328,7 +1332,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1328
1332
"""
1329
1333
1330
1334
endpoint = self .rest_endpoint + "/ontologies/unused"
1331
- response = requests .get (
1335
+ response = self . _connection .get (
1332
1336
endpoint ,
1333
1337
headers = self .headers ,
1334
1338
json = {"after" : after },
@@ -1356,7 +1360,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1356
1360
"""
1357
1361
1358
1362
endpoint = self .rest_endpoint + "/feature-schemas/unused"
1359
- response = requests .get (
1363
+ response = self . _connection .get (
1360
1364
endpoint ,
1361
1365
headers = self .headers ,
1362
1366
json = {"after" : after },
@@ -1881,7 +1885,7 @@ def is_feature_schema_archived(self, ontology_id: str,
1881
1885
1882
1886
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1883
1887
ontology_id )
1884
- response = requests .get (
1888
+ response = self . _connection .get (
1885
1889
ontology_endpoint ,
1886
1890
headers = self .headers ,
1887
1891
)
@@ -1960,7 +1964,7 @@ def delete_feature_schema_from_ontology(
1960
1964
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1961
1965
ontology_id ) + "/feature-schemas/" + urllib .parse .quote (
1962
1966
feature_schema_id )
1963
- response = requests .delete (
1967
+ response = self . _connection .delete (
1964
1968
ontology_endpoint ,
1965
1969
headers = self .headers ,
1966
1970
)
@@ -1997,7 +2001,7 @@ def unarchive_feature_schema_node(self, ontology_id: str,
1997
2001
ontology_endpoint = self .rest_endpoint + "/ontologies/" + urllib .parse .quote (
1998
2002
ontology_id ) + '/feature-schemas/' + urllib .parse .quote (
1999
2003
root_feature_schema_id ) + '/unarchive'
2000
- response = requests .patch (
2004
+ response = self . _connection .patch (
2001
2005
ontology_endpoint ,
2002
2006
headers = self .headers ,
2003
2007
)
0 commit comments