Skip to content

Commit cf62596

Browse files
committed
Merge branch 'release-0.2.1'
2 parents 9e654f3 + 0b1d66a commit cf62596

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.2.0 / 2012-09-25
2+
==================
3+
# Added: `upload_creative` to support uploading creative files.
4+
15
0.2.0 / 2012-08-29
26
==================
37

ox3apiclient/__init__.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import ConfigParser
44
import cookielib
5+
import mimetypes
6+
import random
57

68
# json module is not supported in versions of Python < 2.6 so try to load the
79
# simplejson module instead. Note that as of simplejson v2.1.1, Python 2.4
@@ -24,7 +26,7 @@
2426

2527
import urlparse
2628

27-
__version__ = '0.2.0'
29+
__version__ = '0.2.1'
2830

2931
REQUEST_TOKEN_URL = 'https://sso.openx.com/api/index/initiate'
3032
ACCESS_TOKEN_URL = 'https://sso.openx.com/api/index/token'
@@ -296,6 +298,40 @@ def delete(self, url):
296298
return json.loads('[]')
297299
return json.loads(res.read())
298300

301+
def upload_creative(self, account_id, file_path):
302+
""""""
303+
# Thanks to nosklo for his answer on SO:
304+
# http://stackoverflow.com/a/681182
305+
boundary = '-----------------------------' + str(int(random.random()*1e10))
306+
parts = []
307+
308+
# Set account ID part.
309+
parts.append('--' + boundary)
310+
parts.append('Content-Disposition: form-data; name="account_id"')
311+
parts.append('')
312+
parts.append(str(account_id))
313+
314+
# Set creative contents part.
315+
parts.append('--' + boundary)
316+
parts.append('Content-Disposition: form-data; name="userfile"; filename="%s"' % file_path)
317+
parts.append('Content-Type: %s' % mimetypes.guess_type(file_path)[0] or 'application/octet-stream')
318+
parts.append('')
319+
# TODO: catch errors with opening file.
320+
parts.append(open(file_path, 'r').read())
321+
322+
parts.append('--' + boundary + '--')
323+
parts.append('')
324+
325+
body = '\r\n'.join(parts)
326+
327+
# TODO: refactor Client.request.
328+
# TODO: Catch errors in attempt to upload.
329+
headers = {'content-type': 'multipart/form-data; boundary=' + boundary}
330+
url = self._resolve_url('/a/creative/uploadcreative')
331+
req = urllib2.Request(url, headers=headers, data=body)
332+
res = urllib2.urlopen(req)
333+
334+
return json.loads(res.read())
299335

300336
def client_from_file(file_path='.ox3rc', env=None):
301337
"""Return an instance of ox3apiclient.Client with data from file_path.

0 commit comments

Comments
 (0)