Ejemplo n.º 1
0
 def execute(self, request, params):
     """
     Executes a transport request
     @param request: request object
     @type request: pylastica.request.Request
     @param params: hostname, port, path, etc.
     @type params: dict
     @return:
     @rtype: pylastica.response.Response
     """
     connection = self.connection
     url = connection.get_config('url') if connection.has_config(
         'url') else ''
     if url != '' and url is not None:
         base_url = url
     else:
         connection_data = {
             'scheme': self._scheme,
             'host': connection.host,
             'port': connection.port,
             'path': connection.path
         }
         base_url = "%(scheme)s://%(host)s:%(port)s/%(path)s" % connection_data
     base_url += request.path
     query = request.query  #http query params
     if connection.has_config('headers'):
         for key, value in connection.get_config('headers'):
             self.setHeader(key, value)
     data = request.get_data()
     http_method = request.get_method()
     content = None
     if data is not None:
         if self.has_param('postWithRequestBody') and self.get_param(
                 'postWithRequestBody'):
             http_method = self.METHOD_POST
         if isinstance(data, dict) or isinstance(data, list):
             content = json.dumps(data)
         else:
             content = data
     #TODO: something for debugging
     response = self.request(http_method, base_url, query, content)
     response_object = pylastica.response.Response(response.data,
                                                   response.status)
     return response_object
Ejemplo n.º 2
0
 def execute(self, request, params):
     """
     Executes a transport request
     @param request: request object
     @type request: pylastica.request.Request
     @param params: hostname, port, path, etc.
     @type params: dict
     @return:
     @rtype: pylastica.response.Response
     """
     connection = self.connection
     url = connection.get_config('url') if connection.has_config('url') else ''
     if url != '' and url is not None:
         base_url = url
     else:
         connection_data = {
             'scheme': self._scheme,
             'host': connection.host,
             'port': connection.port,
             'path': connection.path
         }
         base_url = "%(scheme)s://%(host)s:%(port)s/%(path)s" % connection_data
     base_url += request.path
     query = request.query #http query params
     if connection.has_config('headers'):
         for key, value in connection.get_config('headers'):
             self.setHeader(key, value)
     data = request.get_data()
     http_method = request.get_method()
     content = None
     if data is not None:
         if self.has_param('postWithRequestBody') and self.get_param('postWithRequestBody'):
             http_method = self.METHOD_POST
         if isinstance(data, dict) or isinstance(data, list):
             content = json.dumps(data)
         else:
             content = data
     #TODO: something for debugging
     response = self.request(http_method, base_url, query, content)
     response_object = pylastica.response.Response(response.data, response.status)
     return response_object
Ejemplo n.º 3
0
    def execute(self, request, params):
        """
        Executes a transport request
        @param request: request object
        @type request: pylastica.request.Request
        @param params: hostname, port, path, etc.
        @type params: dict
        @return:
        @rtype: pylastica.response.Response
        """
        connection = self.connection
        send_timeout = int(connection.get_config('send_timeout')) if connection.has_config('send_timeout') else None
        recv_timeout = int(connection.get_config('recv_timeout')) if connection.has_config('recv_timeout') else None
        framed_transport = bool(connection.get_config('framed_transport')) if connection.has_config('framed_transport') else False
        try:
            client = self._get_client(connection.host, connection.port, send_timeout, recv_timeout, framed_transport)
            rest_request = Rest.RestRequest()
            rest_request.method = Rest.Method._NAMES_TO_VALUES[request.get_method()]
            rest_request.uri = request.path

            query = request.query
            if query is not None:
                rest_request.parameters = query
            data = request.get_data()
            if data is not None:
                if isinstance(data, dict):
                    content = json.dumps(data)
                else:
                    content = data
                rest_request.body = content
            result = client.execute(rest_request)
            response = pylastica.response.Response(result.body)
        except thrift.Thrift.TException as e:
            response = pylastica.response.Response('')
            raise pylastica.exception.connection.ThriftException(e, request, response)
        if response.has_error():
            raise pylastica.exception.ResponseException(request, response)
        return response