@@ -15,6 +15,9 @@ def __init__(self, api_key):
15
15
self .api_key = api_key
16
16
self .interceptor = _Interceptor (api_key )
17
17
18
+ def _error (r ):
19
+ return not 200 <= r .status_code < 300
20
+
18
21
def download (self , id , path ):
19
22
r = requests .get ('{}/download/{}' .format (API_URL , id ), stream = True )
20
23
with open (path , 'wb' ) as f :
@@ -32,24 +35,24 @@ def barcode_generate(self, format=None, content=None, size=None):
32
35
r = requests .post (API_URL + '/barcode/generate' , auth = self .interceptor ,
33
36
json = json )
34
37
data = r .json ()
35
- if not 200 <= r . status_code < 300 :
38
+ if self . _error ( r ) :
36
39
raise APIError (data ['code' ], data ['message' ])
37
40
return data
38
41
39
42
def barcode_scan (self , file = None ):
40
43
files = {'file' : open (file , 'rb' )}
41
44
r = requests .post (API_URL + '/barcode/scan' , auth = self .interceptor , files = files )
42
45
data = r .json ()
43
- if not 200 <= r . status_code < 300 :
46
+ if self . _error ( r ) :
44
47
raise APIError (data ['code' ], data ['message' ])
45
48
return data
46
49
47
- def currency_exchange (self , base = None ):
50
+ def currency_convert (self , base = None ):
48
51
json = {'base' : base }
49
- r = requests .post (API_URL + '/currency/exchange ' , auth = self .interceptor ,
52
+ r = requests .post (API_URL + '/currency/convert ' , auth = self .interceptor ,
50
53
json = json )
51
54
data = r .json ()
52
- if not 200 <= r . status_code < 300 :
55
+ if self . _error ( r ) :
53
56
raise APIError (data ['code' ], data ['message' ])
54
57
return data
55
58
@@ -62,7 +65,7 @@ def dns_lookup(self, domain=None, type=None):
62
65
r = requests .post (API_URL + '/dns/lookup' , auth = self .interceptor ,
63
66
json = json )
64
67
data = r .json ()
65
- if not 200 <= r . status_code < 300 :
68
+ if self . _error ( r ) :
66
69
raise APIError (data ['code' ], data ['message' ])
67
70
return data
68
71
@@ -71,15 +74,15 @@ def email_verify(self, email=None):
71
74
r = requests .post (API_URL + '/email/verify' , auth = self .interceptor ,
72
75
json = json )
73
76
data = r .json ()
74
- if not 200 <= r . status_code < 300 :
77
+ if self . _error ( r ) :
75
78
raise APIError (data ['code' ], data ['message' ])
76
79
return data
77
80
78
81
def image_compress (self , file = None ):
79
82
files = {'file' : open (file , 'rb' )}
80
83
r = requests .post (API_URL + '/image/compress' , auth = self .interceptor , files = files )
81
84
data = r .json ()
82
- if not 200 <= r . status_code < 300 :
85
+ if self . _error ( r ) :
83
86
raise APIError (data ['code' ], data ['message' ])
84
87
return data
85
88
@@ -93,7 +96,7 @@ def image_resize(self, file=None, width=None, height=None, format=None):
93
96
r = requests .post ('{}/image/resize' .format (API_URL ), auth = self .interceptor ,
94
97
files = files , data = data )
95
98
data = r .json ()
96
- if not 200 <= r . status_code < 300 :
99
+ if self . _error ( r ) :
97
100
raise APIError (data ['code' ], data ['message' ])
98
101
return data
99
102
@@ -112,15 +115,15 @@ def image_watermark(self, file=None, text=None, font=None, size=None, color=None
112
115
r = requests .post ('{}/image/watermark' .format (API_URL ), auth = self .interceptor ,
113
116
files = files , data = data )
114
117
data = r .json ()
115
- if not 200 <= r . status_code < 300 :
118
+ if self . _error ( r ) :
116
119
raise APIError (data ['code' ], data ['message' ])
117
120
return data
118
121
119
122
def pdf_compress (self , file = None ):
120
123
files = {'file' : open (file , 'rb' )}
121
124
r = requests .post (API_URL + '/pdf/compress' , auth = self .interceptor , files = files )
122
125
data = r .json ()
123
- if not 200 <= r . status_code < 300 :
126
+ if self . _error ( r ) :
124
127
raise APIError (data ['code' ], data ['message' ])
125
128
return data
126
129
@@ -131,7 +134,7 @@ def pdf_image(self, file=None, extract=None):
131
134
}
132
135
r = requests .post (API_URL + '/pdf/image' , auth = self .interceptor , files = files , data = data )
133
136
data = r .json ()
134
- if not 200 <= r . status_code < 300 :
137
+ if self . _error ( r ) :
135
138
raise APIError (data ['code' ], data ['message' ])
136
139
return data
137
140
@@ -142,7 +145,7 @@ def pdf_split(self, file=None, pages=None):
142
145
}
143
146
r = requests .post (API_URL + '/pdf/split' , auth = self .interceptor , files = files , data = data )
144
147
data = r .json ()
145
- if not 200 <= r . status_code < 300 :
148
+ if self . _error ( r ) :
146
149
raise APIError (data ['code' ], data ['message' ])
147
150
return data
148
151
@@ -151,7 +154,7 @@ def text_sentiment(self, text=None):
151
154
r = requests .post (API_URL + '/text/sentiment' , auth = self .interceptor ,
152
155
json = json )
153
156
data = r .json ()
154
- if not 200 <= r . status_code < 300 :
157
+ if self . _error ( r ) :
155
158
raise APIError (data ['code' ], data ['message' ])
156
159
return data
157
160
@@ -160,7 +163,7 @@ def text_spellcheck(self, text=None):
160
163
r = requests .post (API_URL + '/text/spellcheck' , auth = self .interceptor ,
161
164
json = json )
162
165
data = r .json ()
163
- if not 200 <= r . status_code < 300 :
166
+ if self . _error ( r ) :
164
167
raise APIError (data ['code' ], data ['message' ])
165
168
return data
166
169
@@ -174,7 +177,7 @@ def text_summary(self, text=None, url=None, language=None, length=None):
174
177
r = requests .post (API_URL + '/text/summary' , auth = self .interceptor ,
175
178
json = json )
176
179
data = r .json ()
177
- if not 200 <= r . status_code < 300 :
180
+ if self . _error ( r ) :
178
181
raise APIError (data ['code' ], data ['message' ])
179
182
return data
180
183
@@ -187,7 +190,7 @@ def webpage_pdf(self, url=None, size=None, layout=None):
187
190
r = requests .post (API_URL + '/webpage/pdf' , auth = self .interceptor ,
188
191
json = json )
189
192
data = r .json ()
190
- if not 200 <= r . status_code < 300 :
193
+ if self . _error ( r ) :
191
194
raise APIError (data ['code' ], data ['message' ])
192
195
return data
193
196
@@ -196,7 +199,7 @@ def word_lookup(self, word=None):
196
199
r = requests .post (API_URL + '/word/lookup' , auth = self .interceptor ,
197
200
json = json )
198
201
data = r .json ()
199
- if not 200 <= r . status_code < 300 :
202
+ if self . _error ( r ) :
200
203
raise APIError (data ['code' ], data ['message' ])
201
204
return data
202
205
0 commit comments