15
15
16
16
from .file import File , LazyFile
17
17
18
- uuid_regex = re .compile (r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}' )
18
+ uuid_regex = re .compile (r'[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12}' )
19
+
19
20
20
21
class UploadCareException (Exception ):
21
- def __init__ (self , message , response = None ):
22
+ def __init__ (self , response , data ):
23
+ message = 'Response status is %i. Data: %s' % (response .status , data )
22
24
super (UploadCareException , self ).__init__ (message )
23
25
self .response = response
26
+ self .data = data
27
+
24
28
25
-
26
29
class UploadCare (object ):
27
30
def __init__ (self , pub_key , secret , timeout = 5 , api_base = "http://api.uploadcare.com/" ):
28
31
self .pub_key = pub_key
@@ -47,23 +50,22 @@ def file(self, file_serialized):
47
50
48
51
if file_serialized .startswith ('http' ):
49
52
f ._cached_url = file_serialized
50
-
53
+
51
54
return f
52
55
53
56
def file_from_url (self , url ):
54
57
data = self .make_request ('POST' , '/files/download/' , {'source_url' : url })
55
58
return LazyFile (data ['id' ], self )
56
59
57
-
58
- def make_request (self , verb , uri , data = None ):
60
+ def make_request (self , verb , uri , data = None ):
59
61
parts = ['' ] + filter (None , self .path .split ('/' ) + uri .split ('/' )) + ['' ]
60
62
uri = '/' .join (parts )
61
63
62
64
content = ''
63
-
65
+
64
66
if data :
65
67
content = json .dumps (data )
66
-
68
+
67
69
content_type = 'application/json'
68
70
content_md5 = hashlib .md5 (content ).hexdigest ()
69
71
date = email .utils .formatdate (usegmt = True )
@@ -77,7 +79,7 @@ def make_request(self, verb, uri, data=None):
77
79
sign = hmac .new (str (self .secret ),
78
80
sign_string ,
79
81
hashlib .sha1 ).hexdigest ()
80
-
82
+
81
83
headers = {
82
84
'Authentication' : 'UploadCare %s:%s' % (self .pub_key , sign ),
83
85
'Date' : date ,
@@ -87,8 +89,6 @@ def make_request(self, verb, uri, data=None):
87
89
con = httplib .HTTPConnection (self .host , self .port , timeout = self .timeout )
88
90
con .request (verb , uri , content , headers )
89
91
90
- # assert False
91
-
92
92
logger .debug ('sent: %s %s %s' % (verb , uri , content ))
93
93
94
94
response = con .getresponse ()
@@ -102,9 +102,4 @@ def make_request(self, verb, uri, data=None):
102
102
if response .status == 204 : # No Content
103
103
return
104
104
105
- raise UploadCareException ('Response status is %i. Data: %s' % (response .status , data ),
106
- response = response )
107
-
108
-
109
-
110
-
105
+ raise UploadCareException (response , data )
0 commit comments