1
- """
2
- Main Zencoder module
3
- """
4
-
5
1
import os
6
2
import httplib2
7
3
from urllib import urlencode
@@ -32,15 +28,14 @@ def __init__(self, http_response, content):
32
28
33
29
class HTTPBackend (object ):
34
30
"""
35
- Abstracts out an HTTP backend, but defaults to httplib2
31
+ Abstracts out an HTTP backend, but defaults to httplib2. Required arguments
32
+ are `base_url` and `api_key`.
36
33
37
- @FIXME: Build in support for supplying arbitrary backends
34
+ .. note::
35
+ While `as_xml` is provided as a keyword argument, XML or input or output
36
+ is not supported.
38
37
"""
39
38
def __init__ (self , base_url , api_key , as_xml = False , resource_name = None , timeout = None , test = False , version = None ):
40
- """
41
- Creates an HTTPBackend object, which abstracts out some of the
42
- library specific HTTP stuff.
43
- """
44
39
self .base_url = base_url
45
40
if resource_name :
46
41
self .base_url = self .base_url + resource_name
@@ -53,12 +48,16 @@ def __init__(self, base_url, api_key, as_xml=False, resource_name=None, timeout=
53
48
54
49
def content_length (self , body ):
55
50
"""
56
- Returns the content length as an int for the given body data
51
+ Returns the content length as an int for the given `body` data. Used by
52
+ PUT and POST requests to set the Content-Length header.
57
53
"""
58
54
return str (len (body )) if body else "0"
59
55
60
56
@property
61
57
def headers (self ):
58
+ """ Returns default headers, by setting the Content-Type and Accepts
59
+ headers.
60
+ """
62
61
if self .as_xml :
63
62
return {'Content-Type' : 'application/xml' ,
64
63
'Accepts' : 'application/xml' }
@@ -68,8 +67,11 @@ def headers(self):
68
67
69
68
def encode (self , data ):
70
69
"""
71
- Encodes data as either JSON or XML, so that it can be passed onto
72
- the Zencoder API
70
+ Encodes data as JSON (by calling `json.dumps`), so that it can be
71
+ passed onto the Zencoder API.
72
+
73
+ .. note::
74
+ Encoding as XML is not supported.
73
75
"""
74
76
if not self .as_xml :
75
77
return json .dumps (data )
@@ -78,7 +80,11 @@ def encode(self, data):
78
80
79
81
def decode (self , raw_body ):
80
82
"""
81
- Returns the raw_body as json (the default) or XML
83
+ Returns the JSON-encoded `raw_body` and decodes it to a `dict` (using
84
+ `json.loads`).
85
+
86
+ .. note::
87
+ Decoding as XML is not supported.
82
88
"""
83
89
if not self .as_xml :
84
90
# only parse json when it exists, else just return None
0 commit comments