def post_mime(url, httplib=default_httplib, timeout=DEFAULT_TIMEOUT, retries=5, **kwargs): body = [] crlf = '\r\n' boundary = "graphBoundary" # UTF8 params utf8_kwargs = dict([(k, v.encode('UTF-8')) for (k,v) in kwargs.iteritems() if k != 'file' and v is not None]) # Add args for (k,v) in utf8_kwargs.iteritems(): body.append("--"+boundary) body.append('Content-Disposition: form-data; name="%s"' % k) body.append('') body.append(str(v)) # Add raw data file = kwargs.get('file') if file: file.open() data = file.read() file.close() body.append("--"+boundary) body.append('Content-Disposition: form-data; filename="facegraphfile.png"') body.append('') body.append(data) body.append("--"+boundary+"--") body.append('') body = crlf.join(body) # Post to server kwargs = {} if timeout: kwargs = {'timeout': timeout} r = httplib.HTTPSConnection(get_host(url), **kwargs) headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundary, 'Content-Length': str(len(body)), 'MIME-Version': '1.0'} r.request('POST', get_path(url).encode(), body, headers) attempt = 0 while True: try: response = r.getresponse().read() return json.loads(response) except JSONDecodeError, e: if len(e.doc) == 0: raise EmptyStringReturnedException(str(e)) else: raise WrappedJSONDecodeError(response, e) except (httplib.BadStatusLine, IOError): if attempt < retries: attempt += 1 else: raise
def post(self, **params): """ POST to this URL (with parameters); return the JSON-decoded result. Example: >>> Graph('ACCESS TOKEN').me.feed.post(message="Test.") Node({'id': '...'}) Some methods allow file attachments so uses MIME request to send those through. Must pass in a file object as 'file' """ if self.access_token: params['access_token'] = self.access_token if get_path(self.url).split('/')[-1] in ['photos']: params['timeout'] = self.timeout params['httplib'] = self.httplib fetch = partial(self.post_mime, self.url, httplib=self.httplib, retries=self.retries, **params) else: params = dict([(k, v.encode('UTF-8')) for (k,v) in params.iteritems() if v is not None]) fetch = partial(self.fetch, self.url, urllib2=self.urllib2, httplib=self.httplib, timeout=self.timeout, retries=self.retries, data=urllib.urlencode(params)) data = json.loads(fetch()) return self.node(data, params, "post")
def post(self, **params): """ POST to this URL (with parameters); return the JSON-decoded result. Example: >>> Graph('ACCESS TOKEN').me.feed.post(message="Test.") Node({'id': '...'}) Some methods allow file attachments so uses MIME request to send those through. Must pass in a file object as 'file' """ if self.access_token: params['access_token'] = self.access_token if get_path(self.url).split('/')[-1] in ['photos']: params['timeout'] = self.timeout params['httplib'] = self.httplib fetch = partial(self.post_mime, self.url, httplib=self.httplib, retries=self.retries, **params) else: params = dict([(k, v.encode('UTF-8')) for (k, v) in params.iteritems() if v is not None]) fetch = partial(self.fetch, self.url, urllib2=self.urllib2, httplib=self.httplib, timeout=self.timeout, retries=self.retries, data=urllib.urlencode(params)) data = json.loads(fetch()) return self.process_response(data, params, "post")
def test_get_path(self): self.assertEquals('', ops.get_path(u'http://a.com')) self.assertEquals('/', ops.get_path(u'http://a.com/')) self.assertEquals('/a', ops.get_path(u'http://a.com/a')) self.assertEquals('/a/', ops.get_path(u'http://a.com/a/')) self.assertEquals('/a/b', ops.get_path(u'http://a.com/a/b'))
def post_mime(url, httplib=default_httplib, timeout=DEFAULT_TIMEOUT, retries=5, **kwargs): body = [] crlf = '\r\n' boundary = "graphBoundary" # UTF8 params utf8_kwargs = dict([(k, v.encode('UTF-8')) for (k, v) in kwargs.iteritems() if k != 'file' and v is not None]) # Add args for (k, v) in utf8_kwargs.iteritems(): body.append("--" + boundary) body.append('Content-Disposition: form-data; name="%s"' % k) body.append('') body.append(str(v)) # Add raw data file = kwargs.get('file') if file: file.open() data = file.read() file.close() body.append("--" + boundary) body.append( 'Content-Disposition: form-data; filename="facegraphfile.png"') body.append('') body.append(data) body.append("--" + boundary + "--") body.append('') body = crlf.join(body) # Post to server kwargs = {} if timeout: kwargs = {'timeout': timeout} r = httplib.HTTPSConnection(get_host(url), **kwargs) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary, 'Content-Length': str(len(body)), 'MIME-Version': '1.0' } r.request('POST', get_path(url).encode(), body, headers) attempt = 0 while True: try: return r.getresponse().read() except (httplib.BadStatusLine, IOError): if attempt < retries: attempt += 1 else: raise finally: r.close()