コード例 #1
0
ファイル: async.py プロジェクト: MostAwesomeDude/gentleman
    def request(self, method, path, query=None, content=None):
        """
        Sends an HTTP request.

        This constructs a full URL, encodes and decodes HTTP bodies, and
        handles invalid responses in a pythonic way.

        @type method: string
        @param method: HTTP method to use
        @type path: string
        @param path: HTTP URL path
        @type query: list of two-tuples
        @param query: query arguments to pass to urllib.urlencode
        @type content: str or None
        @param content: HTTP body content

        @rtype: object
        @return: JSON-Decoded response

        @raises GanetiApiError: If an invalid response is returned
        """

        if not path.startswith("/"):
            raise ClientError("Implementation error: Called with bad path %s"
                              % path)

        body = None

        if content is not None:
            data = self._json_encoder.encode(content)
            body = StringProducer(data)

        url = self._base_url + path

        if query:
            prepare_query(query)
            params = urlencode(query, doseq=True)
            url += "?%s" % params

        log.msg("Sending request to %s %s %s" % (url, self.headers, body),
                system="Gentleman")

        d = self._agent.request(method, url, headers=self.headers,
                                bodyProducer=body)

        protocol = JsonResponseProtocol(d)

        @d.addErrback
        def connectionFailed(failure):
            failure.trap(ConnectionRefusedError)
            raise GanetiApiError("Connection refused!")

        @d.addCallback
        def cb(response):
            if response.code != 200:
                raise NotOkayError(code=response.code)
            response.deliverBody(protocol)

        return protocol.getData()
コード例 #2
0
ファイル: async.py プロジェクト: nibalizer/gentleman
    def request(self, method, path, query=None, content=None):
        """
        Sends an HTTP request.

        This constructs a full URL, encodes and decodes HTTP bodies, and
        handles invalid responses in a pythonic way.

        @type method: string
        @param method: HTTP method to use
        @type path: string
        @param path: HTTP URL path
        @type query: list of two-tuples
        @param query: query arguments to pass to urllib.urlencode
        @type content: str or None
        @param content: HTTP body content

        @rtype: object
        @return: JSON-Decoded response

        @raises GanetiApiError: If an invalid response is returned
        """

        if not path.startswith("/"):
            raise ClientError("Implementation error: Called with bad path %s"
                              % path)

        kwargs = {
            "verify": False,
        }

        if self.username and self.password:
            kwargs["auth"] = self.username, self.password

        if content is not None:
            kwargs["data"] = self._json_encoder.encode(content)

        if query:
            prepare_query(query)
            kwargs["params"] = query

        url = self._base_url + path

        log.msg("Sending request to %s %s" % (url, kwargs))

        d = self._agent.request(method, url, headers=headers,
                                bodyProducer=None)

        protocol = JsonResponseProtocol()

        @d.addErrback
        def connectionFailed(failure):
            failure.trap(ConnectionRefusedError)
            raise GanetiApiError("Connection refused!")

        @d.addCallback
        def cb(response):
            response.deliverBody(protocol)

        return protocol.d
コード例 #3
0
ファイル: sync.py プロジェクト: MostAwesomeDude/gentleman
    def request(self, method, path, query=None, content=None):
        """
        Sends an HTTP request.

        This constructs a full URL, encodes and decodes HTTP bodies, and
        handles invalid responses in a pythonic way.

        @type method: string
        @param method: HTTP method to use
        @type path: string
        @param path: HTTP URL path
        @type query: list of two-tuples
        @param query: query arguments to pass to urllib.urlencode
        @type content: str or None
        @param content: HTTP body content

        @rtype: object
        @return: JSON-Decoded response

        @raises GanetiApiError: If an invalid response is returned
        """

        kwargs = {
            "headers": headers,
            "timeout": self.timeout,
            "verify": False,
        }

        if self.username and self.password:
            kwargs["auth"] = self.username, self.password

        if content is not None:
            kwargs["data"] = self._json_encoder.encode(content)

        if query:
            prepare_query(query)
            kwargs["params"] = query

        url = self._base_url + path

        # print "Sending request to %s %s" % (url, kwargs)

        try:
            r = requests.request(method, url, **kwargs)
        except requests.ConnectionError:
            raise GanetiApiError("Couldn't connect to %s" % self._base_url)
        except requests.Timeout:
            raise GanetiApiError("Timed out connecting to %s" %
                                 self._base_url)

        if r.status_code != requests.codes.ok:
            raise NotOkayError(str(r.status_code), code=r.status_code)

        if r.content:
            return json.loads(r.content)
        else:
            return None
コード例 #4
0
 def test_bool_to_int(self):
     d = {"test": True}
     prepare_query(d)
     self.assertEqual(d["test"], 1)
     self.assertEqual(type(d["test"]), int)