@@ -12,14 +12,49 @@ class ZencoderError(Exception):
12
12
class HTTPBackend (object ):
13
13
"""
14
14
Abstracts out an HTTP backend, but defaults to httplib2
15
+
16
+ @FIXME: Build in support for supplying arbitrary backends
15
17
"""
16
- def __init__ (self ):
18
+ def __init__ (self , as_xml = False ):
17
19
"""
18
20
Creates an HTTPBackend object, which abstracts out some of the
19
21
library specific HTTP stuff.
20
22
"""
21
23
self .base_url = 'https://app.zencoder.com/api/'
22
24
self .http = httplib2 .Http ()
25
+ self .as_xml = as_xml
26
+
27
+ if self .as_xml :
28
+ self .headers = {}
29
+ else :
30
+ self .headers = {'Content-Type' : 'application/json' ,
31
+ 'Accepts' : 'application/json' }
32
+
33
+ def decode (self , raw_body ):
34
+ """
35
+ Returns the raw_body as json (the default) or XML
36
+ """
37
+ if not self .as_xml :
38
+ return json .loads (raw_body )
39
+
40
+ def post (self , url , body = None ):
41
+ """
42
+ Execute a HTTP POST request for the given URL
43
+ """
44
+ response , content = self .http .request (url , method = "POST" ,
45
+ body = body ,
46
+ headers = self .headers )
47
+
48
+ return self .process (response , content )
49
+
50
+ def process (self , http_response , content ):
51
+ """
52
+ Returns HTTP backend agnostic Response data
53
+ """
54
+ code = http_response .status
55
+ body = self .decode (content )
56
+ response = Response (code , body , content , http_response )
57
+ return response
23
58
24
59
class Zencoder (object ):
25
60
""" This is the entry point to the Zencoder API """
0 commit comments