Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8999dda

Browse files
author
Alex Schworer
committed
Change HTTP library from httplib2 to requests.
1 parente65f7b3 commit8999dda

File tree

2 files changed

+47
-78
lines changed

2 files changed

+47
-78
lines changed

‎setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
author_email='alex.schworer@gmail.com',
1111
url='http://github.com/schworer/zencoder-py',
1212
license="MIT License",
13-
install_requires=['httplib2'],
13+
install_requires=['requests>=1.0'],
1414
packages=['zencoder'],
1515
platforms='any',
1616
classifiers=[

‎zencoder/core.py

Lines changed: 46 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
importos
2-
importhttplib2
2+
importrequests
33
fromurllibimporturlencode
44
fromdatetimeimportdatetime
55

@@ -30,37 +30,35 @@ def __init__(self, http_response, content):
3030
self.content=content
3131

3232
classHTTPBackend(object):
33-
"""
34-
Abstracts out an HTTP backend, but defaults to httplib2. Required arguments
35-
are `base_url` and `api_key`.
33+
""" Abstracts out an HTTP backend. Required argument are `base_url` and
34+
`api_key`. """
35+
def__init__(self,
36+
base_url,
37+
api_key,
38+
resource_name=None,
39+
timeout=None,
40+
test=False,
41+
version=None):
3642

37-
.. note::
38-
While `as_xml` is provided as a keyword argument, XML or input or output
39-
is not supported.
40-
"""
41-
def__init__(self,base_url,api_key,as_xml=False,resource_name=None,timeout=None,test=False,version=None):
4243
self.base_url=base_url
44+
4345
ifresource_name:
4446
self.base_url=self.base_url+resource_name
4547

46-
self.http=httplib2.Http(timeout=timeout)
47-
self.as_xml=as_xml
48+
self.http=requests.Session()
49+
50+
self.as_xml=False
4851
self.api_key=api_key
4952
self.test=test
5053
self.version=version
5154

52-
defcontent_length(self,body):
53-
"""
54-
Returns the content length as an int for the given `body` data. Used by
55-
PUT and POST requests to set the Content-Length header.
56-
"""
57-
returnstr(len(body))ifbodyelse"0"
55+
# sets request headers for the entire session
56+
self.http.headers.update(self.headers)
5857

5958
@property
6059
defheaders(self):
61-
""" Returns default headers, by setting the Content-Type and Accepts
62-
headers.
63-
"""
60+
""" Returns default headers, by setting the Content-Type, Accepts,
61+
User-Agent and API Key headers."""
6462
content_type='xml'ifself.as_xmlelse'json'
6563

6664
headers= {
@@ -85,35 +83,14 @@ def encode(self, data):
8583
else:
8684
raiseNotImplementedError('Encoding as XML is not supported.')
8785

88-
defdecode(self,raw_body):
89-
"""
90-
Returns the JSON-encoded `raw_body` and decodes it to a `dict` (using
91-
`json.loads`).
92-
93-
.. note::
94-
Decoding as XML is not supported.
95-
"""
96-
ifnotself.as_xml:
97-
# only parse json when it exists, else just return None
98-
ifnotraw_bodyorraw_body==' ':
99-
returnNone
100-
else:
101-
returnjson.loads(raw_body)
102-
else:
103-
raiseNotImplementedError('Decoding as XML is not supported.')
104-
10586
defdelete(self,url,params=None):
10687
"""
10788
Executes an HTTP DELETE request for the given URL
10889
109-
params should be aurllib.urlencoded string
90+
params should be adictionary
11091
"""
111-
ifparams:
112-
url='?'.join([url,params])
113-
114-
response,content=self.http.request(url,method="DELETE",
115-
headers=self.headers)
116-
returnself.process(response,content)
92+
response=self.http.delete(url,params=params)
93+
returnself.process(response)
11794

11895
defget(self,url,data=None):
11996
"""
@@ -125,66 +102,58 @@ def get(self, url, data=None):
125102
params=urlencode(data)
126103
url='?'.join([url,params])
127104

128-
response,content=self.http.request(url,method="GET",
129-
headers=self.headers)
130-
returnself.process(response,content)
105+
response=self.http.get(url,headers=self.headers,params=data)
106+
returnself.process(response)
131107

132108
defpost(self,url,body=None):
133109
"""
134110
Executes an HTTP POST request for the given URL
135111
"""
136-
headers=self.headers
137-
headers['Content-Length']=self.content_length(body)
138-
response,content=self.http.request(url,method="POST",
139-
body=body,
140-
headers=self.headers)
112+
response=self.http.post(url,data=body,headers=self.headers)
141113

142-
returnself.process(response,content)
114+
returnself.process(response)
143115

144116
defput(self,url,data=None,body=None):
145117
"""
146118
Executes an HTTP PUT request for the given URL
147119
"""
148-
headers=self.headers
149-
headers['Content-Length']=self.content_length(body)
120+
response=self.http.put(url,params=data,data=body,headers=self.headers)
150121

151-
ifdata:
152-
params=urlencode(data)
153-
url='?'.join([url,params])
122+
returnself.process(response)
154123

155-
response,content=self.http.request(url,method="PUT",
156-
body=body,
157-
headers=headers)
158-
159-
returnself.process(response,content)
160-
161-
defprocess(self,http_response,content):
162-
"""
163-
Returns HTTP backend agnostic Response data
164-
"""
124+
defprocess(self,response):
125+
""" Returns HTTP backend agnostic Response data. """
165126

166127
try:
167-
code=http_response.status
168-
body=self.decode(content)
169-
response=Response(code,body,content,http_response)
170-
171-
returnresponse
128+
code=response.status_code
129+
130+
# 204 - No Content
131+
ifcode==204:
132+
body=None
133+
# add an error message to 402 errors
134+
elifcode==402:
135+
body= {
136+
"message":"Payment Required",
137+
"status":"error"
138+
}
139+
else:
140+
body=response.json()
172141

142+
returnResponse(code,body,response.content,response)
173143
exceptValueError:
174-
raiseZencoderResponseError(http_response,content)
144+
raiseZencoderResponseError(response,content)
175145

176146
classZencoder(object):
177147
""" This is the entry point to the Zencoder API """
178148
def__init__(self,api_key=None,api_version=None,as_xml=False,timeout=None,test=False):
179149
"""
180-
Initializes Zencoder. You must have a validAPI_KEY.
150+
Initializes Zencoder. You must have a valid`api_key`.
181151
182152
You can pass in the api_key as an argument, or set
183153
`ZENCODER_API_KEY` as an environment variable, and it will use
184-
that, if api_key is unspecified.
154+
that, if`api_key` is unspecified.
185155
186156
Set api_version='edge' to get the Zencoder development API. (defaults to 'v2')
187-
Set as_xml=True to get back xml data instead of the default json.
188157
"""
189158
ifnotapi_version:
190159
api_version='v2'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp