def make_request(request, method, default_headers=None, **kwargs): """ convert parameters into relevant parts of the an http request :param request: either url or an HTTPRequest object :param method: http request method :param default_headers: default headers :param kwargs: headers, query string params for GET and DELETE, data for POST and PUT """ kwargs = convert(kwargs) if not default_headers: headers = dict(DEFAULT_HEADERS) else: headers = default_headers.copy() if isinstance(request, HTTPRequest): headers.update(request.headers) if 'headers' in kwargs: headers.update(kwargs.pop('headers')) if isinstance(request, HTTPRequest): request.method = method request.headers.update(headers) else: request = HTTPRequest(request, method, headers) if kwargs: if method in ['GET', 'DELETE']: request.url = "{}?{}".format(request.url, urllib.urlencode(kwargs)) elif method in ['POST', 'PUT']: if request.headers['Content-Type'] == JSON_TYPE: request.body = json.dumps(kwargs) elif 'body' in kwargs: request.body = kwargs['body'] return request
def send(self, message): """ Send a message to API URL """ send_url = '{}/{}'.format( self.url, self.get_api_path_equivalent(message=message)) logger.debug("EventReporter API URL = {}".format(send_url)) request = HTTPRequest(url=send_url) request.method = 'POST' request.headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } request.body = message.to_json() response = None try: #noinspection PyTypeChecker response = self.http_client.fetch(request=request) logger.debug("Response from Event Reporter: {}".format( response.body)) except httpclient.HTTPError, e: logger.error( "Error when trying to contact Event Reporter! Error: {}". format(e))
def _create_http_request(self, method, host, port, path, params=None, data=None, **kwargs): url = 'http://{host}:{port}{uri}'.format(host=host, port=port, uri=path) if params and isinstance(params, dict): url += '?' + urlencode(params) request = HTTPRequest( method=method, url=url, allow_nonstandard_methods=True, connect_timeout=self._connect_timeout, request_timeout=self._request_timeout, **kwargs ) if data and method in ['POST', 'PUT', 'PATCH']: try: request.body = json.dumps(data) except TypeError as e: logging.error(str(e)) raise DBApiError(e) return request
def getData(self,url,method,data,cookie): try: client = HTTPClient() request = HTTPRequest( url, method=method, headers={ 'Cookie':cookie } ) if data and method=="GET": data = json.loads(data) url = url_concat(url,data) request.url = url elif data and method=="POST": data = json.loads(data) print data data = urllib.urlencode(data) request.body = data # print request.url response = client.fetch(request) return response.body except Exception,e: # print str(e) return None
def getData(self,url,method,data,cookie): try: client = HTTPClient() request = HTTPRequest( url, method=method, headers={ 'Cookie':cookie } ) if data and method=="GET": url = url_concat(url,data) url = url.replace("+","%20") request.url = url elif data and method=="POST": realData = {} for i in data: realData[i[0]] = i[1] data = urllib.urlencode(realData) request.body = data response = client.fetch(request) return json.loads(response.body) except Exception,e: # print str(e) #traceback.print_exc() return str(e)
def _create_http_request(self, method, host, port, path, params=None, data=None, **kwargs): url = 'http://{host}:{port}{uri}'.format(host=host, port=port, uri=path) if params and isinstance(params, dict): url += '?' + urlencode(params) request = HTTPRequest(method=method, url=url, allow_nonstandard_methods=True, connect_timeout=self._connect_timeout, request_timeout=self._request_timeout, **kwargs) if data and method in ['POST', 'PUT', 'PATCH']: try: request.body = json.dumps(data) except TypeError as e: logging.error(str(e)) raise DBApiError(e) return request
def perform_request(self, request, response, method): try: constants = request_consts[method] url = request[constants.URL] timeout = request[constants.TIMEOUT] http_request = HTTPRequest(url=url, method=method) http_request.request_timeout = float(timeout)/1000 if method == 'POST': http_request.body = request[constants.BODY] #adds cookies to request params_num = len(request) if constants.COOKIES <= params_num - 1: cookies = request[constants.COOKIES] if len(cookies) > 0: list_of_cookies = list('{0}={1}'.format(cookie, value) for cookie, value in cookies.iteritems()) cookies_str = '; '.join(list_of_cookies) http_request.headers.add('Cookie', cookies_str) #adds headers to request if constants.HEADERS <= params_num - 1: for name, values_list in request[constants.HEADERS].iteritems(): for value in values_list: http_request.headers.add(name, value) self.logger.info("Downloading {0}, headers {1}, method {2}".format(url, http_request.headers, method)) http_response = yield self.http_client.fetch(http_request) response_headers = self._get_headers_from_response(http_response) response.write((True, http_response.body, http_response.code, response_headers,)) response.close() self.logger.info("{0} has been successfuly downloaded".format(url)) except HTTPError as e: self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url)) if e.response is not None: http_response = e.response response_headers = self._get_headers_from_response(http_response) response.write((False, http_response.body, http_response.code, response_headers,)) else: response.write((False, '', e.code, {},)) response.close() except socket.gaierror as e: self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url)) response.write((False, '', e.errno, {},)) response.close() except Exception as e: self.logger.error("Unhandled error ({0}) occured in perform_request, report about this problem " "to httpclient service developers. Method is {1}, stacktrace is: {2}".format( e.message, method, traceback.format_exc())) response.write((False, '', 0, {},)) response.close()
def fetch(self, path, body=None, **kwargs): kwargs['url'] = self.get_url(path) request = HTTPRequest(**kwargs) if body is not None: request.body = body request.allow_nonstandard_methods = True self.http_client.fetch(request, self.stop, method=None) return self.wait()
def fetch(self, path, **kwargs): kwargs['url'] = self.get_url(path) body = kwargs.pop('body', None) request = HTTPRequest(**kwargs) request.body = body request.allow_nonstandard_methods = True request.request_timeout = TIMEOUT self.http_client.fetch(request, self.stop, **kwargs) return self.wait()
def post(self): # param from the client message = {} message['status'] = 'error' message['returned_url'] = 'null' #url = self.get_argument('url', None) comic_id = self.get_argument('img', None) self.threash_hold = self.get_argument('threshold', 90) if not self.threash_hold: self.threash_hold = float(90) else: self.threash_hold = float(self.threash_hold) if comic_id: comic_id = int(comic_id[5:]) if len(self.request.files['file']) > 0: img_file = self.request.files['file'][0] # check the client params if not img_file or comic_id <= 0: self.write(json.dumps(message)) self.finish() else: self.comic_picture_id = comic_id ###################################################### # print self.client_cv_img filename = img_file['filename'] saved_path = self.IMAGE_PATH + filename self.client_saved_img = saved_path if not os.path.exists(filename): with open(saved_path, "wb") as fp: fp.write(img_file['body']) # save the client img self.client_cv_img = scv.Image(r'%s' % str(saved_path)) ###################################################### # ok, we save the client image and gen the SimpleCV img # we pass the client img url to oxford to get the params # get parameters get_params = urllib.urlencode({ 'analyzesFaceLandmarks': 'true', 'analyzesAge': 'true', 'analyzesGender': 'true', 'analyzesHeadPose': 'true', }) url = self.API_URL % get_params post_params = {} post_params['url'] = self.HOST + saved_path #print post_params['url'] # request request = HTTPRequest(url, method='POST') request.body = json.dumps(post_params) request.headers['Content-Type'] = 'application/json' request.headers['Ocp-Apim-Subscription-key'] = self.TOKEN # fetch client = AsyncHTTPClient() client.fetch(request, self.handleResponse)
def _request(self, extension, body, callback): """Wraps tornado syntax for sending an HTTP request.""" request = HTTPRequest('http://{}/{}'.format(self.ip, extension), connect_timeout=self.timeout, request_timeout=self.timeout) if body: request.body = body request.method = 'POST' if extension == 'ToolWeb/Cmd': request.headers = {'Content-Type': 'text/xml'} self.client.fetch(request, callback)
def do_send_sms(cell, data): http_client = HTTPClient() sms_url = "http://172.31.11.203:8080/notify/sms/" request = HTTPRequest(sms_url) request.headers["Content-Type"] = "application/json" request.headers["HTTP_HEAD_ENCODING"] = "utf-8" request.method = "POST" request.body = '{"id":"0","phones":"' + cell + '","content":"' + data + '"}' resp = http_client.fetch(request) if resp.code != 201: raise RuntimeError("SMS Gateway Error: " + str(resp.code)) print resp.code, resp.body
def send_request(self, method, url, data=None, data_type="json"): method = method.upper() has_payload = method == self.POST or method == self.PUT is_CUD = has_payload or method == self.DELETE full_url = urlparse.urlunparse((controller_address['scheme'], controller_address['host'] + ':' + str(controller_address['port']), url, None, None, None)) headers = { 'Content-Type': HttpClient.set_content_type(data_type) } request = HTTPRequest(url=full_url, method=method, headers=headers, auth_username=controller_auth['username'], auth_password=controller_auth['password'], connect_timeout=http_client_settings.get("timeouts", {}).get("connect", 3), request_timeout=http_client_settings.get("timeouts", {}).get("request", 10)) if has_payload: if data_type == "json": request.body = json.dumps(data) if is_CUD: if self.dry_run: logger.info("\nDRY RUN") logger.debug("\n\nSending {} request.\nUrl: {}\nBody: {}\n".format(method, full_url, request.body)) if is_CUD and self.dry_run: response = json.dumps({ "status": "ok", "msg": "dry_run" }) else: try: response = HTTPClient().fetch(request) if not self.fail_silently and not self.is_ok(response.code): raise HttpClientException(response) logger.debug("\n\nResponse ({}).\nUrl: {}\nBody: {}\n".format(response.code, full_url, response.body)) return response except HTTPError as e: logger.debug("HttpClient error: {}".format(e.message)) if not self.fail_silently: raise HttpClientException(e) return None return response
def _make_proxy_request(self, request_data): timeout = float(request_data.get("timeout", DEFAULT_TIMEOUT)) validate_cert = bool(request_data.get("validate_cert") or True) max_redirects = request_data.get("max_http_redirects") or 0 follow_redirects = max_redirects > 0 # 0 means do not follow redirects url = request_data.get("url") params = request_data.get("data") post_type = request_data.get("post_type") if params and post_type is None: url = "%s?%s" % (url, urlencode(params)) logger.info("[%s]agent request url: %s", self.id, url) proxy_request = HTTPRequest( url, validate_cert=validate_cert, headers=self._get_proxy_request_headers(request_data), method=request_data.get("method", "GET"), allow_nonstandard_methods=True, connect_timeout=timeout, request_timeout=timeout, streaming_callback=self._streaming_callback, header_callback=self._header_callback, follow_redirects=follow_redirects, max_redirects=max_redirects, prepare_curl_callback=self.prepare_curl_callback, ) role_name = request_data.get("role") if role_name: InterfaceRoleManager.set_curl_interface_role( proxy_request, role_name, ) keystone_auth_info = request_data.get("keystone") if keystone_auth_info: logger.warning( "[%s]agent request required keystone token", ) auth_headers = yield self._get_keystone_auth_headers( keystone_auth_info, validate_cert=validate_cert, ) if not auth_headers: raise gen.Return() proxy_request.headers.update(auth_headers) body = self._get_request_body(request_data) if body: proxy_request.body = body raise gen.Return(proxy_request)
def fetch(self, endpoint, message, headers=None, **kwargs): try: request = HTTPRequest(self._endpoints[endpoint], 'POST') request.headers = self._sanitize_headers(headers) request.body = self._sanitize_body(message) for key, value in kwargs.items(): setattr(request, key, value) try: response = yield AsyncHTTPClient().fetch(request) response = self._sanitize_response(response) except HTTPError, e: response = self._sanitize_response(error=e) except Exception, e: response = e
def send_message(url, body=None, method='GET', handler=None): """ if handler == None: send a sync request else handler == <function>: send async request """ request = HTTPRequest(url, method=method) if body: request.body = body if handler: httpclient.AsyncHTTPClient(request, handler) else: client = httpclient.HTTPClient() response = client.fetch(request) return response.body
def send(self, message): """ Send a message to API URL """ send_url = '{}/{}'.format(self.url, self.get_api_path_equivalent(message=message)) logger.debug("EventReporter API URL = {}".format(send_url)) request = HTTPRequest(url=send_url) request.method = 'POST' request.headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} request.body = message.to_json() response = None try: #noinspection PyTypeChecker response = self.http_client.fetch(request=request) logger.debug("Response from Event Reporter: {}".format(response.body)) except httpclient.HTTPError, e: logger.error("Error when trying to contact Event Reporter! Error: {}".format(e))
def post(self): ret = {'code': 200, 'content': ''} try: url = self.get_argument("url", None) method = self.get_argument("method", None) data = self.get_argument("data", None) if not (url and method): ret['code'] = 400 ret['content'] = u'参数缺少' else: client = HTTPClient() request = HTTPRequest(url=url, method=method) if (method == "POST"): request.body = urllib.urlencode(json.loads(data)) response = client.fetch(request) ret['content'] = json.loads(response.body) except Exception, e: ret['code'] = 500 ret['content'] = u'系统错误'
async def test_birth_year_handler( http_client, base_url, db_user_registration, get_device_mock_auth, user_for_device_mock, device_ids_mock, ): """ A user has two devices with the ids: [device_id1, device_id2]. Initially there are no birth dates associated with any of the device ids. The user updates the birth date information twice for device_id1. The birth date information for device_id2 should be updated accordingly. """ with testsql.set_db_user(testsql.DB_USER_SERVICE_API): birth_year = await sql.get_birth_year(device_id=TEST_DEVICE_ID) assert birth_year is None birth_year = await sql.get_birth_year(device_id="device_id2") assert birth_year is None req = HTTPRequest( method="POST", url=base_url + "/birthyear", body='{"birthyear": 1989}', ) mw.add_auth_header(req, key=TEST_DEVICE_KEY, device_id=TEST_DEVICE_ID) for birth_year in [1989, 1990]: req.body = f'{{"birthyear": {birth_year}}}' mw.add_auth_header(req, key=TEST_DEVICE_KEY, device_id=TEST_DEVICE_ID) resp = await http_client.fetch(req) assert resp.code == 200 with testsql.set_db_user(testsql.DB_USER_SERVICE_API): birth_year_stored = await sql.get_birth_year( device_id=TEST_DEVICE_ID) assert birth_year_stored == birth_year birth_year = await sql.get_birth_year(device_id="device_id2") assert birth_year_stored == birth_year
def post(self): ret = {'code':200,'content':''} try: url = self.get_argument("url",None) method = self.get_argument("method",None) data = self.get_argument("data",None) if not (url and method): ret['code'] = 400 ret['content'] = u'参数缺少' else: client = HTTPClient() request = HTTPRequest( url = url, method = method ) if(method=="POST"): request.body = urllib.urlencode(json.loads(data)) response = client.fetch(request) ret['content'] = json.loads(response.body) except Exception,e: ret['code'] = 500 ret['content'] = u'系统错误'
def send_message(logger, msg): try: target_ip = msg['target']['ip'] r = redis_tool.get_it() farm_ips = r.keys('%s_$_%s_$_*_$_%s' % ('mapup', '*', target_ip)) logger.debug('farm_ips : %s', farm_ips) for farm_ip in farm_ips: logger.debug('forward msg to ip: %s' % r.get(farm_ip)) request = HTTPRequest('http://%s/msg' % r.get(farm_ip)) request.method = 'POST' request.body = json.dumps(msg) response = yield AsyncHTTPClient().fetch(request) logger.debug('response is %s' % response.body) rs = json.loads(response.body) if not rs['success']: logger.error(rs['msg']) raise Exception('manor.error.send.command') except: logger.error(generals.trace())
def getData(self, url, method, data, cookie): try: client = HTTPClient() request = HTTPRequest(url, method=method, headers={'Cookie': cookie}) if data and method == "GET": url = url_concat(url, data) url = url.replace("+", "%20") request.url = url elif data and method == "POST": realData = {} for i in data: realData[i[0]] = i[1] data = urllib.urlencode(realData) request.body = data response = client.fetch(request) return json.loads(response.body) except Exception, e: # print str(e) #traceback.print_exc() return str(e)
def test_body_setter(self): request = HTTPRequest('http://example.com') request.body = 'foo' self.assertEqual(request.body, utf8('foo'))
def test_body_setter(self): request = HTTPRequest("http://example.com") request.body = "foo" # type: ignore self.assertEqual(request.body, utf8("foo"))
def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): """ :param method: http request method :param url: http request url :param query_params: query parameters in the url :param headers: http request headers :param body: request json body, for `application/json` :param post_params: request post parameters, `application/x-www-form-urlencoded` and `multipart/form-data` :param _preload_content: this is a non-applicable field for the AiohttpClient. :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts. """ method = method.upper() assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH', 'OPTIONS'] if post_params and body: raise ValueError( "body parameter cannot be used with post_params parameter." ) request = HTTPRequest(url) request.ssl_context = self.ssl_context request.proxy_host = self.proxy_host request.proxy_port = self.proxy_port request.method = method if headers: request.headers = headers if 'Content-Type' not in headers: request.headers['Content-Type'] = 'application/json' request.request_timeout = _request_timeout or 5 * 60 post_params = post_params or {} if query_params: request.url += '?' + urlencode(query_params) # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: if re.search('json', headers['Content-Type'], re.IGNORECASE): if body: body = json.dumps(body) request.body = body elif headers['Content-Type'] == 'application/x-www-form-urlencoded': request.body = urlencode(post_params) # TODO: transform to multipart form elif headers['Content-Type'] == 'multipart/form-data': request.body = encode_multipart_formdata(post_params) # Pass a `bytes` parameter directly in the body to support # other content types than Json when `body` argument is provided # in serialized form elif isinstance(body, bytes): request.body = body else: # Cannot generate the request from given parameters msg = """Cannot prepare a request message for provided arguments. Please check that your arguments match declared content type.""" raise ApiException(status=0, reason=msg) r = yield self.pool_manager.fetch(request) r = RESTResponse(r, r.body) # log response body logger.debug("response body: %s", r.data) if not 200 <= r.status <= 299: raise ApiException(http_resp=r) return r
def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): """ :param method: http request method :param url: http request url :param query_params: query parameters in the url :param headers: http request headers :param body: request json body, for `application/json` :param post_params: request post parameters, `application/x-www-form-urlencoded` and `multipart/form-data` :param _preload_content: this is a non-applicable field for the AiohttpClient. :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts. """ method = method.upper() assert method in [ 'GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH', 'OPTIONS' ] if post_params and body: raise ValueError( "body parameter cannot be used with post_params parameter.") request = HTTPRequest(url) request.ssl_context = self.ssl_context request.proxy_host = self.proxy_host request.proxy_port = self.proxy_port request.method = method if headers: request.headers = headers if 'Content-Type' not in headers: request.headers['Content-Type'] = 'application/json' request.request_timeout = _request_timeout or 5 * 60 post_params = post_params or {} if query_params: request.url += '?' + urlencode(query_params) # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: if re.search('json', headers['Content-Type'], re.IGNORECASE): if body: body = json.dumps(body) request.body = body elif headers[ 'Content-Type'] == 'application/x-www-form-urlencoded': request.body = urlencode(post_params) # TODO: transform to multipart form elif headers['Content-Type'] == 'multipart/form-data': request.body = encode_multipart_formdata(post_params) # Pass a `bytes` parameter directly in the body to support # other content types than Json when `body` argument is provided # in serialized form elif isinstance(body, bytes): request.body = body else: # Cannot generate the request from given parameters msg = """Cannot prepare a request message for provided arguments. Please check that your arguments match declared content type.""" raise ApiException(status=0, reason=msg) r = yield self.pool_manager.fetch(request) r = RESTResponse(r, r.body) # log response body logger.debug("response body: %s", r.data) if not 200 <= r.status <= 299: raise ApiException(http_resp=r) return r
def send_request(self, method, path_components, body='', query_params=None, encode_body=True): """ Send an asynchronous HTTP request to ES via tornado, and return the JSON-decoded response. :arg method: An HTTP method, like "GET" :arg path_components: An iterable of path components, to be joined by "/" :arg body: The request body :arg query_params: A map of querystring param names to values or ``None`` :arg encode_body: Whether to encode the body of the request as JSON """ path = self._join_path(path_components) if query_params: path = '?'.join( [path, urlencode(dict((k, self._utf8(self._to_query(v))) for k, v in iteritems(query_params)))]) request_body = self._encode_json(body) if encode_body else body server_url, was_dead = self.servers.get() url = "%s%s" % (server_url, path) request = HTTPRequest( url, method = method.upper(), headers = { 'Accept': 'application/json', 'Content-type': 'application/json', }, connect_timeout = self.timeout or 5, request_timeout = self.timeout or 30, allow_nonstandard_methods = True, # this is required to have request body to GET ) if body: request.body = request_body self.logger.debug( "Making a request equivalent to this: curl -X%s '%s' -d '%s'", method, url, request_body, ) for attempt in xrange(self.max_retries + 1): try: response = yield self.client.fetch(request) except HTTPError, he: if attempt >= self.max_retries: raise self.logger.error( "HTTP %d (%s) from %s. %d more attempts." % ( he.code, he.message, server_url, (self.max_retries - attempt) ) )
def post(self): request = HTTPRequest(self._url) if len(self.arguments) > 0: request.body = '&'.join(self.arguments) request.method = "POST" return self.client.fetch(request)