def do_target(self, target, method, payload): req = Request( method = method, headers = self.headers) if target.submission_type == 'urlparams': payload = dict_to_urlparams(payload) req.url = target.uri + '?' + payload elif target.submission_type == 'json': payload = serialise_to_json(payload, True) req.url = target.uri req.data = payload elif target.submission_type == 'urlencode': payload = serialise_to_url(payload) req.url = target.uri req.data = payload else: raise NotImplementedError try: resp = self.send( request = req.prepare(), timeout = self.timeout) except RequestException as e: return e return resp
def _set_content(request: Request, payload=None, params: dict = None) -> None: params = params or {} request.params = {k: v for k, v in params.items() if v is not None} if payload is not None: if request.headers['Content-Type'] == 'application/json': request.data = json.dumps(payload) else: request.data = payload
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): headers = {'Authorization': 'bearer %s' % reddit_session.access_token} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write('substituting {} for {} in url\n'.format( config.oauth_url, prefix)) url = config.oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) request.data = data request.files = files return request
def send_post(self, command, endpoint): nao_request = Request('POST', self.lab_address + endpoint) nao_request.data = command nao_request.headers['Content-Type'] = 'application/json' nao_prepared_request = nao_request.prepare() return self.nao_session.send(nao_prepared_request, verify=self.SSL_VERIFY)
def build_request(self, auth, endpoint, body, method, timeout, headers): """ Build up the request object. auth : object | string Will either contain a configured form of authentication - like the well supported HTTPDigestAuth that is inbuilt to requests or simply a string that will notify the service of which type of authentication to use. endpoint : string The URI path to send the request to body : dict If a GET request, will be used for the URL params. If a POST request this will be the data in the body of the packet. method : string The HTTP method timeout : float The time that the request will wait before timing out if there is no data from the server. headers : dict The headers that will be sent in the request """ # Build up Request # timeout=timeout request = Request(method=method, url=endpoint, headers=headers) # if auth is not None: Probably not needed, since there is a separate auth arg # request.headers['Authorization'] = auth if (auth is 'bearer'): request = self.sws.bearer_auth(request) if method is 'GET': request.params = body if method is 'PUT' or method is 'PATCH' or method is 'POST': request.data = body return request
def do_inference(self, subscription, model_id, model_dir, parameters): request = Request() request.headers['apim-subscription-id'] = subscription request.data = self.prepare_inference_data(parameters) meta = get_meta(self.config, subscription, model_id) context = json.loads(meta['context']) actual_model_id = context['modelId'] result = self.magaclient.inference(request, actual_model_id) if not result['resultId']: raise Exception(result['errorMessage']) resultId = result['resultId'] while True: result = self.magaclient.get_result(request, resultId) if result['statusSummary']['status'] == 'READY' or result[ 'statusSummary']['status'] == 'FAILED': break else: log.info("Inference id: {}, result: {}".format( resultId, result)) self.tsanaclient.save_inference_result(parameters, result['result']) time.sleep(5) return STATUS_SUCCESS, result
def setUp(self): # mock request object request = Request() request.method = 'GET' request.url = 'http://example.com/' request.params = {} request.data = {} request.params_and_data = {} self.request = request # mock response object response = Mock() response.content = 'access_token=321' response.headers = {'content-type': 'text/html; charset=UTF-8'} response.ok = True response.status_code = 200 self.response = response # mock raise_for_status with an error def raise_for_status(): raise Exception('Response not OK!') self.raise_for_status = raise_for_status # mock hook object hook = Mock() hook.consumer_key = '123' hook.consumer_secret = '456' hook.access_token = '321' hook.access_token_secret = '654' self.hook = hook
def setUp(self): # mock request object request = Request() request.method = "GET" request.url = "http://example.com/" request.params = {} request.data = {} request.params_and_data = {} self.request = request # mock response object response = Mock() response.content = "access_token=321" response.headers = {"content-type": "text/html; charset=UTF-8"} response.ok = True response.status_code = 200 self.response = response # mock raise_for_status with an error def raise_for_status(): raise Exception("Response not OK!") self.raise_for_status = raise_for_status # mock hook object hook = Mock() hook.consumer_key = "123" hook.consumer_secret = "456" hook.access_token = "321" hook.access_token_secret = "654" self.hook = hook
def do_train(self, subscription, model_id, model_dir, parameters): request = Request() request.headers['apim-subscription-id'] = subscription request.data = self.prepare_training_data(parameters) result = self.magaclient.train(request) if 'modelId' in result: update_state(self.config, subscription, model_id, ModelState.Training, json.dumps(result), None) while True: state = self.magaclient.state(request, result['modelId']) if state['summary']['status'] != 'CREATED' and state[ 'summary']['status'] != 'RUNNING': break else: update_state(self.config, subscription, model_id, ModelState.Training, json.dumps(state), None) time.sleep(5) if state['summary']['status'] == 'READY': update_state(self.config, subscription, model_id, ModelState.Ready, json.dumps(state), None) return STATUS_SUCCESS, json.dumps(state) else: update_state(self.config, subscription, model_id, ModelState.Failed, json.dumps(state), None) return STATUS_FAIL, json.dumps(state) else: update_state(self.config, subscription, model_id, ModelState.Failed, json.dumps(result), result['message']) return STATUS_FAIL, result['message']
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): bearer = 'bearer {0}'.format(reddit_session.access_token) headers = {'Authorization': bearer} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: msg = 'substituting {0} for {1} in url\n'.format( config.oauth_url, prefix) sys.stderr.write(msg) url = config.oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if isinstance(data, dict): if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) else: request.headers.setdefault('Content-Type', 'application/json') request.data = data request.files = files return request
def build_request(self, smarty_request): request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters) request.headers = self.headers if smarty_request.payload: request.data = smarty_request.payload request.method = 'POST' else: request.method = 'GET' return request
def build_request(self, auth, endpoint, body, params, method, timeout, headers): """ Build up the request object. auth : object | string Will either contain a configured form of authentication - like the well supported HTTPDigestAuth that is inbuilt to requests or simply a string that will notify the service of which type of authentication to use. endpoint : string The URI path to send the request to body : dict If a GET request, will be used for the URL params. If a POST request this will be the data in the body of the packet. method : string The HTTP method timeout : float The time that the request will wait before timing out if there is no data from the server. headers : dict The headers that will be sent in the request """ request = Request(method=method, url=endpoint, headers=headers) if auth is 'bearer': request.headers[ 'Authorization'] = f'Bearer {self.sws.access_token}' elif isinstance(auth, HTTPBasicAuth): request.auth = auth if method is 'GET' or method is 'DELETE': request.params = body if self.sws.test_env: request.headers.update(self.FirewallHeader.getHeader()) if method == 'PUT' or method == 'PATCH' or method == 'POST': if 'Content-Type' in headers and headers[ 'Content-Type'] == 'application/x-www-form-urlencoded': request.data = body else: request.data = json.dumps(body) # JSON in the body by default if params != {}: request.params = params return request
def build_request(self, uri, method, body): url = self.build_url(uri) headers = { "Authorization": "Bearer " + self.access_token } req = Request(method, url, headers=headers) if body: if req.method in ["POST", "PUT", "PATH"]: req.data = body else: req.params = body return req
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, "_use_oauth", False): headers = {"Authorization": "bearer %s" % reddit_session.access_token} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write("substituting {} for {} in url\n".format(config.oauth_url, prefix)) url = config.oauth_url + url[len(prefix) :] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = "POST" else: method = "GET" # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write("{0}: {1}\n".format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write("params: {0}\n".format(params)) if data: sys.stderr.write("data: {0}\n".format(data)) if auth: sys.stderr.write("auth: {0}\n".format(auth)) # Prepare request request = Request( method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies ) if method == "GET": return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault("api_type", "json") if reddit_session.modhash: data.setdefault("uh", reddit_session.modhash) request.data = data request.files = files return request
def build_request(smarty_request): request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters) request.headers['User-Agent'] = "smartystreets (sdk:python@{})".format( version.__version__) request.headers['Content-Type'] = smarty_request.content_type if smarty_request.referer: request.headers['Referer'] = smarty_request.referer if smarty_request.payload: request.data = smarty_request.payload request.method = 'POST' else: request.method = 'GET' return request
def test_sign_request(self): signing_key = authenticationutils.load_signing_key( './test_key_container.p12', "Password1") consumer_key = 'dummy' uri = "https://sandbox.api.mastercard.com/fraud/merchant/v1/termination-inquiry?Format=XML&PageOffset=0" request = Request() request.method = "POST" request.data = "" signer = OAuthSigner(consumer_key, signing_key) request = signer.sign_request(uri, request) auth_header = request.headers['Authorization'] self.assertTrue("OAuth" in auth_header) self.assertTrue("dummy" in auth_header)
def _prepare_request(lex_session, url, params, data, auth, files): """Return a requests Request object that can be "prepared".""" headers = {} headers.update(lex_session.http.headers) request = Request(method='GET', url=url, headers=headers, params=params, auth=auth, cookies=lex_session.http.cookies) if not data and not files: # GET request return request if not auth: data.setdefault('api_type', 'json') request.method = 'POST' request.data = data request.files = files return request
def _prepare_request(reddit_session, url, params, data, auth, files): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): headers = {'Authorization': 'bearer %s' % reddit_session.access_token} config = reddit_session.config # pylint: disable-msg=W0212 for prefix in (config._site_url, config._ssl_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write('substituting %s for %s in url\n' % (config._oauth_url, prefix)) url = config._oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('retrieving: %s\n' % url) if reddit_session.config.log_requests >= 2: sys.stderr.write('params: %s\n' % (params or 'None')) sys.stderr.write('data: %s\n' % (data or 'None')) if auth: sys.stderr.write('auth: %s\n' % str(auth)) # Prepare request request = Request(method='GET', url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if not data and not files: # GET request return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) request.method = 'POST' request.data = data request.files = files return request
def test_sign_request(self): if os.path.exists('./test_key_container.p12'): signing_key = authenticationutils.load_signing_key( './test_key_container.p12', "Password1") consumer_key = OAuthSigner("YOUR CONSUMER KEY", signing_key) uri = "https://sandbox.api.mastercard.com/fraud/merchant/v1/termination-inquiry?Format=XML&PageOffset=0" request = Request() request.method = "POST" request.data = "" signer = OAuthSigner(consumer_key, signing_key) request = signer.sign_request(uri, request) else: print( "Please add a ./test_key_container.p12 file to enable key tests" )
def request(self, method: str, endpoint_url: str, payload: dict = None, query: dict = None, parse: bool = True) -> Union[str, Dict]: req = Request( method=method, url=self._configuration.environment.api_url + endpoint_url, headers={ "User-Agent": self._user_agent, "X-ApiKey": self._configuration.key, "Authorization": f"TOKEN {self._configuration.token}", }, ) if payload and isinstance(payload, dict): req.data = json.dumps(payload, cls=JSONEncoder) req.headers['Content-Type'] = 'application/json' if query and isinstance(query, dict): req.params = query prepared = req.prepare() try: r = self._session.send(prepared, timeout=self._configuration.timeout) except RequestException: raise ApiException(prepared, 'Request to server timed out.') try: r.raise_for_status() except HTTPError as e: raise ApiException(prepared, e.args[0], previous=e) data = r.text if parse: try: data = json.loads(data) except json.JSONDecodeError: raise ApiException(prepared, 'Cannot parse response data to json.') return data
def sendHttpRequest(_args): s = Session() url = makeURL(_args) req = Request(_args.get("method"), url) if (_args.get("headers") is not None) : req.headers = _args.get("headers") if (_args.get("auth") is not None) : req.auth = HTTPBasicAuth(_args.get("auth")[0], _args.get("auth")[1]) if (_args.get("params") is not None) : req.params = _args.get("params") if (_args.get("cookies") is not None) : req.cookies = _args.get("cookies") if (_args.get("data") is not None) : req.data = _args.get("data") prepped = req.prepare() if (_args.get("body") is not None) : prepped.body = _args.get("body") # do something with prepped.body # do something with prepped.headers resp = s.send(prepped,timeout=_args.get("timeout"), proxies=_args.get("proxies")) return resp
def _do_call(self, method, path, cls=None, data=None): request = Request( method, self._build_url(path) ) if data is not None: request.data = data response = self._session.send( request.prepare() ) if response.status_code != 200: raise Exception("Bad status: " + str(response.status_code)) if cls is None: return response.json() else: return convert_dict_to_object(cls, response.json(), self)
def _prepare_request(session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" headers = {} headers.update(session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if isinstance(data, dict): if not auth: data.setdefault('api_type', 'json') if session.modhash: data.setdefault('uh', session.modhash) else: request.headers.setdefault('Content-Type', 'application/json') request.data = data request.files = files return request
def sendHttpRequest(_args): s = Session() url = makeURL(_args) req = Request(_args.get("method"), url) if (_args.get("headers") is not None): req.headers = _args.get("headers") if (_args.get("auth") is not None): req.auth = HTTPBasicAuth(_args.get("auth")[0], _args.get("auth")[1]) if (_args.get("params") is not None): req.params = _args.get("params") if (_args.get("cookies") is not None): req.cookies = _args.get("cookies") if (_args.get("data") is not None): req.data = _args.get("data") prepped = req.prepare() if (_args.get("body") is not None): prepped.body = _args.get("body") # do something with prepped.body # do something with prepped.headers resp = s.send(prepped, timeout=_args.get("timeout"), proxies=_args.get("proxies")) return resp
def api_request(verb, url, has_data): try: s = Session() r = Request(verb, base_url + url, headers=headers, auth=(environ.get('DOME9_API_KEY'), environ.get('DOME9_API_SECRET'))) if has_data: r.data = json.dumps(payload) prepared_request = s.prepare_request(r) response = s.send(prepared_request, proxies={"https": environ.get('DOME9_HTTPS_PROXY')}) if response.status_code != 200: raise Exception("Dome9 API Response unexpected: " + response.reason) except Exception as e: print(str(e)) sys.exit(0) return response.json()
def request(url, method=None, data=None, decode=True): if not url.startswith('http://') and not url.startswith('https://'): url = 'http://' + url request = Request(method, url) if data: request.data = json.dumps(data) with Session() as session: prepped = session.prepare_request(request) try: response = session.send(prepped) except: return False if decode: return json.loads(response.text) else: return response
def setUp(self): # mock request object request = Request() request.method = 'GET' request.url = 'http://example.com/' request.headers = {} request.params = {} request.data = {} request.params_and_data = {} self.request = request # mock consumer object consumer = Mock() consumer.key = '123' consumer.secret = '456' self.consumer = consumer # mock token object token = Mock() token.key = '321' token.secret = '456' self.token = token
def inference_wrapper(self, request, model_key, parameters, callback): try: result = {} subrequest = Request() subrequest.headers = request.headers subrequest.data = parameters result = self.magaclient.inference(subrequest, model_key) if not result['resultId']: raise Exception(result['errorMessage']) resultId = result['resultId'] while True: result = self.magaclient.get_result(subrequest, resultId) if result['status'] == 'READY': break except Exception as e: result['errorMessage'] = str(e) if callback is not None: callback(request, model_key, result) return STATUS_SUCCESS, ''
def create_request_object(self): """ Functionality: We attempt to create the Request() object and apply the correct data to it. We also run the validation to make sure that all the data we are applying is correct """ request_object = Request() # Run internal validation to first determine if the body schema and type is correct self.validate_body_schematic() self.validate_body_type() # Method Apply and validation self.validate_method() request_object.method = self._request_model.method # URL Apply and validation self.validate_endpoint() request_object.url = self._request_model.endpoint # Query Apply and validation self.validate_query_param() request_object.params = self._request_model.query # Headers Apply and validation self.validate_headers() request_object.headers = self._request_model.headers # Body Apply and validation self.validate_body() request_object.data = self._request_model.body # Authentication Apply and validation self.validate_auth() request_object.auth_data = self._request_model.auth_data request_object.auth_type = self._request_model.auth_type return request_object
def process_request(req): # api_stream = open(file, 'r') # api = yaml.load_all(api_stream) # for reqs in api: # for req in reqs: preq = Request() pr("Testing: {0}, which is a {1} request".format(req['request']['name'], req['request']['method'])) static_headers = process_static_headers(req['request']['headers']) var_headers = process_var_headers(req['request']['headers']) all_headers = dict(static_headers.items() + var_headers.items()) #print all_headers if req['request']['params']: preq.params = req['request']['params'] preq.headers = all_headers preq.method = req['request']['method'] preq.url = req['request']['uri'] if 'body' in req['request'].keys(): if req['request']['body']['type'] == 'post-vars': preq.data = req['request']['body']['value'] elif req['request']['body']['type'] == 'json': preq.json = req['request']['body']['value'] elif req['request']['body']['type'] == 'file': filepath = req['request']['body']['value']['filepath'] preq.files = {filepath: open(filepath, 'rb')} else: raise Exception('Payload type unsupported') sys.exit(1) rsess = Session() prepped = preq.prepare() # for item in req['request']['exclude_headers']: # if item in prepped.headers.keys(): # del prepped.headers[item] response = rsess.send(prepped, verify=cert_bundle, proxies=proxies) return response
def setUp(self): # mock request object request = Request() request.method = 'GET' request.url = 'http://example.com/' request.headers = {} request.params = {} request.data = {} request.params_and_data = {} self.request = request # mock response object response = Mock() response.content = 'access_token=321' response.headers = {'content-type': 'text/html; charset=UTF-8'} response.ok = True response.status_code = 200 response.raise_for_status = lambda: None self.response = response # mock raise_for_status with an error def raise_for_status(): raise Exception('Response not OK!') self.raise_for_status = raise_for_status # mock consumer object consumer = Mock() consumer.key = '123' consumer.secret = '456' self.consumer = consumer # mock token object token = Mock() token.key = '321' token.secret = '456' self.token = token
def run(self, request: Request) -> Request: if dataclasses.is_dataclass(request.data): request.json = conv.unstructure(request.data) request.data = None return request
def run(self, request: Request) -> Request: if is_attrs_class(request.data): request.json = conv.unstructure(request.data) request.data = None return request
def upload_file(self, local_file, remote_name=None, metadata={}, headers={}, queue_derive=True, ignore_bucket=False, verbose=False, debug=False): """Upload a single file to an item. The item will be created if it does not exist. :type local_file: str or file :param local_file: The filepath or file-like object to be uploaded. :type remote_name: str :param remote_name: (optional) Sets the remote filename. :type metadata: dict :param metadata: (optional) Metadata used to create a new item. :type headers: dict :param headers: (optional) Add additional IA-S3 headers to request. :type queue_derive: bool :param queue_derive: (optional) Set to False to prevent an item from being derived after upload. :type ignore_bucket: bool :param ignore_bucket: (optional) Set to True to ignore and clobber existing files and metadata. :type debug: bool :param debug: (optional) Set to True to print headers to stdout, and exit without sending the upload request. Usage:: >>> import internetarchive >>> item = internetarchive.Item('identifier') >>> item.upload_file('/path/to/image.jpg', ... remote_name='photos/image1.jpg') True :rtype: bool :returns: True if the request was successful and file was uploaded, False otherwise. """ if not self.session: self.session = Session() if not hasattr(local_file, 'read'): local_file = open(local_file, 'rb') if not remote_name: remote_name = local_file.name.split('/')[-1] # Attempt to add size-hint header. if not headers.get('x-archive-size-hint'): try: local_file.seek(0, os.SEEK_END) headers['x-archive-size-hint'] = local_file.tell() local_file.seek(0, os.SEEK_SET) except IOError: pass # Prepare Request. endpoint = 'http://s3.us.archive.org/{0}/{1}'.format(self.identifier, remote_name) headers = ias3.build_headers(metadata, headers, queue_derive=queue_derive, ignore_bucket=ignore_bucket) request = Request('PUT', endpoint, headers=headers) # TODO: Add support for multipart. # `contextlib.closing()` is used to make StringIO work with # `with` statement. with closing(local_file) as data: request.data = data.read() prepped_request = request.prepare() if debug: return prepped_request else: if verbose: stdout.write(' uploading file: {0}\n'.format(remote_name)) return self.session.send(prepped_request)
def __callRequest(self, jsonDecode=False): if self.__enableDNS: import socket self.save_getaddrinfo = socket.getaddrinfo socket.getaddrinfo = self.new_getaddrinfo if self.__aParamatersLine: sParameters = self.__aParamatersLine else: sParameters = self.__aParamaters if (self.__cType == cRequestHandler.REQUEST_TYPE_GET): if (len(sParameters) > 0): if (self.__sUrl.find('?') == -1): self.__sUrl = self.__sUrl + '?' + str(sParameters) sParameters = '' else: self.__sUrl = self.__sUrl + '&' + str(sParameters) sParameters = '' sContent = '' if self.BUG_SSL == True: self.verify = False if self.__cType == cRequestHandler.REQUEST_TYPE_GET: method = "GET" else: method = "POST" oResponse = None try: _request = Request(method, self.__sUrl, headers=self.__aHeaderEntries) if method in ['POST']: _request.data = sParameters if self.__Cookie: _request.cookies = self.__Cookie if self.json: _request.json = self.json prepped = _request.prepare() self.s.headers.update(self.__aHeaderEntries) oResponse = self.s.send(prepped, timeout=self.__timeout, allow_redirects=self.redirects, verify=self.verify) self.__sResponseHeader = oResponse.headers self.__sRealUrl = oResponse.url if jsonDecode == False: sContent = oResponse.content #Necessaire pour Python 3 if isMatrix() and not 'youtube' in oResponse.url: try: sContent = sContent.decode('unicode-escape') except: try: sContent = sContent.decode() except: pass else: sContent = oResponse.json() except ConnectionError as e: # Retry with DNS only if addon is present from Plugins.Extensions.IPTVPlayer.tsiplayer.addons.resources.lib import xbmcvfs if xbmcvfs.exists('special://home/addons/script.module.dnspython/' ) and self.__enableDNS == False: self.__enableDNS = True return self.__callRequest() else: error_msg = addon().VSlang(30470) except RequestException as e: if 'CERTIFICATE_VERIFY_FAILED' in str(e) and self.BUG_SSL == False: self.BUG_SSL = True return self.__callRequest() elif 'getaddrinfo failed' in str(e) and self.__enableDNS == False: # Retry with DNS only if addon is present from Plugins.Extensions.IPTVPlayer.tsiplayer.addons.resources.lib import xbmcvfs if xbmcvfs.exists( 'special://home/addons/script.module.dnspython/'): self.__enableDNS = True return self.__callRequest() else: error_msg = addon().VSlang(30470) else: error_msg = "%s (%s),%s" % (addon().VSlang(30205), e, self.__sUrl) dialog().VSerror(error_msg) sContent = '' if oResponse.status_code in [503, 403]: if not "Forbidden" in sContent: #Default CLOUDPROXY_ENDPOINT = 'http://localhost:8191/v1' try: json_session = post(CLOUDPROXY_ENDPOINT, headers=self.__aHeaderEntries, data=dumps({'cmd': 'sessions.list'})) except: dialog().VSerror("%s" % ( "Page protege par Cloudflare, veuillez executer FlareSolverr." )) #On regarde si une session existe deja. if json_session.json()['sessions']: cloudproxy_session = json_session.json()['sessions'][0] else: json_session = post(CLOUDPROXY_ENDPOINT, headers=self.__aHeaderEntries, data=dumps({'cmd': 'sessions.create'})) response_session = loads(json_session.text) cloudproxy_session = response_session['session'] self.__aHeaderEntries[ 'Content-Type'] = 'application/x-www-form-urlencoded' if ( method == 'post') else 'application/json' #Ont fait une requete. json_response = post(CLOUDPROXY_ENDPOINT, headers=self.__aHeaderEntries, data=dumps({ 'cmd': 'request.%s' % method.lower(), 'url': self.__sUrl, 'session': cloudproxy_session, 'postData': '%s' % urlEncode(sParameters) if (method.lower() == 'post') else '' })) http_code = json_response.status_code response = loads(json_response.text) if 'solution' in response: if self.__sUrl != response['solution']['url']: self.__sRealUrl = response['solution']['url'] sContent = response['solution']['response'] if oResponse and not sContent: #Ignorer ces deux codes erreurs. ignoreStatus = [200, 302] if oResponse.status_code not in ignoreStatus: dialog().VSerror("%s (%d),%s" % (addon().VSlang(30205), oResponse.status_code, self.__sUrl)) if sContent: if (self.__bRemoveNewLines == True): sContent = sContent.replace("\n", "") sContent = sContent.replace("\r\t", "") if (self.__bRemoveBreakLines == True): sContent = sContent.replace(" ", "") if self.__enableDNS: socket.getaddrinfo = self.save_getaddrinfo self.__enableDNS = False return sContent
def getRequestObject(self, config, metadata, inputMap): """ Gets the Request Object with URL and """ #Separate the headers from the inputMap headerMap = util.subMap(inputMap, config.getHeaderParams()) #Separate the query from the inputMap queryMap = util.subMap(inputMap, config.getQueryParams()) #Extract the action action = config.getAction() #getting the url fullURL = self.getURL(config, metadata, inputMap) #get method from action method = self.getMethod(action) if method is None: raise APIException("Invalid action supplied: " + action) #Create the request object request = Request() #set the request parameters request.method = method request.url = fullURL request.headers[ APIController.KEY_ACCEPT] = APIController.APPLICATION_JSON request.headers[ APIController.KEY_CONTENT_TYPE] = APIController.APPLICATION_JSON request.headers[ APIController. KEY_USER_AGENT] = APIController.PYTHON_SDK + "/" + metadata.getVersion( ) #Add inputMap to params if action in read,delete,list,query if action.upper() in [ APIController.ACTION_READ, APIController.ACTION_DELETE, APIController.ACTION_LIST, APIController.ACTION_QUERY ]: request.params = inputMap elif action.upper() in [ APIController.ACTION_CREATE, APIController.ACTION_UPDATE ]: request.data = json.dumps(inputMap) #Set the query parameter Format as JSON request.params[APIController.KEY_FORMAT] = APIController.JSON #Add the query in queryMap request.params.update(queryMap) #Add headers for key, value in list(headerMap.items()): request.headers[key] = value #Sign the request #This should add the authorization header in the request if (Config.getAuthentication()): Config.getAuthentication().signRequest(fullURL, request) return request
def sendRequest(options): if 'errorMessage' in options: if 'toBuffer' in options: return options['errorMessage'], None, None else: return options['errorMessage'], None ## # Define empty headers hash wich will be injected # along with Requests parameters headers = {} ## # Set up HTTP proxy if 'proxy' in options: proxy = options['proxy'] ## # Set the response mode to binary # and stream the response back # when dealing with Binary Responses if 'toFile' in options or 'toBuffer' in options: options['request']['response'] = {'mode': 'binary'} headers['X-Optidash-Binary'] = '1' stream = True ## # Use JSON request for fetch() mode if 'withFetch' in options: endpoint = 'https://api.optidash.ai/1.0/fetch' headers['Content-Type'] = 'application/json' ## # Use Multipart request for upload() mode if 'withUpload' in options: endpoint = 'https://api.optidash.ai/1.0/upload' if type(options['file']) is not str: file = options['file'].getvalue() else: if (os.path.isfile(options['file']) is False): error = 'Input file `' + options['file'] + '` does not exist' if 'toBuffer' in options: return error, None, None else: return error, None file = open(options['file'], 'rb') files = {'file': file} ## # Initiate POST request to the API s = Session() req = Request( 'POST', endpoint, auth=(options['key'], ''), headers=headers, files=files if 'files' in vars() else None, ) if 'withFetch' in options: req.json = options['request'] if 'withUpload' in options: req.data = {'data': json.dumps(options['request'])} prepped = req.prepare() res = s.send(prepped, proxies=proxy if 'proxy' in vars() else None, stream=stream if 'stream' in vars() else False) ## # Parse the response body when dealing with toJSON() requests # and return the data to the user if 'toJSON' in options: try: response = res.json() except Exception as e: error = 'Unable to parse JSON response from the Optidash API' if 'toBuffer' in options: return error, None, None else: return error, None if response['success'] is False: error = response['message'] if 'toBuffer' in options: return error, None, None else: return error, None return None, response ## # Try to parse JSON data from X-Optidash-Meta header try: meta = json.loads(res.headers['X-Optidash-Meta']) except Exception as e: error = 'Unable to parse JSON data from X-Optidash-Meta header' if 'toBuffer' in options: return error, None, None else: return error, None ## # Check whether the API call resulted with a failed response # and pass the error message to the user if meta['success'] is False: error = meta['message'] if 'toBuffer' in options: return error, meta, None else: return error, meta ## # Stream incoming binary data to disk # when dealing with toFile() requests if 'toFile' in options: try: with open(options['outputPath'], 'wb') as f: res.raw.decode_content = True shutil.copyfileobj(res.raw, f) except Exception as e: error = 'Unable to save resulting image at `' + options[ 'outputPath'] + '` location' return error, meta return None, meta ## # Return raw data to the user # when dealing with toBuffer() requests if 'toBuffer' in options: res.raw.decode_content = True return None, meta, res.raw.data
def __callRequest(self, jsonDecode=False): if self.__enableDNS: import socket self.save_getaddrinfo = socket.getaddrinfo socket.getaddrinfo = self.new_getaddrinfo if self.__aParamatersLine: sParameters = self.__aParamatersLine else: sParameters = self.__aParamaters if (self.__cType == cRequestHandler.REQUEST_TYPE_GET): if (len(sParameters) > 0): if (self.__sUrl.find('?') == -1): self.__sUrl = self.__sUrl + '?' + str(sParameters) sParameters = '' else: self.__sUrl = self.__sUrl + '&' + str(sParameters) sParameters = '' sContent = '' if self.__cType == cRequestHandler.REQUEST_TYPE_GET: method = "GET" else: method = "POST" try: _request = Request(method, self.__sUrl, headers=self.__aHeaderEntries) if method in ['POST', 'PATCH', 'PUT']: _request.data = sParameters if self.__Cookie: _request.cookies = self.__Cookie if self.json: _request.json = self.json prepped = _request.prepare() self.s.headers.update(self.__aHeaderEntries) oResponse = self.s.send(prepped, timeout=self.__timeout, allow_redirects=self.redirects, verify=self.verify) self.__sResponseHeader = oResponse.headers self.__sRealUrl = oResponse.url if jsonDecode==False: sContent = oResponse.content #Necessaire pour Python 3 if isMatrix(): try: sContent = sContent.decode('unicode-escape') except: try: sContent = sContent.decode() except: pass else: sContent = oResponse.json() except HTTPError as e: if 'CERTIFICATE_VERIFY_FAILED' in str(e.reason) and self.BUG_SSL == False: self.BUG_SSL = True return self.__callRequest() elif 'getaddrinfo failed' in str(e.reason) and self.__enableDNS == False: # Retry with DNS only if addon is present import xbmcvfs if xbmcvfs.exists('special://home/addons/script.module.dnspython/'): self.__enableDNS = True return self.__callRequest() else: error_msg = addon().VSlang(30470) else: error_msg = "%s (%s),%s" % (addon().VSlang(30205), e.reason, self.__sUrl) dialog().VSerror(error_msg) sContent = '' if oResponse.status_code == 503: # Protected by cloudFlare ? from resources.lib import cloudflare if cloudflare.CheckIfActive(sContent): cookies = self.GetCookies() VSlog('Page protegee par cloudflare') CF = cloudflare.CloudflareBypass() sContent = CF.GetHtml(self.__sUrl, sContent, cookies, sParameters, oResponse.headers) self.__sRealUrl, self.__sResponseHeader = CF.GetReponseInfo() if not sContent: #Ignorer ces deux codes erreurs. ignoreStatus = [200,302] if oResponse.status_code not in ignoreStatus: dialog().VSerror("%s (%d),%s" % (addon().VSlang(30205), oResponse.status_code, self.__sUrl)) if sContent: if (self.__bRemoveNewLines == True): sContent = sContent.replace("\n", "") sContent = sContent.replace("\r\t", "") if (self.__bRemoveBreakLines == True): sContent = sContent.replace(" ", "") if self.__enableDNS: socket.getaddrinfo = self.save_getaddrinfo self.__enableDNS = False return sContent
from requests import Request from forecast.forecast_plugin_service import ForecastPluginService from os import environ import json if __name__ == '__main__': environ['SERVICE_CONFIG_FILE'] = 'forecast/config/service_config.yaml' forecast_plugin = ForecastPluginService() request = Request() request.data = r'{"groupId":"8e826a5d-1b01-4ff4-a699-38bea97e17de","seriesSets":[{"seriesSetId":"b643e346-6883-4764-84a5-e63a3788eec9","metricId":"dc5b66cf-6dd0-4c83-bb8f-d849e68a7660","dimensionFilter":{"ts_code":"600030.SH"},"seriesSetName":"Stock price_high","metricMeta":{"granularityName":"Daily","granularityAmount":0,"datafeedId":"29595b1c-531f-445c-adcf-b75b2ab93c34","metricName":"high","datafeedName":"Stock price","dataStartFrom":1105315200000}},{"seriesSetId":"0d4cce4d-f4d4-4cef-be87-dbd28062abfc","metricId":"3274f7e6-683b-4d92-b134-0c1186e416a1","dimensionFilter":{"ts_code":"600030.SH"},"seriesSetName":"Stock price_change","metricMeta":{"granularityName":"Daily","granularityAmount":0,"datafeedId":"29595b1c-531f-445c-adcf-b75b2ab93c34","metricName":"change","datafeedName":"Stock price","dataStartFrom":1105315200000}}],"gran":{"granularityString":"Daily","customInSeconds":0},"instance":{"instanceName":"Forecast_Instance_1586447708033","instanceId":"528cbe52-cb6a-44c0-b388-580aba57f2f7","status":"Active","appId":"173276d9-a7ed-494b-9300-6dd1aa09f2c3","appName":"Forecast","appDisplayName":"Forecast","appType":"Internal","remoteModelKey":"","params":{"missingRatio":0.5,"target":{"filters":{"ts_code":"600030.SH"},"metricId":"dc5b66cf-6dd0-4c83-bb8f-d849e68a7660","name":"Stock price_high"},"waitInSeconds":60,"windowSize":28, "step":1},"hookIds":[]},"startTime":"2020-03-18T00:00:00Z","endTime":"2020-04-18T00:00:00Z","modelId":""}' #response = forecast_plugin.train(request) #print(response) response = { 'modelId' : 'a735a3be-abfc-11ea-a803-000d3af88183'} #status = forecast_plugin.state(request, response['modelId']) #print(status) inference_result = forecast_plugin.inference(request, response['modelId']) print(inference_result) models = forecast_plugin.list_models(request) print(models) delete_result = forecast_plugin.delete(request, response['modelId']) print(delete_result)