1818from labelbox .schema .organization import Organization
1919from labelbox .schema .labeling_frontend import LabelingFrontend
2020
21-
2221logger = logging .getLogger (__name__ )
2322
24-
2523_LABELBOX_API_KEY = "LABELBOX_API_KEY"
2624
2725
@@ -31,7 +29,8 @@ class Client:
3129 querying and creating top-level data objects (Projects, Datasets).
3230 """
3331
34- def __init__ (self , api_key = None ,
32+ def __init__ (self ,
33+ api_key = None ,
3534 endpoint = 'https://api.labelbox.com/graphql' ):
3635 """ Creates and initializes a Labelbox Client.
3736
@@ -54,9 +53,11 @@ def __init__(self, api_key=None,
5453 logger .info ("Initializing Labelbox client at '%s'" , endpoint )
5554
5655 self .endpoint = endpoint
57- self .headers = {'Accept' : 'application/json' ,
58- 'Content-Type' : 'application/json' ,
59- 'Authorization' : 'Bearer %s' % api_key }
56+ self .headers = {
57+ 'Accept' : 'application/json' ,
58+ 'Content-Type' : 'application/json' ,
59+ 'Authorization' : 'Bearer %s' % api_key
60+ }
6061
6162 def execute (self , query , params = None , timeout = 10.0 ):
6263 """ Sends a request to the server for the execution of the
@@ -95,15 +96,17 @@ def convert_value(value):
9596 return value
9697
9798 if params is not None :
98- params = {key : convert_value (value ) for key , value in params .items ()}
99+ params = {
100+ key : convert_value (value ) for key , value in params .items ()
101+ }
99102
100- data = json .dumps (
101- {'query' : query , 'variables' : params }).encode ('utf-8' )
103+ data = json .dumps ({'query' : query , 'variables' : params }).encode ('utf-8' )
102104
103105 try :
104- response = requests .post (self .endpoint , data = data ,
105- headers = self .headers ,
106- timeout = timeout )
106+ response = requests .post (self .endpoint ,
107+ data = data ,
108+ headers = self .headers ,
109+ timeout = timeout )
107110 logger .debug ("Response: %s" , response .text )
108111 except requests .exceptions .Timeout as e :
109112 raise labelbox .exceptions .TimeoutError (str (e ))
@@ -136,8 +139,8 @@ def check_errors(keywords, *path):
136139 return error
137140 return None
138141
139- if check_errors (["AUTHENTICATION_ERROR" ],
140- "extensions" , "exception" , " code" ) is not None :
142+ if check_errors (["AUTHENTICATION_ERROR" ], "extensions" , "exception" ,
143+ "code" ) is not None :
141144 raise labelbox .exceptions .AuthenticationError ("Invalid API key" )
142145
143146 authorization_error = check_errors (["AUTHORIZATION_ERROR" ],
@@ -155,7 +158,8 @@ def check_errors(keywords, *path):
155158 else :
156159 raise labelbox .exceptions .InvalidQueryError (message )
157160
158- graphql_error = check_errors (["GRAPHQL_PARSE_FAILED" ], "extensions" , "code" )
161+ graphql_error = check_errors (["GRAPHQL_PARSE_FAILED" ], "extensions" ,
162+ "code" )
159163 if graphql_error is not None :
160164 raise labelbox .exceptions .InvalidQueryError (
161165 graphql_error ["message" ])
@@ -167,8 +171,8 @@ def check_errors(keywords, *path):
167171
168172 if len (errors ) > 0 :
169173 logger .warning ("Unparsed errors on query execution: %r" , errors )
170- raise labelbox .exceptions .LabelboxError (
171- "Unknown error: %s" % str (errors ))
174+ raise labelbox .exceptions .LabelboxError ("Unknown error: %s" %
175+ str (errors ))
172176
173177 return response ["data" ]
174178
@@ -201,24 +205,30 @@ def upload_data(self, data):
201205 labelbox.exceptions.LabelboxError: If upload failed.
202206 """
203207 request_data = {
204- "operations" : json .dumps ({
205- "variables" : {"file" : None , "contentLength" : len (data ), "sign" : False },
206- "query" : """mutation UploadFile($file: Upload!, $contentLength: Int!,
208+ "operations" :
209+ json .dumps ({
210+ "variables" : {
211+ "file" : None ,
212+ "contentLength" : len (data ),
213+ "sign" : False
214+ },
215+ "query" :
216+ """mutation UploadFile($file: Upload!, $contentLength: Int!,
207217 $sign: Boolean) {
208218 uploadFile(file: $file, contentLength: $contentLength,
209- sign: $sign) {url filename} } """ ,}),
219+ sign: $sign) {url filename} } """ ,
220+ }),
210221 "map" : (None , json .dumps ({"1" : ["variables.file" ]})),
211- }
222+ }
212223 response = requests .post (
213224 self .endpoint ,
214225 headers = {"authorization" : "Bearer %s" % self .api_key },
215226 data = request_data ,
216- files = {"1" : data }
217- )
227+ files = {"1" : data })
218228
219229 try :
220230 file_data = response .json ().get ("data" , None )
221- except ValueError as e : # response is not valid JSON
231+ except ValueError as e : # response is not valid JSON
222232 raise labelbox .exceptions .LabelboxError (
223233 "Failed to upload, unknown cause" , e )
224234
@@ -350,9 +360,11 @@ def _create(self, db_object_type, data):
350360 """
351361 # Convert string attribute names to Field or Relationship objects.
352362 # Also convert Labelbox object values to their UIDs.
353- data = {db_object_type .attribute (attr ) if isinstance (attr , str ) else attr :
354- value .uid if isinstance (value , DbObject ) else value
355- for attr , value in data .items ()}
363+ data = {
364+ db_object_type .attribute (attr ) if isinstance (attr , str ) else attr :
365+ value .uid if isinstance (value , DbObject ) else value
366+ for attr , value in data .items ()
367+ }
356368
357369 query_string , params = query .create (db_object_type , data )
358370 res = self .execute (query_string , params )
0 commit comments