def post(path, base=URL_BASE, params_dict={}): app_logger.debug("post: base={0} path={1} payload={2}".format(base, path, forms.form_encode(params_dict))) resource = create_resource() response = exec_request(partial(resource.post, path=path, payload=forms.form_encode(params_dict))) resp = response.body_string() if response.charset != 'utf8': resp = resp.decode('utf8') return resp
def _create_repository(self, username, password, repo_name): '''Makes a REST call to Bitbucket to create the new remote repository before we can push.''' try: resp = request('https://api.bitbucket.org/1.0/repositories/', method='POST', body=form_encode({'name': repo_name}), filters=[BasicAuth(username, password)], headers={ 'Content-Type': 'application/x-www-form-urlencoded' }, follow_redirect=True) if resp.status_int == 401: return CREATE_RESULT.UNAUTHORIZED elif resp.status_int == 400: return CREATE_RESULT.BAD_REQUEST elif (resp.status_int // 100) != 2: print 'Call failed:', resp.status_int, resp.body_string() return CREATE_RESULT.REMOTE_ERROR else: return CREATE_RESULT.OK except: type_, message, tb = sys.exc_info() print type_, message print util.traceback_to_str(tb) return CREATE_RESULT.UNKNOWN
def _set_body(self, body): ctype = self.headers.ipop('content-type', None) clen = self.headers.ipop('content-length', None) if isinstance(body, dict): if ctype is not None and \ ctype.startswith("multipart/form-data"): type_, opts = cgi.parse_header(ctype) boundary = opts.get('boundary', uuid.uuid4().hex) self._body, self.headers = multipart_form_encode(body, self.headers, boundary) # at this point content-type is "multipart/form-data" # we need to set the content type according to the # correct boundary like # "multipart/form-data; boundary=%s" % boundary ctype = self.headers.ipop('content-type', None) else: ctype = "application/x-www-form-urlencoded; charset=utf-8" self._body = form_encode(body) elif hasattr(body, "boundary") and hasattr(body, "get_size"): ctype = "multipart/form-data; boundary=%s" % body.boundary clen = body.get_size() self._body = body else: self._body = body if not ctype: ctype = 'application/octet-stream' if hasattr(self.body, 'name'): ctype = mimetypes.guess_type(body.name)[0] if not clen: if hasattr(self._body, 'fileno'): try: self._body.flush() except IOError: pass try: fno = self._body.fileno() clen = str(os.fstat(fno)[6]) except IOError: if not self.is_chunked(): clen = len(self._body.read()) elif hasattr(self._body, 'getvalue') and not \ self.is_chunked(): clen = len(self._body.getvalue()) elif isinstance(self._body, types.StringTypes): self._body = to_bytestring(self._body) clen = len(self._body) if clen is not None: self.headers['Content-Length'] = clen # TODO: maybe it's more relevant # to check if Content-Type is already set in self.headers # before overiding it if ctype is not None: self.headers['Content-Type'] = ctype
def set_body(self, body, headers, chunked=False): """ set HTTP body and manage form if needed """ content_type = headers.get('CONTENT-TYPE') content_length = headers.get('CONTENT-LENGTH') if not body: if content_type is not None: self.headers.append(('Content-Type', content_type)) if self.method in ('POST', 'PUT'): self.headers.append(("Content-Length", "0")) return # set content lengh if needed if isinstance(body, dict): if content_type is not None and \ content_type.startswith("multipart/form-data"): type_, opts = cgi.parse_header(content_type) boundary = opts.get('boundary', uuid.uuid4().hex) body, self.headers = multipart_form_encode(body, self.headers, boundary) else: content_type = "application/x-www-form-urlencoded; charset=utf-8" body = form_encode(body) elif hasattr(body, "boundary"): content_type = "multipart/form-data; boundary=%s" % body.boundary content_length = body.get_size() if not content_type: content_type = 'application/octet-stream' if hasattr(body, 'name'): content_type = mimetypes.guess_type(body.name)[0] if not content_length: if hasattr(body, 'fileno'): try: body.flush() except IOError: pass content_length = str(os.fstat(body.fileno())[6]) elif hasattr(body, 'getvalue'): try: content_length = str(len(body.getvalue())) except AttributeError: pass elif isinstance(body, types.StringTypes): body = util.to_bytestring(body) content_length = len(body) if content_length: self.headers.append(("Content-Length", content_length)) if content_type is not None: self.headers.append(('Content-Type', content_type)) elif not chunked: raise RequestError("Can't determine content length and " + "Transfer-Encoding header is not chunked") self.body = body
def set_body(self, body, headers, chunked=False): """ set HTTP body and manage form if needed """ content_type = headers.get('CONTENT-TYPE') content_length = headers.get('CONTENT-LENGTH') if not body: if content_type is not None: self.bheaders.append(('Content-Type', content_type)) if self.method in ('POST', 'PUT'): self.bheaders.append(("Content-Length", "0")) return # set content lengh if needed if isinstance(body, dict): if content_type is not None and \ content_type.startswith("multipart/form-data"): type_, opts = cgi.parse_header(content_type) boundary = opts.get('boundary', uuid.uuid4().hex) body, self.bheaders = multipart_form_encode(body, self.headers, boundary) else: content_type = "application/x-www-form-urlencoded; charset=utf-8" body = form_encode(body) elif hasattr(body, "boundary"): content_type = "multipart/form-data; boundary=%s" % body.boundary content_length = body.get_size() if not content_type: content_type = 'application/octet-stream' if hasattr(body, 'name'): content_type = mimetypes.guess_type(body.name)[0] if not content_length: if hasattr(body, 'fileno'): try: body.flush() except IOError: pass content_length = str(os.fstat(body.fileno())[6]) elif hasattr(body, 'getvalue'): try: content_length = str(len(body.getvalue())) except AttributeError: pass elif isinstance(body, types.StringTypes): body = util.to_bytestring(body) content_length = len(body) if content_length: self.bheaders.append(("Content-Length", content_length)) if content_type is not None: self.bheaders.append(('Content-Type', content_type)) elif not chunked: raise RequestError("Can't determine content length and " + "Transfer-Encoding header is not chunked") self.body = body
def request(self, method, path=None, payload=None, headers=None, **params): """ HTTP request This method may be the only one you want to override when subclassing `restkit.rest.Resource`. :param payload: string or File object passed to the body of the request :param path: string additionnal path to the uri :param headers: dict, optionnal headers that will be added to HTTP request. :param params: Optionnal parameterss added to the request """ headers = headers or {} headers.update(self._headers.copy()) self._body_parts = [] if payload is not None: if isinstance(payload, dict): ctype = headers.get('Content-Type') if ctype is not None and \ ctype.startswith("multipart/form-data"): type_, opts = cgi.parse_header(ctype) boundary = opts.get('boundary', uuid.uuid4().hex) payload, headers = multipart_form_encode(payload, headers, boundary) else: ctype = "application/x-www-form-urlencoded; charset=utf-8" headers['Content-Type'] = ctype payload = form_encode(payload) headers['Content-Length'] = len(payload) elif isinstance(payload, MultipartForm): ctype = "multipart/form-data; boundary=%s" % payload.boundary headers['Content-Type'] = ctype headers['Content-Length'] = str(payload.get_size()) if 'Content-Type' not in headers: ctype = 'application/octet-stream' if hasattr(payload, 'name'): ctype = mimetypes.guess_type(payload.name)[0] headers['Content-Type'] = ctype uri = self._make_uri(self.uri, path, **params) try: resp = self.do_request(uri, method=method, payload=payload, headers=headers) except ParserError: raise except Exception, e: raise RequestError(e)
def _post(self, properties={}, **args): form = {} i = 0 for k, v in properties.items(): form['propertyId[%d]' % i] = k form['propertyValue[%d]' % i] = v i += 1 for k, v in args.items(): form[k] = v payload = form_encode(form) return self.repo.client._post(self.repo.root, payload)
def _set_body(self, body): ctype = self.headers.ipop('content-type', None) clen = self.headers.ipop('content-length', None) if isinstance(body, dict): if ctype is not None and \ ctype.startswith("multipart/form-data"): type_, opts = cgi.parse_header(ctype) boundary = opts.get('boundary', uuid.uuid4().hex) self._body, self.headers = multipart_form_encode(body, self.headers, boundary) else: ctype = "application/x-www-form-urlencoded; charset=utf-8" self._body = form_encode(body) elif hasattr(body, "boundary"): ctype = "multipart/form-data; boundary=%s" % self.body.boundary clen = body.get_size() self._body = body else: self._body = body if not ctype: ctype = 'application/octet-stream' if hasattr(self.body, 'name'): ctype = mimetypes.guess_type(body.name)[0] if not clen: if hasattr(self._body, 'fileno'): try: self._body.flush() except IOError: pass try: fno = self._body.fileno() clen = str(os.fstat(fno)[6]) except IOError: if not self.is_chunked(): clen = len(self._body.read()) elif hasattr(self._body, 'getvalue') and not \ self.is_chunked(): clen = len(self._body.getvalue()) elif isinstance(self._body, types.StringTypes): self._body = to_bytestring(self._body) clen = len(self._body) if clen is not None: self.headers['Content-Length'] = clen if ctype is not None: self.headers['Content-Type'] = ctype
def _set_body(self, body): ctype = self.headers.ipop('content-type', None) clen = self.headers.ipop('content-length', None) if isinstance(body, dict): if ctype is not None and \ ctype.startswith("multipart/form-data"): type_, opts = cgi.parse_header(ctype) boundary = opts.get('boundary', uuid.uuid4().hex) self._body, self.headers = multipart_form_encode( body, self.headers, boundary) else: ctype = "application/x-www-form-urlencoded; charset=utf-8" self._body = form_encode(body) elif hasattr(body, "boundary"): ctype = "multipart/form-data; boundary=%s" % self.body.boundary clen = body.get_size() self._body = body else: self._body = body if not ctype: ctype = 'application/octet-stream' if hasattr(self.body, 'name'): ctype = mimetypes.guess_type(body.name)[0] if not clen: if hasattr(self._body, 'fileno'): try: self._body.flush() except IOError: pass try: fno = self._body.fileno() clen = str(os.fstat(fno)[6]) except IOError: if not self.is_chunked(): clen = len(self._body.read()) elif hasattr(self._body, 'getvalue') and not \ self.is_chunked(): clen = len(self._body.getvalue()) elif isinstance(self._body, types.StringTypes): self._body = to_bytestring(self._body) clen = len(self._body) if clen is not None: self.headers['Content-Length'] = clen if ctype is not None: self.headers['Content-Type'] = ctype
def delete(path, base=URL_BASE, params_dict={}): payload = forms.form_encode(params_dict) app_logger.debug("delete: base={0} path={1} payload={2}".format(base, path, payload)) resource = create_resource() exec_request(partial(resource.delete, path=path, payload=payload))
def put(path, base=URL_BASE, params_dict={}): app_logger.debug("put: base={0} path={1} payload={2}".format(base, path, forms.form_encode(params_dict))) resource = create_resource() exec_request(partial(resource.put, path=path, payload=forms.form_encode(params_dict)))