Add multipart form support. Does not yet handle additional post parameters correctly.
This commit is contained in:
parent
0ef3abe79e
commit
5858043d44
3 changed files with 43 additions and 5 deletions
|
@ -67,7 +67,9 @@ class ApiPhoto:
|
|||
return photo
|
||||
|
||||
def upload(self, photo_file, **kwds):
|
||||
raise NotImplementedError("Use upload_encoded instead.")
|
||||
result = self._client.post("/photo/upload.json", files={'photo': photo_file},
|
||||
**kwds)["result"]
|
||||
return Photo(self._client, result)
|
||||
|
||||
def upload_encoded(self, photo_file, **kwds):
|
||||
""" Base64-encodes and uploads the specified file """
|
||||
|
|
30
openphoto/multipart_post.py
Normal file
30
openphoto/multipart_post.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import mimetypes
|
||||
import mimetools
|
||||
|
||||
def encode_multipart_formdata(params, files):
|
||||
boundary = mimetools.choose_boundary()
|
||||
|
||||
lines = []
|
||||
for name in params:
|
||||
lines.append("--" + boundary)
|
||||
lines.append("Content-Disposition: form-data; name=\"%s\"" % name)
|
||||
lines.append("")
|
||||
lines.append(str(params[name]))
|
||||
for name in files:
|
||||
filename = files[name]
|
||||
content_type, _ = mimetypes.guess_type(filename)
|
||||
if content_type is None:
|
||||
content_type = "application/octet-stream"
|
||||
|
||||
lines.append("--" + boundary)
|
||||
lines.append("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" % (name, filename))
|
||||
lines.append("Content-Type: %s" % content_type)
|
||||
lines.append("")
|
||||
lines.append(open(filename, "rb").read())
|
||||
lines.append("--" + boundary + "--")
|
||||
lines.append("")
|
||||
|
||||
body = "\r\n".join(lines)
|
||||
headers = {'Content-Type': "multipart/form-data; boundary=%s" % boundary,
|
||||
'Content-Length': str(len(body))}
|
||||
return headers, body
|
|
@ -9,6 +9,7 @@ except ImportError:
|
|||
|
||||
from objects import OpenPhotoObject
|
||||
from errors import *
|
||||
from multipart_post import encode_multipart_formdata
|
||||
|
||||
DUPLICATE_RESPONSE = {"code": 409,
|
||||
"message": "This photo already exists"}
|
||||
|
@ -58,7 +59,7 @@ class OpenPhotoHttp:
|
|||
else:
|
||||
return content
|
||||
|
||||
def post(self, endpoint, process_response=True, **params):
|
||||
def post(self, endpoint, process_response=True, files = {}, **params):
|
||||
"""
|
||||
Performs an HTTP POST to the specified endpoint (API path),
|
||||
passing parameters if given.
|
||||
|
@ -74,10 +75,15 @@ class OpenPhotoHttp:
|
|||
|
||||
consumer = oauth.Consumer(self._consumer_key, self._consumer_secret)
|
||||
token = oauth.Token(self._token, self._token_secret)
|
||||
|
||||
client = oauth.Client(consumer, token)
|
||||
body = urllib.urlencode(params)
|
||||
_, content = client.request(url, "POST", body)
|
||||
|
||||
if files:
|
||||
headers, body = encode_multipart_formdata(params, files)
|
||||
else:
|
||||
headers = {}
|
||||
body = urllib.urlencode(params)
|
||||
|
||||
_, content = client.request(url, "POST", body, headers)
|
||||
|
||||
self.last_url = url
|
||||
self.last_params = params
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue