@@ -36,7 +36,7 @@ class HTTPBackend(object):
3636
3737 @FIXME: Build in support for supplying arbitrary backends
3838 """
39- def __init__ (self ,base_url ,api_key ,as_xml = False ,resource_name = None ,timeout = None ,test = False ):
39+ def __init__ (self ,base_url ,api_key ,as_xml = False ,resource_name = None ,timeout = None ,test = False , version = None ):
4040"""
4141 Creates an HTTPBackend object, which abstracts out some of the
4242 library specific HTTP stuff.
@@ -49,13 +49,22 @@ def __init__(self, base_url, api_key, as_xml=False, resource_name=None, timeout=
4949self .as_xml = as_xml
5050self .api_key = api_key
5151self .test = test
52+ self .version = version
5253
54+ def content_length (self ,body ):
55+ """
56+ Returns the content length as an int for the given body data
57+ """
58+ return str (len (body ))if body else "0"
59+
60+ @property
61+ def headers (self ):
5362if self .as_xml :
54- self . headers = {'Content-Type' :'application/xml' ,
55- 'Accepts' :'application/xml' }
63+ return {'Content-Type' :'application/xml' ,
64+ 'Accepts' :'application/xml' }
5665else :
57- self . headers = {'Content-Type' :'application/json' ,
58- 'Accepts' :'application/json' }
66+ return {'Content-Type' :'application/json' ,
67+ 'Accepts' :'application/json' }
5968
6069def encode (self ,data ):
6170"""
@@ -111,12 +120,31 @@ def post(self, url, body=None):
111120"""
112121 Executes an HTTP POST request for the given URL
113122 """
123+ headers = self .headers
124+ headers ['Content-Length' ]= self .content_length (body )
114125response ,content = self .http .request (url ,method = "POST" ,
115126body = body ,
116127headers = self .headers )
117128
118129return self .process (response ,content )
119130
131+ def put (self ,url ,data = None ,body = None ):
132+ """
133+ Executes an HTTP PUT request for the given URL
134+ """
135+ headers = self .headers
136+ headers ['Content-Length' ]= self .content_length (body )
137+
138+ if data :
139+ params = urlencode (data )
140+ url = '?' .join ([url ,params ])
141+
142+ response ,content = self .http .request (url ,method = "PUT" ,
143+ body = body ,
144+ headers = headers )
145+
146+ return self .process (response ,content )
147+
120148def process (self ,http_response ,content ):
121149"""
122150 Returns HTTP backend agnostic Response data
@@ -139,7 +167,7 @@ def __init__(self, api_key=None, api_version=None, as_xml=False, timeout=None, t
139167 Initializes Zencoder. You must have a valid API_KEY.
140168
141169 You can pass in the api_key as an argument, or set
142- ' ZENCODER_API_KEY' as an environment variable, and it will use
170+ ` ZENCODER_API_KEY` as an environment variable, and it will use
143171 that, if api_key is unspecified.
144172
145173 Set api_version='edge' to get the Zencoder development API. (defaults to 'v2')
@@ -162,9 +190,12 @@ def __init__(self, api_key=None, api_version=None, as_xml=False, timeout=None, t
162190
163191self .test = test
164192self .as_xml = as_xml
165- self .job = Job (self .base_url ,self .api_key ,self .as_xml ,timeout = timeout ,test = self .test )
166- self .account = Account (self .base_url ,self .api_key ,self .as_xml ,timeout = timeout )
167- self .output = Output (self .base_url ,self .api_key ,self .as_xml ,timeout = timeout )
193+
194+ args = (self .base_url ,self .api_key ,self .as_xml )
195+ kwargs = dict (timeout = timeout ,test = self .test ,version = api_version )
196+ self .job = Job (* args ,** kwargs )
197+ self .account = Account (* args ,** kwargs )
198+ self .output = Output (* args ,** kwargs )
168199
169200class Response (object ):
170201"""
@@ -179,11 +210,12 @@ def __init__(self, code, body, raw_body, raw_response):
179210
180211class Account (HTTPBackend ):
181212""" Account object """
182- def __init__ (self ,base_url , api_key = None , as_xml = False , timeout = None ):
213+ def __init__ (self ,* args , ** kwargs ):
183214"""
184215 Initializes an Account object
185216 """
186- super (Account ,self ).__init__ (base_url ,api_key ,as_xml ,'account' ,timeout = timeout )
217+ kwargs ['resource_name' ]= 'account'
218+ super (Account ,self ).__init__ (* args ,** kwargs )
187219
188220def create (self ,email ,tos = 1 ,options = None ):
189221"""
@@ -214,19 +246,20 @@ def integration(self):
214246
215247def live (self ):
216248"""
217- Puts your account into live mode."
249+ Puts your account into live mode.
218250 """
219251data = {'api_key' :self .api_key }
220252
221253return self .get (self .base_url + '/live' ,data = data )
222254
223255class Output (HTTPBackend ):
224256""" Gets information regarding outputs """
225- def __init__ (self ,base_url , api_key , as_xml = False , timeout = None ):
257+ def __init__ (self ,* args , ** kwargs ):
226258"""
227259 Contains all API methods relating to Outputs.
228260 """
229- super (Output ,self ).__init__ (base_url ,api_key ,as_xml ,'outputs' ,timeout = timeout )
261+ kwargs ['resource_name' ]= 'outputs'
262+ super (Output ,self ).__init__ (* args ,** kwargs )
230263
231264def progress (self ,output_id ):
232265"""
@@ -248,15 +281,16 @@ class Job(HTTPBackend):
248281"""
249282 Contains all API methods relating to transcoding Jobs.
250283 """
251- def __init__ (self ,base_url , api_key , as_xml = False , timeout = None , test = False ):
284+ def __init__ (self ,* args , ** kwargs ):
252285"""
253- Initialize a job object
286+ Initializes a job object
254287 """
255- super (Job ,self ).__init__ (base_url ,api_key ,as_xml ,'jobs' ,timeout = timeout ,test = test )
288+ kwargs ['resource_name' ]= 'jobs'
289+ super (Job ,self ).__init__ (* args ,** kwargs )
256290
257291def create (self ,input ,outputs = None ,options = None ):
258292"""
259- Create a job
293+ Creates a job
260294
261295 @param input: the input url as string
262296 @param outputs: a list of output dictionaries
@@ -275,7 +309,10 @@ def create(self, input, outputs=None, options=None):
275309
276310def list (self ,page = 1 ,per_page = 50 ):
277311"""
278- List some jobs
312+ Lists some jobs.
313+
314+ @param page: <int> the page of results to return
315+ @param per_page: <int> the number of results per page
279316 """
280317data = {"api_key" :self .api_key ,
281318"page" :page ,
@@ -295,23 +332,31 @@ def progress(self, job_id):
295332
296333def resubmit (self ,job_id ):
297334"""
298- Resubmitsa job
335+ Resubmitsthe given `job_id`
299336 """
300337data = {'api_key' :self .api_key }
301- return self .get (self .base_url + '/%s/resubmit' % str (job_id ),data = data )
338+ url = self .base_url + '/%s/resubmit' % str (job_id )
339+ return self .put (url ,data = data )
302340
303341def cancel (self ,job_id ):
304342"""
305- Cancelsa job
343+ Cancelsthe given `job_id`
306344 """
345+ if self .version == 'v1' :
346+ verb = self .get
347+ else :
348+ verb = self .put
349+
307350data = {'api_key' :self .api_key }
308- return self .get (self .base_url + '/%s/cancel' % str (job_id ),data = data )
351+ url = self .base_url + '/%s/cancel' % str (job_id )
352+ return verb (url ,data = data )
309353
310354def delete (self ,job_id ):
311355"""
312- Deletes a job
356+ Deletes the given `job_id`
357+
358+ WARNING: This method is aliased to `Job.cancel` -- it is deprecated in
359+ API version 2 and greater.
313360 """
314- data = {'api_key' :self .api_key }
315- return self .delete (self .base_url + '/%s' % str (job_id ),
316- params = urlencode (data ))
361+ return self .cancel (job_id )
317362