1
1
import requests
2
- from . hub import _Hub
3
- from . jet import _Jet
2
+
3
+ API_URL = 'https://api.labstack.com'
4
4
5
5
class _Interceptor (requests .auth .AuthBase ):
6
6
def __init__ (self , api_key ):
7
7
self .api_key = api_key
8
8
9
9
def __call__ (self , r ):
10
10
r .headers ['Authorization' ] = 'Bearer ' + self .api_key
11
- r .headers ['Content-Type' ] = 'application/json; charset=utf-8'
12
11
return r
13
12
14
13
class Client ():
15
- def __init__ (self , account_id , api_key ):
16
- self .account_id = account_id
14
+ def __init__ (self , api_key ):
17
15
self .api_key = api_key
18
16
self .interceptor = _Interceptor (api_key )
19
17
20
- def hub (self , client_id ):
21
- return _Hub (self .account_id , self .api_key , client_id )
18
+ def image_compress (self , file = None ):
19
+ files = {'file' : open (file , 'rb' )}
20
+ r = requests .post ('{}/image/compress' .format (API_URL ), auth = self .interceptor , files = files )
21
+ data = r .json ()
22
+ if not 200 <= r .status_code < 300 :
23
+ raise APIError (data ['code' ], data ['message' ])
24
+ return data
25
+
26
+ def image_resize (self , file = None , width = None , height = None , crop = None ):
27
+ files = {'file' : open (file , 'rb' )}
28
+ data = {
29
+ 'width' : width ,
30
+ 'height' : height ,
31
+ 'crop' : crop
32
+ }
33
+ r = requests .post ('{}/image/resize' .format (API_URL ), auth = self .interceptor ,
34
+ files = files , data = data )
35
+ data = r .json ()
36
+ if not 200 <= r .status_code < 300 :
37
+ raise APIError (data ['code' ], data ['message' ])
38
+ return data
22
39
23
- def jet (self ):
24
- return _Jet (self .interceptor )
25
-
40
+ class APIError (Exception ):
41
+ def __init__ (self , code , message ):
42
+ self .code = code
43
+ self .message = message
44
+
45
+ def __str__ (self ):
46
+ return self .messag
0 commit comments