1
1
# -*- coding: utf-8 -*-
2
2
3
- import ConfigParser
4
- import cookielib
3
+ from six . moves import configparser as ConfigParser
4
+ from six . moves import http_cookiejar as cookielib
5
5
import logging
6
6
import mimetypes
7
7
from pprint import pformat
8
8
import random
9
9
import json
10
- from urlparse import parse_qs , urlparse
10
+ from six . moves . urllib . parse import parse_qs , urlparse
11
11
12
12
import requests
13
13
from requests_oauthlib import OAuth1
14
14
15
- __version__ = '0.5.2 '
15
+ __version__ = '0.6.0 '
16
16
17
17
REQUEST_TOKEN_URL = 'https://sso.openx.com/api/index/initiate'
18
18
ACCESS_TOKEN_URL = 'https://sso.openx.com/api/index/token'
@@ -123,9 +123,9 @@ def log_request(self, response):
123
123
self .logger .debug ("%s: %s" % (k , v ))
124
124
self .logger .debug ('====={0:=<45}' .format ('OX3 api call response body' ))
125
125
try :
126
- self .logger .debug (pformat (json .loads (response .content )))
126
+ self .logger .debug (pformat (json .loads (response .text )))
127
127
except ValueError :
128
- self .logger .debug ("%s" % response .content )
128
+ self .logger .debug ("%s" % response .text )
129
129
self .logger .debug ('====={0:=<45}' .format ('OX3 api call finished' ))
130
130
131
131
def request (self , url , method = 'GET' , headers = None , data = None , sign = False ,
@@ -164,8 +164,8 @@ def fetch_request_token(self):
164
164
response = self ._session .post (url = self .request_token_url , auth = oauth , timeout = self .timeout )
165
165
self .log_request (response )
166
166
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 )
169
169
self ._token = {'key' : credentials ['oauth_token' ][0 ],
170
170
'secret' : credentials ['oauth_token_secret' ][0 ]}
171
171
return self ._token
@@ -192,10 +192,10 @@ def authorize_token(self, email=None, password=None):
192
192
response = self ._session .post (url = self .authorization_url , data = data , timeout = self .timeout )
193
193
self .log_request (response )
194
194
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 ))
196
196
197
197
# 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 ]
199
199
200
200
def fetch_access_token (self ):
201
201
"""Helper method to fetch and set access token.
@@ -212,8 +212,8 @@ def fetch_access_token(self):
212
212
response = self ._session .post (url = self .access_token_url , auth = oauth , timeout = self .timeout )
213
213
self .log_request (response )
214
214
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 ]
217
217
return self ._token
218
218
219
219
def validate_session (self ):
@@ -244,7 +244,7 @@ def validate_session(self):
244
244
if self .api_path == API_PATH_V1 :
245
245
response = self ._session .put (url = self ._resolve_url ('/a/session/validate' ), timeout = self .timeout )
246
246
self .log_request (response )
247
- return response .content
247
+ return response .text
248
248
249
249
def logon (self , email = None , password = None ):
250
250
"""Returns self after authentication.
@@ -276,7 +276,7 @@ def logoff(self):
276
276
277
277
response = self ._session .delete (url = self .access_token_url , auth = oauth , timeout = self .timeout )
278
278
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 ))
280
280
else :
281
281
raise UnknownAPIFormatError (
282
282
'Unrecognized API path: %s' % self .api_path )
@@ -305,13 +305,13 @@ def _resolve_url(self, url):
305
305
306
306
def _response_value (self , response ):
307
307
""" Utility method. Returns decoded json. If the response content cannot be decoded, then
308
- the content is returned.
308
+ the text is returned.
309
309
310
310
"""
311
311
try :
312
312
return response .json ()
313
313
except ValueError :
314
- return response .content
314
+ return response .text
315
315
316
316
def get (self , url ):
317
317
"""Issue a GET request to the given URL or API shorthand
@@ -377,7 +377,7 @@ def upload_creative(self, account_id, file_path):
377
377
"""
378
378
# Thanks to nosklo for his answer on SO:
379
379
# http://stackoverflow.com/a/681182
380
- boundary = '-----------------------------' + str (int (random .random ()* 1e10 ))
380
+ boundary = '-----------------------------' + str (int (random .random () * 1e10 ))
381
381
parts = []
382
382
383
383
# Set account ID part.
@@ -392,7 +392,8 @@ def upload_creative(self, account_id, file_path):
392
392
parts .append ('Content-Type: %s' % mimetypes .guess_type (file_path )[0 ] or 'application/octet-stream' )
393
393
parts .append ('' )
394
394
# 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 ())
396
397
397
398
parts .append ('--' + boundary + '--' )
398
399
parts .append ('' )
@@ -479,6 +480,7 @@ def client_from_file(file_path='.ox3rc', env=None):
479
480
480
481
return client
481
482
483
+
482
484
# The exposed API has moved to using Client instead of OX3APIClient, but create
483
485
# a temporary alias for backwards compatibility.
484
486
OX3APIClient = Client
0 commit comments