Revision: 7339 Author: russblau Date: 2009-09-30 01:23:04 +0000 (Wed, 30 Sep 2009)
Log Message: ----------- Add ability to assemble multipart/form-data messages; will be needed for implementation of upload api, but for now is disabled.
Modified Paths: -------------- branches/rewrite/pywikibot/data/api.py
Modified: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py 2009-09-29 20:33:35 UTC (rev 7338) +++ branches/rewrite/pywikibot/data/api.py 2009-09-30 01:23:04 UTC (rev 7339) @@ -186,6 +186,8 @@
""" from pywikibot.comms import http + from email.mime.multipart import MIMEMultipart + from email.mime.nonmultipart import MIMENonMultipart
params = self.http_params() if self.site._loginstatus == -3: @@ -197,6 +199,7 @@ "protect", "block", "unblock" ) self.site.throttle(write=write) + mime = False # debugging uri = self.site.scriptpath() + "/api.php" try: ssl = False @@ -205,10 +208,30 @@ ssl = True elif config.use_SSL_always: ssl = True - rawdata = http.request(self.site, uri, ssl, method="POST", - headers={'Content-Type': - 'application/x-www-form-urlencoded'}, - body=params) + if mime: + global container + # construct a MIME message containing all API key/values + container = MIMEMultipart(_subtype='form-data') + for key in self.params: + submsg = MIMENonMultipart("text", "plain") + submsg.add_header("Content-disposition", "form-data", + name=key) + submsg.set_payload(self.params[key]) + container.attach(submsg) + # strip the headers to get the HTTP message body + body = container.as_string() + marker = "\n\n" # separates headers from body + eoh = body.find(marker) + body = body[ eoh + len(marker): ] + # retrieve the headers from the MIME object + mimehead = dict(container.items()) + rawdata = http.request(self.site, uri, ssl, method="POST", + headers=mimehead, body=body) + else: + rawdata = http.request(self.site, uri, ssl, method="POST", + headers={'Content-Type': + 'application/x-www-form-urlencoded'}, + body=params) except Server504Error: logger.debug(u"Caught 504 error") raise