Skip to content

Commit 393ae40

Browse files
rowedonaldeFederico Delgado
authored andcommitted
Fixed validation for v2. Drew a distinction
between using API v2 and sending JSON blobs in PUT and POST requests since you still need to use the default form when validating in v2.
1 parent 6dfac5b commit 393ae40

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

ox3apiclient/__init__.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
API_PATH_V1 = '/ox/3.0'
4242
API_PATH_V2 = '/ox/4.0'
4343
ACCEPTABLE_PATHS = (API_PATH_V1, API_PATH_V2)
44+
JSON_PATHS = (API_PATH_V2,)
4445
HTTP_METHOD_OVERRIDES = ['DELETE', 'PUT']
4546

4647
class UnknownAPIFormatError(ValueError):
@@ -149,18 +150,18 @@ def _sign_request(self, req):
149150
return \
150151
urllib2.Request(req.get_full_url(), headers=req.headers, data=data)
151152

152-
def request(self, url, method='GET', headers={}, data=None, sign=False):
153+
def request(self, url, method='GET', headers={}, data=None, sign=False,
154+
send_json=False):
153155
"""Helper method to make a (optionally OAuth signed) HTTP request."""
154156

155157
# Since we are using a urllib2.Request object we need to assign a value
156158
# other than None to "data" in order to make the request a POST request,
157159
# even if there is no data to post.
158-
if method == 'POST' and not data:
160+
if method in ('POST', 'PUT') and not data:
159161
data = ''
160162

161-
# If it's a POST or PUT request for v2, set the header
162-
# to handle JSON:
163-
if method in ('POST', 'PUT') and self.api_path == API_PATH_V2:
163+
# If we're sending a JSON blob, we need to specify the header:
164+
if method in ('POST', 'PUT') and send_json:
164165
headers['Content-Type'] = 'application/json'
165166

166167
req = urllib2.Request(url, headers=headers, data=data)
@@ -179,13 +180,10 @@ def request(self, url, method='GET', headers={}, data=None, sign=False):
179180
data_utf8 = req.get_data()
180181
for i in data_utf8:
181182
data_utf8[i] = data_utf8[i].encode('utf-8')
182-
if self.api_path == API_PATH_V1:
183-
req.add_data(urllib.urlencode(data_utf8))
184-
elif self.api_path == API_PATH_V2:
183+
if send_json:
185184
req.add_data(json.dumps(data_utf8))
186185
else:
187-
raise UnknownAPIFormatError(
188-
'Unrecognized API path: %s' % self.api_path)
186+
req.add_data(urllib.urlencode(data_utf8))
189187

190188
# In 2.4 and 2.5, urllib2 throws errors for all non 200 status codes.
191189
# The OpenX API uses 201 create responses and 204 for delete respones.
@@ -280,20 +278,15 @@ def validate_session(self):
280278

281279
self._cookie_jar.set_cookie(cookie)
282280

281+
# v2 doesn't need this extra step, just the cookie:
283282
if self.api_path == API_PATH_V1:
284283
url_format = '%s://%s%s/a/session/validate'
285-
elif self.api_path == API_PATH_V2:
286-
url_format = '%s://%s%s/session/validate'
287-
else:
288-
raise UnknownAPIFormatError(
289-
'Unrecognized API path: %s' % self.api_path)
290-
291-
url = url_format % (self.scheme,
292-
self.domain,
293-
self.api_path)
284+
url = url_format % (self.scheme,
285+
self.domain,
286+
self.api_path)
294287

295-
res = self.request(url=url, method='PUT')
296-
return res.read()
288+
res = self.request(url=url, method='PUT')
289+
return res.read()
297290

298291
def logon(self, email=None, password=None):
299292
"""Returns self after authentication.
@@ -346,12 +339,14 @@ def get(self, url):
346339

347340
def put(self, url, data=None):
348341
"""Issue a PUT request to url with the data."""
349-
res = self.request(self._resolve_url(url), method='PUT', data=data)
342+
res = self.request(self._resolve_url(url), method='PUT', data=data,
343+
send_json=(self.api_path in JSON_PATHS))
350344
return json.loads(res.read())
351345

352346
def post(self, url, data=None):
353347
""""""
354-
res = self.request(self._resolve_url(url), method='POST', data=data)
348+
res = self.request(self._resolve_url(url), method='POST', data=data,
349+
send_json=(self.api_path in JSON_PATHS))
355350
return json.loads(res.read())
356351

357352
def delete(self, url):

0 commit comments

Comments
 (0)