@@ -15,6 +15,9 @@ def __init__(self, api_key):
1515self .api_key = api_key
1616self .interceptor = _Interceptor (api_key )
1717
18+ def _error (r ):
19+ return not 200 <= r .status_code < 300
20+
1821def download (self ,id ,path ):
1922r = requests .get ('{}/download/{}' .format (API_URL ,id ),stream = True )
2023with open (path ,'wb' )as f :
@@ -32,24 +35,24 @@ def barcode_generate(self, format=None, content=None, size=None):
3235r = requests .post (API_URL + '/barcode/generate' ,auth = self .interceptor ,
3336json = json )
3437data = r .json ()
35- if not 200 <= r . status_code < 300 :
38+ if self . _error ( r ) :
3639raise APIError (data ['code' ],data ['message' ])
3740return data
3841
3942def barcode_scan (self ,file = None ):
4043files = {'file' :open (file ,'rb' )}
4144r = requests .post (API_URL + '/barcode/scan' ,auth = self .interceptor ,files = files )
4245data = r .json ()
43- if not 200 <= r . status_code < 300 :
46+ if self . _error ( r ) :
4447raise APIError (data ['code' ],data ['message' ])
4548return data
4649
47- def currency_exchange (self ,base = None ):
50+ def currency_convert (self ,base = None ):
4851json = {'base' :base }
49- r = requests .post (API_URL + '/currency/exchange ' ,auth = self .interceptor ,
52+ r = requests .post (API_URL + '/currency/convert ' ,auth = self .interceptor ,
5053json = json )
5154data = r .json ()
52- if not 200 <= r . status_code < 300 :
55+ if self . _error ( r ) :
5356raise APIError (data ['code' ],data ['message' ])
5457return data
5558
@@ -62,7 +65,7 @@ def dns_lookup(self, domain=None, type=None):
6265r = requests .post (API_URL + '/dns/lookup' ,auth = self .interceptor ,
6366json = json )
6467data = r .json ()
65- if not 200 <= r . status_code < 300 :
68+ if self . _error ( r ) :
6669raise APIError (data ['code' ],data ['message' ])
6770return data
6871
@@ -71,15 +74,15 @@ def email_verify(self, email=None):
7174r = requests .post (API_URL + '/email/verify' ,auth = self .interceptor ,
7275json = json )
7376data = r .json ()
74- if not 200 <= r . status_code < 300 :
77+ if self . _error ( r ) :
7578raise APIError (data ['code' ],data ['message' ])
7679return data
7780
7881def image_compress (self ,file = None ):
7982files = {'file' :open (file ,'rb' )}
8083r = requests .post (API_URL + '/image/compress' ,auth = self .interceptor ,files = files )
8184data = r .json ()
82- if not 200 <= r . status_code < 300 :
85+ if self . _error ( r ) :
8386raise APIError (data ['code' ],data ['message' ])
8487return data
8588
@@ -93,7 +96,7 @@ def image_resize(self, file=None, width=None, height=None, format=None):
9396r = requests .post ('{}/image/resize' .format (API_URL ),auth = self .interceptor ,
9497files = files ,data = data )
9598data = r .json ()
96- if not 200 <= r . status_code < 300 :
99+ if self . _error ( r ) :
97100raise APIError (data ['code' ],data ['message' ])
98101return data
99102
@@ -112,15 +115,15 @@ def image_watermark(self, file=None, text=None, font=None, size=None, color=None
112115r = requests .post ('{}/image/watermark' .format (API_URL ),auth = self .interceptor ,
113116files = files ,data = data )
114117data = r .json ()
115- if not 200 <= r . status_code < 300 :
118+ if self . _error ( r ) :
116119raise APIError (data ['code' ],data ['message' ])
117120return data
118121
119122def pdf_compress (self ,file = None ):
120123files = {'file' :open (file ,'rb' )}
121124r = requests .post (API_URL + '/pdf/compress' ,auth = self .interceptor ,files = files )
122125data = r .json ()
123- if not 200 <= r . status_code < 300 :
126+ if self . _error ( r ) :
124127raise APIError (data ['code' ],data ['message' ])
125128return data
126129
@@ -131,7 +134,7 @@ def pdf_image(self, file=None, extract=None):
131134 }
132135r = requests .post (API_URL + '/pdf/image' ,auth = self .interceptor ,files = files ,data = data )
133136data = r .json ()
134- if not 200 <= r . status_code < 300 :
137+ if self . _error ( r ) :
135138raise APIError (data ['code' ],data ['message' ])
136139return data
137140
@@ -142,7 +145,7 @@ def pdf_split(self, file=None, pages=None):
142145 }
143146r = requests .post (API_URL + '/pdf/split' ,auth = self .interceptor ,files = files ,data = data )
144147data = r .json ()
145- if not 200 <= r . status_code < 300 :
148+ if self . _error ( r ) :
146149raise APIError (data ['code' ],data ['message' ])
147150return data
148151
@@ -151,7 +154,7 @@ def text_sentiment(self, text=None):
151154r = requests .post (API_URL + '/text/sentiment' ,auth = self .interceptor ,
152155json = json )
153156data = r .json ()
154- if not 200 <= r . status_code < 300 :
157+ if self . _error ( r ) :
155158raise APIError (data ['code' ],data ['message' ])
156159return data
157160
@@ -160,7 +163,7 @@ def text_spellcheck(self, text=None):
160163r = requests .post (API_URL + '/text/spellcheck' ,auth = self .interceptor ,
161164json = json )
162165data = r .json ()
163- if not 200 <= r . status_code < 300 :
166+ if self . _error ( r ) :
164167raise APIError (data ['code' ],data ['message' ])
165168return data
166169
@@ -174,7 +177,7 @@ def text_summary(self, text=None, url=None, language=None, length=None):
174177r = requests .post (API_URL + '/text/summary' ,auth = self .interceptor ,
175178json = json )
176179data = r .json ()
177- if not 200 <= r . status_code < 300 :
180+ if self . _error ( r ) :
178181raise APIError (data ['code' ],data ['message' ])
179182return data
180183
@@ -187,7 +190,7 @@ def webpage_pdf(self, url=None, size=None, layout=None):
187190r = requests .post (API_URL + '/webpage/pdf' ,auth = self .interceptor ,
188191json = json )
189192data = r .json ()
190- if not 200 <= r . status_code < 300 :
193+ if self . _error ( r ) :
191194raise APIError (data ['code' ],data ['message' ])
192195return data
193196
@@ -196,7 +199,7 @@ def word_lookup(self, word=None):
196199r = requests .post (API_URL + '/word/lookup' ,auth = self .interceptor ,
197200json = json )
198201data = r .json ()
199- if not 200 <= r . status_code < 300 :
202+ if self . _error ( r ) :
200203raise APIError (data ['code' ],data ['message' ])
201204return data
202205