當前位置: 首頁>>代碼示例>>Python>>正文


Python treq.json_content方法代碼示例

本文整理匯總了Python中treq.json_content方法的典型用法代碼示例。如果您正苦於以下問題:Python treq.json_content方法的具體用法?Python treq.json_content怎麽用?Python treq.json_content使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在treq的用法示例。


在下文中一共展示了treq.json_content方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: check_and_decode_json

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def check_and_decode_json(result, response_code):
    """
    Given ``treq`` response object, extract JSON and ensure response code
    is the expected one.

    :param result: ``treq`` response.
    :param int response_code: Expected response code.

    :return: ``Deferred`` firing with decoded JSON.
    """
    def error(body):
        raise ResponseError(result.code, body)

    if result.code != response_code:
        d = content(result)
        d.addCallback(error)
        return d

    return json_content(result) 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:21,代碼來源:testtools.py

示例2: test_empty_endpoints

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def test_empty_endpoints(self):
        """
        The proxy passes through requests when no endpoints are specified.

        In particular, when POST to the /towel endpoint on the *proxy*, we get
        to see that we were seen by the (admittedly fake) Docker daemon.
        """
        self._configure("endpoints: {}\nadapters: {}")
        d = self.client.post('http://127.0.0.1:%d/towel' % (self.proxyPort,),
                      json.dumps({"hiding": "things"}),
                      headers={'Content-Type': ['application/json']})
        d.addCallback(treq.json_content)
        def verify(response):
            self.assertEqual(response,
                    {"hiding": "things", "SeenByFakeDocker": 42})
        d.addCallback(verify)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:19,代碼來源:test_core.py

示例3: test_endpoint_and_empty_hooks

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def test_endpoint_and_empty_hooks(self):
        """
        An endpoint is specified, but no pre-or post hooks are added to it.
        Requests to the endpoint are proxied.
        """
        endpoint = "/towel"
        self._configure("""endpoints:
  "POST %s":
    pre: []
    post: []
adapters: {}""" % (endpoint,))
        d = self.client.post('http://127.0.0.1:%d%s' % (self.proxyPort, endpoint),
                             json.dumps({"hiding": "things"}),
                             headers={'Content-Type': ['application/json']})
        d.addCallback(treq.json_content)
        def verify(response):
            self.assertEqual(response,
                    {"hiding": "things", "SeenByFakeDocker": 42})
        d.addCallback(verify)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:22,代碼來源:test_core.py

示例4: _hookTest

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def _hookTest(self, config_yml, adderArgs=dict(pre=True), adderTwoArgs=dict(pre=True)):
        """
        Generalised version of a pre-hook test.
        """
        self._getAdder(**adderArgs)
        self._getAdderTwo(**adderTwoArgs)
        self.dockerEndpoint = "/towel"
        self.adapterEndpoint = "/adapter"
        self.args = dict(dockerEndpoint=self.dockerEndpoint,
                         adapterEndpoint=self.adapterEndpoint,
                         adderPort=self.adderPort,
                         adderTwoPort=self.adderTwoPort)
        self._configure(config_yml % self.args)
        self.args["proxyPort"] = self.proxyPort
        d = self.client.post('http://127.0.0.1:%(proxyPort)d%(dockerEndpoint)s' % self.args,
                      json.dumps({"Number": 1}),
                      headers={'Content-Type': ['application/json']})
        d.addCallback(treq.json_content)
        def debug(result, *args, **kw):
            return result
        d.addCallback(debug)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:24,代碼來源:test_core.py

示例5: submit

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def submit(server_url, result):
    """
    Post the given result to the given server.

    :param str server_url: The server's URL.
    :param dict result: The JSON-compatible result.
    :return: Deferred that fires with an ID of the submitted result on success
        or with SubmitFailure if the result is rejected by the server.

    This function may also return any of the ``treq`` failures.
    """
    req = post(
        server_url + "/v1/benchmark-results",
        json.dumps(result),
        headers=({'Content-Type': ['application/json']}),
    )

    def get_response_content(response):
        d = json_content(response)
        d.addCallback(lambda content: (response, content))
        return d

    req.addCallback(get_response_content)

    def process_response(response_and_content):
        (response, content) = response_and_content
        if response.code != CREATED:
            raise SubmitFailure(response.code, response.phrase,
                                content['message'])
        else:
            return content['id']

    req.addCallback(process_response)
    return req 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:36,代碼來源:submit.py

示例6: from_url

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def from_url(
        cls, reactor, url, key, alg=RS256,
        jws_client=None, timeout=_DEFAULT_TIMEOUT,
            ):
        """
        Construct a client from an ACME directory at a given URL.

        :param url: The ``twisted.python.url.URL`` to fetch the directory from.
            See `txacme.urls` for constants for various well-known public
            directories.
        :param reactor: The Twisted reactor to use.
        :param ~josepy.jwk.JWK key: The client key to use.
        :param alg: The signing algorithm to use.  Needs to be compatible with
            the type of key used.
        :param JWSClient jws_client: The underlying client to use, or ``None``
            to construct one.
        :param int timeout: Number of seconds to wait for an HTTP response
            during ACME server interaction.

        :return: The constructed client.
        :rtype: Deferred[`Client`]
        """
        action = LOG_ACME_CONSUME_DIRECTORY(
            url=url, key_type=key.typ, alg=alg.name)
        with action.context():
            check_directory_url_type(url)
            jws_client = _default_client(jws_client, reactor, key, alg)
            jws_client.timeout = timeout
            return (
                DeferredContext(jws_client.get(url.asText()))
                .addCallback(json_content)
                .addCallback(messages.Directory.from_json)
                .addCallback(
                    tap(lambda d: action.add_success_fields(directory=d)))
                .addCallback(cls, reactor, key, jws_client)
                .addActionFinish()) 
開發者ID:twisted,項目名稱:txacme,代碼行數:38,代碼來源:client.py

示例7: test_empty_endpoints_socket

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def test_empty_endpoints_socket(self):
        """
        The proxy is able to connect to Docker on a UNIX socket.
        """
        self._configure("endpoints: {}\nadapters: {}", dockerOnSocket=True)
        d = self.client.post('http://127.0.0.1:%d/towel' % (self.proxyPort,),
                      json.dumps({"hiding": "things"}),
                      headers={'Content-Type': ['application/json']})
        d.addCallback(treq.json_content)
        def verify(response):
            self.assertEqual(response,
                    {"hiding": "things", "SeenByFakeDocker": 42})
        d.addCallback(verify)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:16,代碼來源:test_core.py

示例8: test_douglas_adams_would_be_proud

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def test_douglas_adams_would_be_proud(self):
        d = self.client.post('http://127.0.0.1:%d/towel' % (self.dockerPort,),
                      json.dumps({"hiding": "things"}),
                      headers={'Content-Type': ['application/json']})
        d.addCallback(treq.json_content)
        def verify(response):
            self.assertEqual(response,
                    {"hiding": "things", "SeenByFakeDocker": 42})
        d.addCallback(verify)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:12,代碼來源:test_testtools.py

示例9: test_adder_post

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def test_adder_post(self):
        """
        The adder post-hook increments an integer according to the protocol
        defined in the README.
        """
        self._getAdder(post=True)
        d = self.client.post('http://127.0.0.1:%d/adapter' % (self.adderPort,),
                      json.dumps({
                          "Type": "post-hook",
                          "ClientRequest": {
                              "Method": "POST",
                              "Request": "/fictional",
                              "Body": json.dumps({}),},
                          "ServerResponse": {
                              "ContentType": "application/json",
                              "Body": json.dumps({"Number": 7}),
                              "Code": 200,},
                          }),
                      headers={'Content-Type': ['application/json']})
        def verifyResponseCode(response):
            self.assertEqual(response.code, 200)
            return response
        d.addCallback(verifyResponseCode)
        d.addCallback(treq.json_content)
        def verify(body):
            self.assertEqual(json.loads(body["ModifiedServerResponse"]["Body"])["Number"], 8)
        d.addCallback(verify)
        return d 
開發者ID:ClusterHQ,項目名稱:powerstrip,代碼行數:30,代碼來源:test_testtools.py

示例10: _pod_usage_from_client

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def _pod_usage_from_client(self, client, base_url):
        d = self.kubernetes.versioned_client()
        d.addCallback(lambda client: client.list(client.model.v1.Namespace))

        def got_namespaces(namespaces):
            d = gatherResults(
                client.get(
                    base_url.asText() + self.pod_location(ns.metadata.name)
                ).addCallback(
                    json_content
                )
                for ns
                in namespaces.items
            )

            def combine(pod_usages):
                result = []
                for usage in pod_usages:
                    if usage["items"] is None:
                        continue
                    for item in usage["items"]:
                        result.append(item)
                return {"items": result}
            d.addCallback(combine)
            return d
        d.addCallback(got_namespaces)
        return d 
開發者ID:LeastAuthority,項目名稱:kubetop,代碼行數:29,代碼來源:_topdata.py

示例11: _node_usage_from_client

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def _node_usage_from_client(self, client, base_url):
        d = client.get(base_url.asText() + self.node_location())
        d.addCallback(json_content)
        return d 
開發者ID:LeastAuthority,項目名稱:kubetop,代碼行數:6,代碼來源:_topdata.py

示例12: _node_info_from_client

# 需要導入模塊: import treq [as 別名]
# 或者: from treq import json_content [as 別名]
def _node_info_from_client(self, client, base_url):
        d = client.get(base_url.asText() + "/api/v1/nodes")
        d.addCallback(json_content)
        return d 
開發者ID:LeastAuthority,項目名稱:kubetop,代碼行數:6,代碼來源:_topdata.py


注:本文中的treq.json_content方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。