Python execute request

43 Python code examples are found related to " execute request". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: server.py    From sanic with MIT License 7 votes vote down vote up
def execute_request_handler(self):
        """
        Invoke the request handler defined by the
        :func:`sanic.app.Sanic.handle_request` method

        :return: None
        """
        self._response_timeout_handler = self.loop.call_later(
            self.response_timeout, self.response_timeout_callback
        )
        self._last_request_time = time()
        self._request_handler_task = self.loop.create_task(
            self.request_handler(
                self.request, self.write_response, self.stream_response
            )
        )

    # -------------------------------------------- #
    # Responding
    # -------------------------------------------- # 
Example 2
Source File: schedule_handler.py    From aws-ops-automator with Apache License 2.0 6 votes vote down vote up
def handle_execute_task_request(self, task_config):

        self._logger.info(INF_HANDLING_EXEC_REQUEST, self.executed_task_name)

        task_to_execute = task_config.get_task(name=self.executed_task_name)
        if task_to_execute is None:
            raise ValueError("Task with name {} does not exists for stack {}".format(self.executed_task_name,
                                                                                     os.getenv(handlers.ENV_STACK_NAME)))

        if not task_to_execute.get(handlers.TASK_ENABLED):
            raise ValueError("Task with name {} is not enabled", self.executed_task_name)

        task_group, sub_tasks = self._execute_task(task_to_execute)
        return safe_dict({
            "datetime": datetime.now().isoformat(),
            "executed-task": self.executed_task_name,
            "task-group": task_group,
            "sub-tasks": sub_tasks
        }) 
Example 3
Source File: result_stream.py    From search-tweets-python with MIT License 6 votes vote down vote up
def execute_request(self):
        """
        Sends the request to the API and parses the json response.
        Makes some assumptions about the session length and sets the presence
        of a "next" token.
        """
        if self.n_requests % 20 == 0 and self.n_requests > 1:
            logger.info("refreshing session")
            self.init_session()

        resp = request(session=self.session,
                       url=self.endpoint,
                       rule_payload=self.rule_payload)
        self.n_requests += 1
        ResultStream.session_request_counter += 1
        resp = json.loads(resp.content.decode(resp.encoding))
        self.next_token = resp.get("next", None)
        self.current_tweets = resp["results"] 
Example 4
Source File: AGOLRouteHelper.py    From public-transit-tools with Apache License 2.0 6 votes vote down vote up
def execute_request(task_url, token, referer, task_params):

    common_parameters = {"f" : "json"}
    if token:
        common_parameters["token"] = token
    else:
        referer = None

    parameters = dict(task_params)
    parameters.update(common_parameters)
    
    before_time = time.time()

    response = makeHTTPRequest(task_url, parameters, referer=referer)
    after_time = time.time()
    return response, after_time - before_time 
Example 5
Source File: context.py    From upvote with Apache License 2.0 6 votes vote down vote up
def ExecuteRequest(self, method, api_route=None, query_args=None, data=None):
    """Execute an API request using the current API context."""
    if method not in constants.METHOD.SET_ALL:
      raise ValueError('Invalid method: {}'.format(method))

    url = self._GetApiUrl(api_route, query_args)

    if data is None:
      logging.info('API %s: %s', method, url)
    else:
      logging.info('API %s: %s (data: %s)', method, url, data)

    try:
      response = requests.request(
          method, url, headers=self._GetApiHeaders(), json=data, verify=True,
          timeout=self.timeout)
    except requests.RequestException as e:
      raise excs.RequestError(
          'Error performing {} {}: {}'.format(method, url, e))
    else:
      return self._UnwrapResponse(response) 
Example 6
Source File: graphite_monitor.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def execute_request(self, request):
        try:
            # This is how the carbon graphite server parses the line.  We could be more forgiving but if it works
            # for them, then we can do it as well.
            metric, value, orig_timestamp = request.strip().split()
            value = float(value)
            orig_timestamp = float(orig_timestamp)
            # Include the time that the original graphite request said to associate with the metric value.
            self.__logger.emit_value(
                metric, value, extra_fields={"orig_time": orig_timestamp}
            )
        except ValueError:
            self.__logger.warn(
                "Could not parse incoming metric line from graphite plaintext server, ignoring",
                error_code="graphite_monitor/badPlainTextLine",
            ) 
Example 7
Source File: service_endpoint_client.py    From azure-devops-cli-extension with MIT License 6 votes vote down vote up
def execute_service_endpoint_request(self, service_endpoint_request, project, endpoint_id):
        """ExecuteServiceEndpointRequest.
        [Preview API] Proxy for a GET request defined by a service endpoint.
        :param :class:`<ServiceEndpointRequest> <azure.devops.v5_1.service_endpoint.models.ServiceEndpointRequest>` service_endpoint_request: Service endpoint request.
        :param str project: Project ID or project name
        :param str endpoint_id: Id of the service endpoint.
        :rtype: :class:`<ServiceEndpointRequestResult> <azure.devops.v5_1.service_endpoint.models.ServiceEndpointRequestResult>`
        """
        route_values = {}
        if project is not None:
            route_values['project'] = self._serialize.url('project', project, 'str')
        query_parameters = {}
        if endpoint_id is not None:
            query_parameters['endpointId'] = self._serialize.query('endpoint_id', endpoint_id, 'str')
        content = self._serialize.body(service_endpoint_request, 'ServiceEndpointRequest')
        response = self._send(http_method='POST',
                              location_id='cc63bb57-2a5f-4a7a-b79c-c142d308657e',
                              version='5.1-preview.1',
                              route_values=route_values,
                              query_parameters=query_parameters,
                              content=content)
        return self._deserialize('ServiceEndpointRequestResult', response) 
Example 8
Source File: cleaner.py    From google-drive-trash-cleaner with GNU General Public License v3.0 6 votes vote down vote up
def execute_request(request, timeout=TIMEOUT_DEFAULT):
    """Execute Google API request
    Automatic retry upon Google backend error (500) until timeout
    """
    while timeout >= 0:
        try:
            response = request.execute()
        except HttpError as e:
            if int(e.args[0]['status']) == 500:
                timeout -= RETRY_INTERVAL
                time.sleep(RETRY_INTERVAL)
                continue
            raise e
        else:
            return response
    raise TimeoutError 
Example 9
Source File: bbg_com.py    From tia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def execute_request(cls, request):
        session = DispatchWithEvents('blpapicom.ProviderSession.1', ResponseHandler)
        session.Start()
        try:
            svcname = request.get_bbg_service_name()
            if not session.OpenService(svcname):
                raise Exception('failed to open service %s' % svcname)

            svc = session.GetService(svcname)
            asbbg = request.get_bbg_request(svc, session)
            session.SendRequest(asbbg)
            session.do_init(request)
            while session.waiting:
                PumpWaitingMessages()
            session.has_deferred_exception and session.raise_deferred_exception()
            request.has_exception and request.raise_exception()
            return request
        finally:
            session.Stop()
            session.do_cleanup() 
Example 10
Source File: request.py    From Python-Programming-Blueprints with MIT License 6 votes vote down vote up
def execute_request(hashtag):
    config = read_config()

    if hashtag.refresh_url:
        refresh_url = hashtag.refresh_url[1:]
        url_params = dict(parse_qsl(refresh_url))
    else:
        url_params = {
            'q': f'#{hashtag.name}',
            'result_type': 'mixed'
        }

    url = prepare_request(config.search_endpoint, url_params)

    data = requests.get(url)

    results = json.loads(data.text)

    return (hashtag, results, ) 
Example 11
Source File: handler.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def ExecuteRequest(self, request):
    """Executes an API invocation and returns the response object."""
    service = request.service_name()
    method = request.method()
    service_methods = SERVICE_PB_MAP.get(service, {})
    request_class, response_class = service_methods.get(method, (None, None))
    if not request_class:
      raise apiproxy_errors.CallNotFoundError()

    request_data = request_class()
    request_data.ParseFromString(request.request())
    response_data = response_class()

    if service in self.LOCAL_STUBS:
      self.LOCAL_STUBS[service].MakeSyncCall(service, method, request_data,
                                             response_data)
    else:
      apiproxy_stub_map.MakeSyncCall(service, method, request_data,
                                     response_data)

    return response_data 
Example 12
Source File: jupyter_kernel.py    From tensorlang with Apache License 2.0 5 votes vote down vote up
def execute_request(self, identities, msg):
    def schedule_next():
      print("schedule_next", self._pending_execute_request, self._pending_execute_requests)

      if len(self._pending_execute_requests) == 0:
        self._pending_execute_request = False
      else:
        identities2, msg2 = self._pending_execute_requests.pop(0)
        self._execute_request(schedule_next, identities2, msg2)

    if self._pending_execute_request:
      self._pending_execute_requests.append((identities, msg))
    else:
      self._execute_request(schedule_next, identities, msg) 
Example 13
Source File: googleplay.py    From googleplay-api with GNU General Public License v3.0 5 votes vote down vote up
def executeRequestApi2(self, path, post_data=None, content_type=CONTENT_TYPE_URLENC, params=None):
        if self.authSubToken is None:
            raise LoginError("You need to login before executing any request")
        headers = self.getHeaders()
        headers["Content-Type"] = content_type

        if post_data is not None:
            response = requests.post(path,
                                     data=str(post_data),
                                     headers=headers,
                                     params=params,
                                     verify=ssl_verify,
                                     timeout=60,
                                     proxies=self.proxies_config)
        else:
            response = requests.get(path,
                                    headers=headers,
                                    params=params,
                                    verify=ssl_verify,
                                    timeout=60,
                                    proxies=self.proxies_config)

        message = googleplay_pb2.ResponseWrapper.FromString(response.content)
        if message.commands.displayErrorMessage != "":
            raise RequestError(message.commands.displayErrorMessage)

        return message 
Example 14
Source File: enuminstances.py    From pywbem with GNU Lesser General Public License v2.1 5 votes vote down vote up
def execute_request(server_url, creds, namespace, classname):
    """ Open a connection with the server_url and creds, and
        enumerate instances defined by the functions namespace and
        classname arguments.
        Displays either the error return or the mof for instances
        returned.
    """

    print('Requesting url=%s, ns=%s, class=%s' % \
        (server_url, namespace, classname))

    try:
        # Create a connection
        CONN = WBEMConnection(server_url, creds,
                              default_namespace=namespace,
                              no_verification=True)

        #Issue the request to EnumerateInstances on the defined class
        INSTANCES = CONN.EnumerateInstances(classname)

        #Display of characteristics of the result object
        print('instances type=%s len=%s' % (type(INSTANCES),
                                            len(INSTANCES)))
        #display the mof output
        for inst in INSTANCES:
            print('path=%s\n' % inst.path)
            print(inst.tomof())

    # handle any exception
    except Error as err:
        # If CIMError, display CIMError attributes
        if isinstance(err, CIMError):
            print('Operation Failed: CIMError: code=%s, Description=%s' % \
                  (err.status_code_name, err.status_description))
        else:
            print ("Operation failed: %s" % err)
        sys.exit(1) 
Example 15
Source File: kernel.py    From reactivepy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def execute_request(self, stream, ident, parent):
        """handle an execute_request"""

        try:
            request = RequestInfo(stream, parent, ident)
        except BaseException:
            self.log.error("Got bad msg: ")
            self.log.error("%s", parent)
            return

        self._eventloop.create_task(
            self._inner_execute_request_callback(request)) 
Example 16
Source File: __init__.py    From graphql-django-view with MIT License 5 votes vote down vote up
def execute_graphql_request(self, request):
        query, variables, operation_name = self.get_graphql_params(request, self.parse_body(request))

        if not query:
            raise HttpError(HttpResponseBadRequest('Must provide query string.'))

        source = Source(query, name='GraphQL request')

        try:
            document_ast = parse(source)
            validation_errors = validate(self.schema, document_ast)
            if validation_errors:
                return ExecutionResult(
                    errors=validation_errors,
                    invalid=True,
                )
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True)

        if request.method.lower() == 'get':
            operation_ast = get_operation_ast(document_ast, operation_name)
            if operation_ast and operation_ast.operation != 'query':
                raise HttpError(HttpResponseNotAllowed(
                    ['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
                ))

        try:
            return self.execute(
                document_ast,
                root_value=self.get_root_value(request),
                variable_values=variables,
                operation_name=operation_name,
                context_value=self.get_context(request),
                executor=self.executor,
            )
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True) 
Example 17
Source File: ClientServicesManagerUD.py    From Pirates-Online-Rewritten with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def executeHttpRequest(url, **extras):
    timestamp = str(int(time.time()))
    signature = hmac.new(accountServerSecret, timestamp, hashlib.sha256)
    request = urllib2.Request(accountServerEndpoint + url)
    request.add_header('User-Agent', 'TTI-CSM')
    request.add_header('X-CSM-Timestamp', timestamp)
    request.add_header('X-CSM-Signature', signature.hexdigest())
    for k, v in extras.items():
        request.add_header('X-CSM-' + k, v)
    try:
        return urllib2.urlopen(request).read()
    except:
        return None 
Example 18
Source File: Gatt.py    From pybleno with MIT License 5 votes vote down vote up
def handleExecuteWriteRequest(self, request):
        response = None

        requestType = request[0]
        flag = request[1]

        if self._preparedWriteRequest:
            valueHandle = self._preparedWriteRequest['valueHandle']

            if flag == 0x00:
                response = array.array('B', [ATT_OP_EXEC_WRITE_RESP])
            elif flag == 0x01:
                def create_callback(requestType, valueHandle):
                    def callback(result):
                        callbackResponse = None

                        if ATT_ECODE_SUCCESS == result:
                            callbackResponse = array.array('B', [ATT_OP_EXEC_WRITE_RESP])
                        else:
                            callbackResponse = self.errorResponse(requestType, valueHandle, result)

                        # debug('execute write response: ' + callbackResponse.toString('hex'))

                        self.send(callbackResponse)

                    return callback

                callback = create_callback(requestType, self._preparedWriteRequest['valueHandle'])

                self._preparedWriteRequest['handle']['attribute'].emit('writeRequest',
                                                                       [self._preparedWriteRequest['data'],
                                                                        self._preparedWriteRequest['offset'], False,
                                                                        callback])
            else:
                response = self.errorResponse(requestType, 0x0000, ATT_ECODE_UNLIKELY)

            self._preparedWriteRequest = None
        else:
            response = self.errorResponse(requestType, 0x0000, ATT_ECODE_UNLIKELY)

        return response 
Example 19
Source File: client_runtime_context.py    From Office365-REST-Python-Client with MIT License 5 votes vote down vote up
def execute_request_direct(self, request):
        """

        :type request: RequestOptions
        """
        return self.get_pending_request().execute_request_direct(request) 
Example 20
Source File: graph_client.py    From Office365-REST-Python-Client with MIT License 5 votes vote down vote up
def execute_request(self, url_or_options):
        """
        Constructs and submits request directly

        :type url_or_options: str or RequestOptions
        """
        if not isinstance(url_or_options, RequestOptions):
            url_or_options = RequestOptions("{0}/{1}".format(self.__service_root_url, url_or_options))
        return self.execute_request_direct(url_or_options) 
Example 21
Source File: api.py    From azkaban-cli with MIT License 5 votes vote down vote up
def execute_request(session, host, session_id, project, flow, **execution_options):
    """Execute request for the Azkaban API

    :param session: A session for creating the request
    :type session: requests.Session
    :param str host: Hostname where the request should go
    :param str session_id: An id that the user should have when is logged in
    :param str project: Project name that contains the flow that will be executed on Azkaban
    :param str flow: Flow name to be executed on Azkaban
    :param \*\*execution_options: Optional parameters to execution
    :return: The response from the request made
    :rtype: requests.Response
    :raises requests.exceptions.ConnectionError: if cannot connect to host
    """

    params = {
        u'session.id': session_id,
        u'ajax': 'executeFlow',
        u'project': project,
        u'flow': flow
    }

    params.update(execution_options)

    response = session.get(
        host + '/executor',
        params=params
    )

    logging.debug("Response: \n%s", response.text)

    return response 
Example 22
Source File: eo_net.py    From eo-python with MIT License 5 votes vote down vote up
def execute_request(self, url, params=None, method="GET"):
        """Request the given URL with the given method and parameters.

        Args:
            url: The URL to call.
            params: The optional parameters.
            method: The HTTP request type {GET, POST, PUT, DELETE}.

        Returns:
            The server response or None.
        """
        self.check_request_rate()
        try:
            if method == "GET":
                return self.session.get(url, params=params)
            elif method == "POST":
                return self.session.post(url, params=params)
            elif method == "PUT":
                return self.session.put(url)
            elif method == "DELETE":
                return self.session.delete(url)
            else:
                self.logger.error("unknown request type: {0}".format(method))
        except Exception as e:
            self.logger.error("problem making HTTP request: {0}".format(e))
        return None 
Example 23
Source File: api.py    From baidupan_shell with GNU General Public License v2.0 5 votes vote down vote up
def execute_request(self, url, get_data=None, post_data=None):
        if get_data:
            get_data = urllib.urlencode(get_data)
            sep = '?' if url.find('?') < 0 else '&'
            url = '%s%s%s' % (url, sep, get_data)
        req = urllib2.Request(url)
        if post_data and len(post_data) > 0:
            req.add_data(urllib.urlencode(post_data))
        req.add_header('User-Agent', _USER_AGENT)
        req.add_header('Referer', 'http://pan.baidu.com/disk/home')
        resp = self._url_opener.open(req)
        return resp 
Example 24
Source File: pykms_RequestUnknown.py    From py-kms with The Unlicense 5 votes vote down vote up
def executeRequestLogic(self):
                finalResponse = bytearray()
                finalResponse.extend(bytearray(struct.pack('<I', 0)))
                finalResponse.extend(bytearray(struct.pack('<I', 0)))
                finalResponse.extend(bytearray(struct.pack('<I', ErrorCodes['SL_E_VL_KEY_MANAGEMENT_SERVICE_ID_MISMATCH'][0])))
                return finalResponse.decode('utf-8').encode('utf-8') 
Example 25
Source File: coap_http_proxy.py    From CoAPthon with MIT License 5 votes vote down vote up
def execute_http_request(method, uri, payload):
        response = None
        if method == Codes.GET.number:
            response = requests.get(url=uri)
        elif method == Codes.DELETE.number:
            response = requests.delete(url=uri)
        elif method == Codes.POST.number:
            response = requests.post(url=uri, data=payload)
        elif method == Codes.PUT.number:
            response = requests.put(url=uri, data=payload)
        return response 
Example 26
Source File: processor.py    From gauge-python with MIT License 5 votes vote down vote up
def process_execute_step_request(request):
    params = []
    for p in request.parameters:
        params.append(Table(p.table) if p.parameterType in [
            Parameter.Table, Parameter.Special_Table] else p.value)
    response = create_execution_status_response()
    info = registry.get_info_for(request.parsedStepText)
    execute_method(params, info, response, registry.is_continue_on_failure)
    return response 
Example 27
Source File: api_utils.py    From FATE with Apache License 2.0 5 votes vote down vote up
def request_execute_server(request, execute_host):
    try:
        endpoint = request.base_url.replace(request.host_url, '')
        method = request.method
        url = "http://{}/{}".format(execute_host, endpoint)
        audit_logger.info('sub request: {}'.format(url))
        action = getattr(requests, method.lower(), None)
        response = action(url=url, json=request.json, headers=HEADERS)
        return jsonify(response.json())
    except requests.exceptions.ConnectionError as e:
        return get_json_result(retcode=999, retmsg='please start fate flow server: {}'.format(execute_host))
    except Exception as e:
        raise Exception('local request error: {}'.format(e)) 
Example 28
Source File: utils_gsc.py    From gsc-logger with Apache License 2.0 5 votes vote down vote up
def execute_request(service, property_uri, request, max_retries=5, wait_interval=4,
                    retry_errors=(503, 500)):
    """
    Executes a searchanalytics request.
    Args:
        service: The webmasters service object/client to use for execution.
        property_uri: Matches the URI in Google Search Console.
        request: The request to be executed.
        max_retries. Optional. Sets the maximum number of retry attempts.
        wait_interval. Optional. Sets the number of seconds to wait between each retry attempt.
        retry_errors. Optional. Retry the request whenever these error codes are encountered.
    Returns:
        An array of response rows.
    """

    response = None
    retries = 0
    while retries <= max_retries:
        try:
            response = service.searchanalytics().query(siteUrl=property_uri, body=request).execute()
        except HttpError as err:
            decoded_error_body = err.content.decode('utf-8')
            json_error = json.loads(decoded_error_body)
            log.error(decoded_error_body)
            if json_error['error']['code'] in retry_errors:
                time.sleep(wait_interval)
                retries += 1
                continue
        break

    return response

# Given a site url string, loads all data for a particular date to BiqQuery 
Example 29
Source File: client.py    From Computable with MIT License 5 votes vote down vote up
def send_execute_request(self, socket, code, silent=True, metadata=None, ident=None):
        """construct and send an execute request via a socket.

        """

        if self._closed:
            raise RuntimeError("Client cannot be used after its sockets have been closed")
        
        # defaults:
        metadata = metadata if metadata is not None else {}

        # validate arguments
        if not isinstance(code, basestring):
            raise TypeError("code must be text, not %s" % type(code))
        if not isinstance(metadata, dict):
            raise TypeError("metadata must be dict, not %s" % type(metadata))
        
        content = dict(code=code, silent=bool(silent), user_variables=[], user_expressions={})


        msg = self.session.send(socket, "execute_request", content=content, ident=ident,
                            metadata=metadata)

        msg_id = msg['header']['msg_id']
        self.outstanding.add(msg_id)
        if ident:
            # possibly routed to a specific engine
            if isinstance(ident, list):
                ident = ident[-1]
            if ident in self._engines.values():
                # save for later, in case of engine death
                self._outstanding_dict[ident].add(msg_id)
        self.history.append(msg_id)
        self.metadata[msg_id]['submitted'] = datetime.now()

        return msg

    #--------------------------------------------------------------------------
    # construct a View object
    #-------------------------------------------------------------------------- 
Example 30
Source File: client.py    From dpa-pipe with MIT License 5 votes vote down vote up
def execute_request(self, action, data_type, primary_key=None, data=None,
        params=None, headers=None):

        # Never written this type of code before. If you are an experienced
        # web developer, please suggest improvements. Be gentle.

        # get method and url based on the action and data_type
        (http_method, url) = self._get_url(action, data_type,
            primary_key=primary_key)

        return self.execute_request_url(http_method, url, data, params, headers)

    # ------------------------------------------------------------------------- 
Example 31
Source File: httpclient.py    From cloud-ml-sdk with Apache License 2.0 5 votes vote down vote up
def execute_http_request(conf, params, headers):
  """
  :param conf: dict
    http connection configuration, for example:
    {
      "uri":"xxx",
      "method":"POST",
      "timeout":xxx,
    }
  :param params: dict
    http entity

  :param headers: dict
    http header settings

  :return: dict
    response json to dict
  """
  http_client = None
  response = None
  try:
    http_client = httplib.HTTPConnection(conf["host"], port=conf["port"], timeout=conf["timeout"])
    http_client.request(conf["method"], conf["resource"], params, headers)
    response = http_client.getresponse()
    msg = response.read()
    if response.status != 200:
      raise Exception("http response status code is error, the status code is %d, the response msg is %s" % (response.status, msg))
    response = json.loads(msg, encoding='utf-8')
  except Exception, e:
    print "exception: ", e 
Example 32
Source File: fetch_artifact.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def execute_request_with_retries(request):
  """Executes request and retries on failure."""
  result = None
  for _ in range(MAX_RETRIES):
    try:
      result = request.execute()
      break
    except Exception:
      pass

  return result 
Example 33
Source File: __init__.py    From ACE with Apache License 2.0 5 votes vote down vote up
def execute_request(self, remediation):
        logging.info(f"execution remediation {remediation}")
        message_id, recipient = remediation.key.split(':', 1)

        if 'fail' in message_id:
            raise 

        remediation.status = REMEDIATION_STATUS_COMPLETED
        remediation.successful = True
        remediation.result = 'executed by LogOnlyRemediationSystem'

        logging.info(f"completed remediation request {remediation}")
        return remediation 
Example 34
Source File: execute.py    From shoogle with GNU General Public License v3.0 5 votes vote down vote up
def execute_media_request(request):
    """Process a request containing a MediaFileUpload."""
    while 1:
        status, response = request.next_chunk()
        if status:
            config.logger.debug("MediaFileUpload status: {}".format(status))
        if response:
            return response 
Example 35
Source File: service.py    From django-sberbank with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def execute_request(self, data, method, payment=None):
        rest = method.startswith("rest/")

        headers = {
            "Accept": "application/json"
        }

        if rest:
            data.update({
                "userName": self.merchant.get('username'),
                'password': self.merchant.get('password')
            })
        else:
            headers.update({"Content-Type": "application/json"})
            data = json.dumps(data)

        try:
            response = requests.post(
                '{}/{}.do'.format(self.__default_gateway_address, method), data=data, headers=headers)
        except (requests.ConnectTimeout,
                requests.ConnectionError,
                requests.HTTPError):
            if payment:
                payment.status = Status.FAILED
                payment.save()
            raise NetworkException(payment.uid if payment else None)

        if rest:
            data.update({'password': '****'})

        LogEntry.objects.create(
            action=method,
            bank_id=payment.bank_id if payment else None,
            payment_id=payment.uid if payment else None,
            response_text=response.text, request_text=json.dumps(data) if rest else data)

        if response.status_code != 200:
            if payment:
                payment.status = Status.FAILED
                payment.save()
            raise ProcessingException(payment.uid if payment else None, response.text,
                                      response.status_code)
        try:
            response = response.json()
        except (ValueError, UnicodeDecodeError):
            if payment:
                payment.status = Status.FAILED
                payment.save()
            raise ProcessingException(payment.uid if payment.uid else None)

        if int(response.get('errorCode', 0)) != 0:
            if payment:
                payment.error_code = response.get('errorCode')
                payment.error_message = response.get('errorMessage')
                payment.status = Status.FAILED
                payment.save()
            raise ProcessingException(payment.uid if payment else None, response.get('errorMessage'),
                                      response.get('errorCode'))

        return response 
Example 36
Source File: _googleplayapi.py    From AppCrawler with GNU General Public License v3.0 4 votes vote down vote up
def executeRequestApi2(self, path, datapost=None, post_content_type="application/x-www-form-urlencoded; charset=UTF-8"):
        if (datapost is None and path in self.preFetch):
            data = self.preFetch[path]
        else:
            headers = { "Accept-Language": self.lang,
                                    "Authorization": "GoogleLogin auth=%s" % self.authSubToken,
                                    "X-DFE-Enabled-Experiments": "cl:billing.select_add_instrument_by_default",
                                    "X-DFE-Unsupported-Experiments": "nocache:billing.use_charging_poller,market_emails,buyer_currency,prod_baseline,checkin.set_asset_paid_app_field,shekel_test,content_ratings,buyer_currency_in_app,nocache:encrypted_apk,recent_changes",
                                    "X-DFE-Device-Id": self.androidId,
                                    "X-DFE-Client-Id": "am-android-google",
                                    #"X-DFE-Logging-Id": self.loggingId2, # Deprecated?
                                    # "User-Agent": "Android-Finsky/4.4.3 (api=3,versionCode=8016014,sdk=22,device=GT-I9300,hardware=aries,product=GT-I9300)",
                                    "User-Agent": "Android-Finsky/4.4.3 (api=3,versionCode=8016014,sdk=22,device=hammerhead,hardware=hammerhead,product=hammerhead)",
                                    # "User-Agent": "Android-Finsky/3.7.13 (api=3,versionCode=8013013,sdk=22,device=crespo,hardware=herring,product=soju)",
                                    "X-DFE-SmallestScreenWidthDp": "335",
                                    "X-DFE-Filter-Level": "3",
                                    "Accept-Encoding": "",
                                    "Host": "android.clients.google.com"}

            if datapost is not None:
                headers["Content-Type"] = post_content_type

            url = "https://android.clients.google.com/fdfe/%s" % path
            if datapost is not None:
                response = requests.post(url, data=datapost, headers=headers, proxies=self.proxy_dict, verify=True)
            else:
                response = requests.get(url, headers=headers, proxies=self.proxy_dict, verify=True)
            data = response.content
            #print(data)
        '''
        data = StringIO.StringIO(data)
        gzipper = gzip.GzipFile(fileobj=data)
        data = gzipper.read()
        '''
        message = _googleplay_pb2.ResponseWrapper.FromString(data)
        self._try_register_preFetch(message)

        # Debug
        #print text_format.MessageToString(message)
        return message

    #####################################
    # Google Play API Methods
    ##################################### 
Example 37
Source File: client.py    From anchore-engine with Apache License 2.0 4 votes vote down vote up
def execute_request(self, method, url, connect_timeout=None, read_timeout=None, retries=None):
        """
        Execute an HTTP request with auth params and the specified timeout overrides

        :param method: a callable for the http method to execute (e.g. requests.get, requests.put, ...)
        :param url:
        :param timeout:
        :param retries:
        :return:
        """

        # make a request
        if not connect_timeout:
            connect_timeout = int(self.auth_config['conn_timeout'])

        if not read_timeout:
            read_timeout = int(self.auth_config['read_timeout'])

        if not retries:
            retries = int(self.auth_config['max_retries'])
        retries = int(retries)

        verify = self.auth_config['verify']

        ret = {'status_code': 1, 'content': '', 'success': False}

        success = False
        count = 0

        conn_timeout = int(connect_timeout)
        read_timeout = int(read_timeout)

        while not success and count < retries:
            count += 1
            logger.debug("get attempt " + str(count) + " of " + str(retries))
            try:
                auth = (self.user, self.password)
                logger.debug("making authenticated request (user={}, conn_timeout={}, read_timeout={}, verify={}) to url {}".format(str(self.user), conn_timeout, read_timeout, verify, str(url)))
                r = method(url, auth=auth, timeout=(conn_timeout, read_timeout), verify=verify)
                logger.debug("\tresponse status_code: " + str(r.status_code))
                if r.status_code == 401:
                    logger.debug(
                        "Got HTTP 401 on authenticated {}, response body: {}".format(method.__name__, str(r.text)))
                    r.raise_for_status()
                elif r.status_code == 200:
                    success = True
                    ret['success'] = True
                elif r.status_code in [403, 404]:
                    r.raise_for_status()

                ret['status_code'] = r.status_code
                ret['content'] = r.content
            except requests.exceptions.ConnectTimeout as err:
                logger.debug("attempt failed: " + str(err))
                ret['content'] = ensure_bytes("server error: timed_out: " + str(err))
                # return(ret)

            except requests.HTTPError as e:
                if e.response is not None and 400 <= e.response.status_code < 500:
                    self._map_error_to_exception(e, username=self.user, url=url)
                    # raise e
                else:
                    logger.debug("attempt failed: " + str(e))
                    ret['content'] = ensure_bytes('server error: ' + str(e))
            except Exception as err:
                logger.debug("attempt failed: " + str(err))
                ret['content'] = ensure_bytes("server error: " + str(err))

        return ret 
Example 38
Source File: tornado_graphql_handler.py    From graphene-tornado with MIT License 4 votes vote down vote up
def execute_graphql_request(
        self,
        method: str,
        query: Optional[str],
        variables: Optional[Dict[str, str]],
        operation_name: Optional[str],
        show_graphiql: bool = False,
    ) -> Tuple[
        Optional[Union[Awaitable[ExecutionResult], ExecutionResult]], Optional[bool]
    ]:
        if not query:
            if show_graphiql:
                return None, None
            raise HTTPError(400, "Must provide query string.")

        parsing_ended = await self.extension_stack.parsing_started(query)
        try:
            self.document = parse(query)
            await parsing_ended()
        except GraphQLError as e:
            await parsing_ended(e)
            return ExecutionResult(errors=[e], data=None), True

        validation_ended = await self.extension_stack.validation_started()
        try:
            validation_errors = validate(self.schema.graphql_schema, self.document)
        except GraphQLError as e:
            await validation_ended([e])
            return ExecutionResult(errors=[e], data=None), True

        if validation_errors:
            await validation_ended(validation_errors)
            return ExecutionResult(errors=validation_errors, data=None,), True
        else:
            await validation_ended()

        if method.lower() == "get":
            operation_node = get_operation_ast(self.document, operation_name)
            if not operation_node:
                if show_graphiql:
                    return None, None
                raise HTTPError(
                    405,
                    "Must provide operation name if query contains multiple operations.",
                )

            if not operation_node.operation == OperationType.QUERY:
                if show_graphiql:
                    return None, None
                raise HTTPError(
                    405,
                    "Can only perform a {} operation from a POST request.".format(
                        operation_node.operation.value
                    ),
                )

        execution_ended = await self.extension_stack.execution_started(
            schema=self.schema.graphql_schema,
            document=self.document,
            root=self.root_value,
            context=self.context,
            variables=variables,
            operation_name=operation_name,
            request_context=self.request_context,
        )
        try:
            result = await self.execute(
                self.document,
                root_value=self.get_root(),
                variable_values=variables,
                operation_name=operation_name,
                context_value=self.context,
                middleware=self.get_middleware(),
            )
            await execution_ended()
        except GraphQLError as e:
            await execution_ended([e])
            return ExecutionResult(errors=[e], data=None), True

        return result, False 
Example 39
Source File: rpc.py    From LSP with MIT License 4 votes vote down vote up
def execute_request(
            self,
            request: Request,
            handler: Callable[[Optional[Any]], None],
            error_handler: Optional[Callable[[Any], None]] = None,
            timeout: float = DEFAULT_SYNC_REQUEST_TIMEOUT
    ) -> None:
        """
        Sends a request and waits for response up to timeout (default: 1 second), blocking the current thread.
        """
        if self.transport is None:
            debug('unable to send', request.method)
            return None

        result = None  # type: Any
        error = None  # type: Optional[Dict[str, Any]]
        exception = None  # type: Optional[Exception]
        with self._sync_request_cvar:
            try:
                self.request_id += 1
                request_id = self.request_id
                self.logger.outgoing_request(request_id, request.method, request.params, blocking=True)
                self._sync_request_result.prepare(request_id)  # After this, is_requesting() returns True.
                self.send_payload(request.to_payload(request_id))
                # We go to sleep. We wake up once another thread calls .notify() on this condition variable.
                if not self._sync_request_cvar.wait_for(self._sync_request_result.is_ready, timeout):
                    error = {"code": ErrorCode.Timeout, "message": "timeout on {}".format(request.method)}
                elif self._sync_request_result.has_error():
                    error = self._sync_request_result.flush_error()
                else:
                    result = self._sync_request_result.flush()
            except Exception as ex:
                exception = ex
            finally:
                self._sync_request_result.reset()
            self.flush_deferred_notifications()
            self.flush_deferred_responses()
        if exception is None:
            if error is not None:
                if error_handler is None:
                    self._error_display_handler(error["message"])
                else:
                    error_handler(error)
            else:
                handler(result) 
Example 40
Source File: views.py    From graphene-django with MIT License 4 votes vote down vote up
def execute_graphql_request(
        self, request, data, query, variables, operation_name, show_graphiql=False
    ):
        if not query:
            if show_graphiql:
                return None
            raise HttpError(HttpResponseBadRequest("Must provide query string."))

        try:
            backend = self.get_backend(request)
            document = backend.document_from_string(self.schema, query)
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True)

        if request.method.lower() == "get":
            operation_type = document.get_operation_type(operation_name)
            if operation_type and operation_type != "query":
                if show_graphiql:
                    return None

                raise HttpError(
                    HttpResponseNotAllowed(
                        ["POST"],
                        "Can only perform a {} operation from a POST request.".format(
                            operation_type
                        ),
                    )
                )

        try:
            extra_options = {}
            if self.executor:
                # We only include it optionally since
                # executor is not a valid argument in all backends
                extra_options["executor"] = self.executor

            return document.execute(
                root_value=self.get_root_value(request),
                variable_values=variables,
                operation_name=operation_name,
                context_value=self.get_context(request),
                middleware=self.get_middleware(request),
                **extra_options
            )
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True) 
Example 41
Source File: client.py    From dpa-pipe with MIT License 4 votes vote down vote up
def execute_request_url(self, http_method, url, data=None, params=None,
        headers=None):

        data_format = self.data_format

        if params:
            params = self._sanitize_params(params)

        if data:
            data = json.dumps(data)

        if data and not headers:
            if data_format == 'json':
                headers = {'content-type': 'application/json'}
            else:
                raise RestfulClientError("Unknown data format: " + data_format)

        # requests method based on the supplied http method name.
        # it *should* be a 1 to 1 lc mapping. (GET==get, PUT==put, etc.)
        requests_method_name = http_method.lower()

        # see if the requests api has a method that matches
        try:
            requests_method = getattr(requests, requests_method_name)
        except AttributeError:
            raise RestfulClientError(
                "Unknown method for requests: " + str(requests_method_name))

        # execute the request
        response = self._try_request(requests_method, url, params=params,
            data=data, headers=headers)

        # raise a custom exception if 400/500 error
        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError as e:
            raise RestfulClientError(e.response.text)

        # deserialize the returned data. may want to flesh this out at some
        # point to allow for additional data formats
        if data_format == 'json':
            # json.loads(response.content) results in unicode instead of
            # str. using yaml results in string values. so using that for now.
            return yaml.load(response.content)
        else:
            raise RestfulClientError("Unknown data format: " + data_format)

    # -------------------------------------------------------------------------
    # Private class methods:
    # ------------------------------------------------------------------------- 
Example 42
Source File: http_request_utils.py    From botbuilder-python with MIT License 4 votes vote down vote up
def execute_http_request(
        self,
        request_url: str,
        payload_body: object,
        endpoint: QnAMakerEndpoint,
        timeout: float = None,
    ) -> ClientResponse:
        """
        Execute HTTP request.

        Parameters:
        -----------

        request_url: HTTP request URL.

        payload_body: HTTP request body.

        endpoint: QnA Maker endpoint details.

        timeout: Timeout for HTTP call (milliseconds).
        """
        if not request_url:
            raise TypeError(
                "HttpRequestUtils.execute_http_request(): request_url cannot be None."
            )

        if not payload_body:
            raise TypeError(
                "HttpRequestUtils.execute_http_request(): question cannot be None."
            )

        if not endpoint:
            raise TypeError(
                "HttpRequestUtils.execute_http_request(): endpoint cannot be None."
            )

        serialized_payload_body = json.dumps(payload_body.serialize())

        headers = self._get_headers(endpoint)

        if timeout:
            # Convert miliseconds to seconds (as other BotBuilder SDKs accept timeout value in miliseconds)
            # aiohttp.ClientSession units are in seconds
            request_timeout = ClientTimeout(total=timeout / 1000)

            response: ClientResponse = await self._http_client.post(
                request_url,
                data=serialized_payload_body,
                headers=headers,
                timeout=request_timeout,
            )
        else:
            response: ClientResponse = await self._http_client.post(
                request_url, data=serialized_payload_body, headers=headers
            )

        return response