11# -*- coding: utf-8 -*-
22
3- import ConfigParser
4- import cookielib
3+ from six . moves import configparser as ConfigParser
4+ from six . moves import http_cookiejar as cookielib
55import logging
66import mimetypes
77from pprint import pformat
88import random
99import json
10- from urlparse import parse_qs , urlparse
10+ from six . moves . urllib . parse import parse_qs , urlparse
1111
1212import requests
1313from requests_oauthlib import OAuth1
1414
15- __version__ = '0.5.2 '
15+ __version__ = '0.6.0 '
1616
1717REQUEST_TOKEN_URL = 'https://sso.openx.com/api/index/initiate'
1818ACCESS_TOKEN_URL = 'https://sso.openx.com/api/index/token'
@@ -123,9 +123,9 @@ def log_request(self, response):
123123 self .logger .debug ("%s: %s" % (k , v ))
124124 self .logger .debug ('====={0:=<45}' .format ('OX3 api call response body' ))
125125 try :
126- self .logger .debug (pformat (json .loads (response .content )))
126+ self .logger .debug (pformat (json .loads (response .text )))
127127 except ValueError :
128- self .logger .debug ("%s" % response .content )
128+ self .logger .debug ("%s" % response .text )
129129 self .logger .debug ('====={0:=<45}' .format ('OX3 api call finished' ))
130130
131131 def request (self , url , method = 'GET' , headers = None , data = None , sign = False ,
@@ -164,8 +164,8 @@ def fetch_request_token(self):
164164 response = self ._session .post (url = self .request_token_url , auth = oauth , timeout = self .timeout )
165165 self .log_request (response )
166166 if response .status_code != 200 :
167- raise OAuthException ("OAuth token request failed (%s) %s" % (response .status_code , response .content ))
168- credentials = parse_qs (response .content )
167+ raise OAuthException ("OAuth token request failed (%s) %s" % (response .status_code , response .text ))
168+ credentials = parse_qs (response .text )
169169 self ._token = {'key' : credentials ['oauth_token' ][0 ],
170170 'secret' : credentials ['oauth_token_secret' ][0 ]}
171171 return self ._token
@@ -192,10 +192,10 @@ def authorize_token(self, email=None, password=None):
192192 response = self ._session .post (url = self .authorization_url , data = data , timeout = self .timeout )
193193 self .log_request (response )
194194 if response .status_code != 200 :
195- raise OAuthException ("OAuth login failed (%s) %s" % (response .status_code , response .content ))
195+ raise OAuthException ("OAuth login failed (%s) %s" % (response .status_code , response .text ))
196196
197197 # set token verifier
198- self ._token ['verifier' ] = parse_qs (response .content )['oauth_verifier' ][0 ]
198+ self ._token ['verifier' ] = parse_qs (response .text )['oauth_verifier' ][0 ]
199199
200200 def fetch_access_token (self ):
201201 """Helper method to fetch and set access token.
@@ -212,8 +212,8 @@ def fetch_access_token(self):
212212 response = self ._session .post (url = self .access_token_url , auth = oauth , timeout = self .timeout )
213213 self .log_request (response )
214214 if response .status_code != 200 :
215- raise OAuthException ("OAuth token verification failed (%s) %s" % (response .status_code , response .content ))
216- self ._token = parse_qs (response .content )['oauth_token' ][0 ]
215+ raise OAuthException ("OAuth token verification failed (%s) %s" % (response .status_code , response .text ))
216+ self ._token = parse_qs (response .text )['oauth_token' ][0 ]
217217 return self ._token
218218
219219 def validate_session (self ):
@@ -244,7 +244,7 @@ def validate_session(self):
244244 if self .api_path == API_PATH_V1 :
245245 response = self ._session .put (url = self ._resolve_url ('/a/session/validate' ), timeout = self .timeout )
246246 self .log_request (response )
247- return response .content
247+ return response .text
248248
249249 def logon (self , email = None , password = None ):
250250 """Returns self after authentication.
@@ -276,7 +276,7 @@ def logoff(self):
276276
277277 response = self ._session .delete (url = self .access_token_url , auth = oauth , timeout = self .timeout )
278278 if response .status_code != 204 :
279- raise OAuthException ("OAuth token deletion failed (%s) %s" % (response .status_code , response .content ))
279+ raise OAuthException ("OAuth token deletion failed (%s) %s" % (response .status_code , response .text ))
280280 else :
281281 raise UnknownAPIFormatError (
282282 'Unrecognized API path: %s' % self .api_path )
@@ -305,13 +305,13 @@ def _resolve_url(self, url):
305305
306306 def _response_value (self , response ):
307307 """ Utility method. Returns decoded json. If the response content cannot be decoded, then
308- the content is returned.
308+ the text is returned.
309309
310310 """
311311 try :
312312 return response .json ()
313313 except ValueError :
314- return response .content
314+ return response .text
315315
316316 def get (self , url ):
317317 """Issue a GET request to the given URL or API shorthand
@@ -377,7 +377,7 @@ def upload_creative(self, account_id, file_path):
377377 """
378378 # Thanks to nosklo for his answer on SO:
379379 # http://stackoverflow.com/a/681182
380- boundary = '-----------------------------' + str (int (random .random ()* 1e10 ))
380+ boundary = '-----------------------------' + str (int (random .random () * 1e10 ))
381381 parts = []
382382
383383 # Set account ID part.
@@ -392,7 +392,8 @@ def upload_creative(self, account_id, file_path):
392392 parts .append ('Content-Type: %s' % mimetypes .guess_type (file_path )[0 ] or 'application/octet-stream' )
393393 parts .append ('' )
394394 # TODO: catch errors with opening file.
395- parts .append (open (file_path , 'r' ).read ())
395+ with open (file_path , 'r' ) as f :
396+ parts .append (f .read ())
396397
397398 parts .append ('--' + boundary + '--' )
398399 parts .append ('' )
@@ -479,6 +480,7 @@ def client_from_file(file_path='.ox3rc', env=None):
479480
480481 return client
481482
483+
482484# The exposed API has moved to using Client instead of OX3APIClient, but create
483485# a temporary alias for backwards compatibility.
484486OX3APIClient = Client
0 commit comments