本文整理汇总了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)
示例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
示例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
示例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
示例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
示例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())
示例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
示例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
示例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
示例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
示例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
示例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