本文整理汇总了Python中treq.json_content函数的典型用法代码示例。如果您正苦于以下问题:Python json_content函数的具体用法?Python json_content怎么用?Python json_content使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了json_content函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _on_response
def _on_response(response):
if response.code == 201:
json_content(response).addCallback(_construct_message, response)
elif response.code == 204:
reactor.callLater(polling_interval, _perform_call)
else:
return self._handle_error_response(response)
示例2: setUp
def setUp(self):
"""
Create a :obj:`MimicCore` with :obj:`LoadBalancerApi` as the only plugin.
And create a load balancer and add nodes to the load balancer.
"""
helper = APIMockHelper(self, [LoadBalancerApi()])
self.root = helper.root
self.uri = helper.uri
create_lb = request(
self, self.root, "POST", self.uri + '/loadbalancers',
json.dumps({
"loadBalancer": {
"name": "test_lb",
"protocol": "HTTP",
"virtualIps": [{"type": "PUBLIC"}]
}
})
)
create_lb_response = self.successResultOf(create_lb)
self.create_lb_response_body = self.successResultOf(treq.json_content(
create_lb_response))
create_node = request(
self, self.root, "POST", self.uri + '/loadbalancers/' +
str(self.create_lb_response_body["loadBalancer"]["id"]) + '/nodes',
json.dumps({"nodes": [{"address": "127.0.0.1",
"port": 80,
"condition": "ENABLED",
"type": "PRIMARY",
"weight": 10}]})
)
self.create_node_response = self.successResultOf(create_node)
self.create_node_response_body = self.successResultOf(treq.json_content(
self.create_node_response))
self.node = self.create_node_response_body["nodes"]
示例3: test_server_in_building_state_for_specified_time
def test_server_in_building_state_for_specified_time(self):
"""
Test to verify :func:`create_server` creates a server in BUILD
status for the time specified in the metadata.
"""
metadata = {"server_building": 1}
# create server with metadata to keep the server in building state for
# 3 seconds
create_server_response = self.create_server(metadata=metadata)
# verify the create server was successful
self.assertEquals(create_server_response.code, 202)
create_server_response_body = self.successResultOf(
treq.json_content(create_server_response))
# get server and verify status is BUILD
get_server = request(self, self.root, "GET", self.uri + '/servers/' +
create_server_response_body["server"]["id"])
get_server_response = self.successResultOf(get_server)
get_server_response_body = self.successResultOf(
treq.json_content(get_server_response))
self.assertEquals(get_server_response_body['server']['status'], "BUILD")
# Time Passes...
self.helper.clock.advance(2.0)
# get server and verify status changed to active
get_server = request(self, self.root, "GET", self.uri + '/servers/' +
create_server_response_body["server"]["id"])
get_server_response = self.successResultOf(get_server)
get_server_response_body = self.successResultOf(
treq.json_content(get_server_response))
self.assertEquals(get_server_response_body['server']['status'], "ACTIVE")
示例4: check
def check(self):
# Hax
try:
irc = self.master.modules["irc"]
words = yield self.config.get("words", [])
response = yield treq.get("http://a.4cdn.org/a/threads.json")
threads = yield treq.json_content(response)
nthreads, check = {}, []
for o in threads:
for t in o["threads"]:
nthreads[t["no"]] = t["last_modified"]
if t["no"] not in self.threads:
self.threads[t["no"]] = 0
if t["last_modified"] > self.threads[t["no"]]:
check.append(t["no"])
if not self.initial:
threads = []
for t in check:
response = yield treq.get("http://a.4cdn.org/a/res/{:d}.json".format(t))
if response.code == 200:
data = yield treq.json_content(response)
found, posts = set(), []
for p in data["posts"]:
if p["time"] > self.threads[t] and "com" in p:
f = set(filter(lambda x: re.search(r"\b" + x.lower() + r"\b", p["com"].lower()), words))
if f:
found.update(f)
posts.append((p["no"], p["com"]))
if found and posts:
threads.append((t, found, posts))
if len(threads) < 5:
for t, found, posts in threads:
p, also = posts[0], (u"(also {} in same thread)".format(", ".join([str(x[0]) for x in posts[1:]])) if posts[1:] else u"")
url = "https://archive.foolz.us/a/thread/{:d}/#{:d}".format(t, p[0])
#response = yield treq.post("https://www.googleapis.com/urlshortener/v1/url?key="+API_KEY, json.dumps({"longUrl": url}), headers={'Content-Type': ['application/json']})
#data = yield treq.json_content(response)
#url = data["id"]
excerpt = p[1].replace("<br>", "\n")
excerpt = BeautifulSoup(excerpt).get_text()
excerpt = re.sub("\s+", " ", excerpt)
excerpt = excerpt if len(excerpt) <= 100 else excerpt[:97]+"..."
irc.msg(u"#commie-subs", u"\u00039>{} mentioned on /a/ at {} [{}] {}".format(u", ".join(found), url, excerpt, also))
else:
t, found = [str(x[0]) for x in threads], reduce(lambda a,b: a|b, [x[1] for x in threads])
irc.msg(u"#commie-subs", u"\u00039>{} flooded /a/ in threads {}".format(u", ".join(found), u", ".join(t)))
self.threads = nthreads
self.initial = False
except:
self.err(u"4chan module broke horribly :(")
示例5: lookupCharacters
def lookupCharacters(member, characters):
response = yield treq.get("http://www.bungie.net/Platform/User/GetBungieAccount/" + member["membershipId"].encode("UTF-8") + "/254/")
data = yield treq.json_content(response)
for account in data["Response"]["destinyAccounts"]:
if account["userInfo"]["membershipType"] == 1:
platform = "xbox"
elif account["userInfo"]["membershipType"] == 2:
platform = "playstation"
else:
continue
for character in account["characters"]:
character_data = {
"bungieId": member["membershipId"],
"accountId": account["userInfo"]["membershipId"],
"characterId": character["characterId"],
"name": account["userInfo"]["displayName"],
"race": character["race"]["raceName"],
"gender": character["gender"]["genderName"],
"class": character["characterClass"]["className"],
"level": character["level"],
"levelString": "{:,d}".format(character["level"]),
"icon": "http://bungie.net" + character["emblemPath"],
"background": "http://bungie.net" + character["backgroundPath"],
# Default values for extra data (in case it fails)
"light": 0,
"lightString": "{:,d}".format(0),
"grimoire": 0,
"grimoireString": "{:,d}".format(0),
"minutesPlayed": 0,
"minutesPlayedString": "{:,d}".format(0),
"lastSeen": "",
"lastSeenString": ""
}
character_data["style"] = 'background: url("' + character_data["background"] + '")'
try:
response = yield treq.get("http://www.bungie.net/Platform/Destiny/{!s}/Account/{!s}/Character/{!s}/".format(account["userInfo"]["membershipType"], account["userInfo"]["membershipId"], character["characterId"]))
extra_data = yield treq.json_content(response)
extra_data = extra_data["Response"]["data"]["characterBase"]
character_data["light"] = extra_data["stats"]["STAT_LIGHT"]["value"]
character_data["lightString"] = "{:,d}".format(character_data["light"])
character_data["grimoire"] = extra_data["grimoireScore"]
character_data["grimoireString"] = "{:,d}".format(character_data["grimoire"])
character_data["minutesPlayed"] = int(extra_data["minutesPlayedTotal"])
character_data["minutesPlayedString"] = "{:,d}".format(character_data["minutesPlayed"])
character_data["lastSeen"] = extra_data["dateLastPlayed"]
character_data["lastSeenString"] = datetime.datetime.strptime(extra_data["dateLastPlayed"], "%Y-%m-%dT%H:%M:%SZ").strftime("%B %d, %I:%M%p")
except:
pass
characters[platform].append(character_data)
示例6: request_done
def request_done(response):
def _raise_error(body):
exceptions.raise_exceptions(response.code, body)
return body
return treq.json_content(response.original).addCallback(
_raise_error)
示例7: _fetch_ticket
def _fetch_ticket(self, ticket):
"""
Fetch a ticket representation from CouchDB.
"""
url = '''%(scheme)s%(host)s:%(port)s/%(db)s/_design/views/_view/get_ticket''' % {
'scheme': self._scheme,
'host': self._couch_host,
'port': self._couch_port,
'db': self._couch_db}
url = url.encode('utf-8')
params = {'key': json.dumps(ticket.encode('utf-8'))}
self.debug("[DEBUG][CouchDB] _fetch_ticket(), url: %s" % url)
self.debug("[DEBUG][CouchDB] _fetch_ticket(), params: %s" % str(params))
httpClient = self.httpClientFactory(self.reactor)
response = yield httpClient.get(url,
params=params,
headers=Headers({'Accept': ['application/json']}),
auth=(self._couch_user, self._couch_passwd))
response = yield http_status_filter(response, [(200,200)], CouchDBError)
doc = yield treq.json_content(response)
rows = doc[u'rows']
if len(rows) > 0:
entry = rows[0][u'value']
entry[u'expires'] = parse_date(entry[u'expires'])
if u'pgts' in entry:
entry[u'pgts'] = set(entry[u'pgts'])
defer.returnValue(entry)
defer.returnValue(None)
示例8: test_add_load_balancer_with_nodes
def test_add_load_balancer_with_nodes(self):
"""
Test to verify :func:`add_load_balancer` on ``POST /v1.0/<tenant_id>/loadbalancers``,
with nodes
"""
lb_name = 'mimic_lb'
create_lb = request(
self, self.root, "POST", self.uri + '/loadbalancers',
json.dumps({
"loadBalancer": {
"name": lb_name,
"protocol": "HTTP",
"virtualIps": [{"type": "PUBLIC"}],
"nodes": [{"address": "127.0.0.2",
"port": 80,
"condition": "ENABLED",
"type": "PRIMARY"},
{"address": "127.0.0.0",
"port": 80,
"condition": "ENABLED",
"type": "SECONDARY"}]
}
})
)
create_lb_response = self.successResultOf(create_lb)
create_lb_response_body = self.successResultOf(treq.json_content(create_lb_response))
self.assertEqual(create_lb_response.code, 202)
self.assertEqual(len(create_lb_response_body['loadBalancer']['nodes']), 2)
示例9: test_get_server_ips
def test_get_server_ips(self):
"""
Test to verify :func:`get_ips` on ``GET /v2.0/<tenant_id>/servers/<server_id>/ips``
"""
get_server_ips = request(self, self.root, "GET",
self.uri + '/servers/' + self.server_id + '/ips')
get_server_ips_response = self.successResultOf(get_server_ips)
get_server_ips_response_body = self.successResultOf(
treq.json_content(get_server_ips_response))
self.assertEqual(get_server_ips_response.code, 200)
list_servers_detail = request(self, self.root, "GET", self.uri + '/servers/detail')
list_servers_detail_response = self.successResultOf(list_servers_detail)
list_servers_detail_response_body = self.successResultOf(
treq.json_content(list_servers_detail_response))
self.assertEqual(get_server_ips_response_body['addresses'],
list_servers_detail_response_body['servers'][0]['addresses'])
示例10: setUp
def setUp(self):
"""
Create a :obj:`MimicCore` with :obj:`QueueApi` as the only plugin,
and create a queue
"""
self.clock = Clock()
self.core = MimicCore(self.clock, [QueueApi()])
self.root = MimicRoot(self.core).app.resource()
self.response = request(
self, self.root, b"POST", "/identity/v2.0/tokens",
json.dumps({
"auth": {
"passwordCredentials": {
"username": "testQueue",
"password": "testQueuePassword",
},
}
}).encode("utf-8")
)
self.auth_response = self.successResultOf(self.response)
self.json_body = self.successResultOf(
treq.json_content(self.auth_response))
self.uri = self.json_body['access']['serviceCatalog'][0]['endpoints'][0]['publicURL']
self.queue_name = "test_queue"
self.create_queue = request(
self, self.root, b"PUT", self.uri + '/queues/' + self.queue_name)
self.create_queue_response = self.successResultOf(self.create_queue)
示例11: lookup_timezone
def lookup_timezone(loc, api_key=''):
"""
Determine the timezone of a lat/long pair.
@param loc: lat/long coordinates of a location
@type loc: dict
@type api_key: str
@rtype: defer.Deferred yielding a second offset representing the timezone
"""
try:
res = yield treq.get(("https://maps.googleapis.com/maps/api/timezone/json"),
params={'location': str(loc['lat']) + ',' + str(loc['lng']),
'timestamp': str(now_in_utc_secs()), 'sensor': 'false', 'key': api_key})
if res and res.code == 200:
data = yield treq.json_content(res)
if data['status'] == 'OK':
# API returned timezone info. What we care about: rawOffset
defer.returnValue(int(data['rawOffset'])+int(data['dstOffset']))
else:
log.warn("Bad status response from Google Geocode API: %s" % (data['status']))
elif res is not None:
log.warn("Bad HTTP status from Google Timezone API: %s" % (res.code))
except TypeError:
log.warn("Bad lat/long parameter passed to lookup_timezone: %s" % (loc), exc_info=True)
示例12: lookup_geocode
def lookup_geocode(location, api_key=''):
"""
Determine the lat/long coordinates for a location name.
:param location: location name
:type location: str
:type api_key: str
:return: a dict with keys 'lat', 'lng', 'loc' that contain
the lat/long coordinates and placename for the location.
:rtype: defer.Deferred
"""
try:
res = yield treq.get("http://maps.googleapis.com/maps/api/geocode/json",
params={'address': location, 'sensor': 'false', 'key': api_key})
if res and res.code == 200:
data = yield treq.json_content(res)
if data['status'] == 'OK':
# API returned at least one geocode
ret = data['results'][0]['geometry']['location']
ret['loc'] = data['results'][0]['formatted_address']
defer.returnValue(ret)
else:
log.warn("Bad status response from Google Geocode API: %s" % (data['status']))
elif res is not None:
log.warn("Bad HTTP status from Google Geocode API: %s" % (res.code))
except TypeError:
log.warn("Bad location passed to lookup_geocode", exc_info=True)
示例13: setUp
def setUp(self):
"""
Create a :obj:`MimicCore` with :obj:`LoadBalancerApi` as the only plugin.
And create a load balancer and add nodes to the load balancer.
"""
helper = APIMockHelper(self, [LoadBalancerApi()])
self.root = helper.root
self.uri = helper.uri
create_lb = request(
self, self.root, "POST", self.uri + '/loadbalancers',
json.dumps({
"loadBalancer": {
"name": "test_lb",
"protocol": "HTTP",
"virtualIps": [{"type": "PUBLIC"}]
}
})
)
create_lb_response = self.successResultOf(create_lb)
self.create_lb_response_body = self.successResultOf(treq.json_content(
create_lb_response))
self.lb_id = self.create_lb_response_body["loadBalancer"]["id"]
create_node = self._create_nodes(["127.0.0.1"])
[(self.create_node_response, self.create_node_response_body)] = create_node
self.node = self.create_node_response_body["nodes"]
示例14: got_result
def got_result(result):
if result.code == success_code:
return json_content(result)
else:
d = content(result)
d.addCallback(error, result.code)
return d
示例15: test_basic_auth
def test_basic_auth(self):
response = yield self.get("/basic-auth/treq/treq", auth=("treq", "treq"))
self.assertEqual(response.code, 200)
yield print_response(response)
json = yield treq.json_content(response)
self.assertTrue(json["authenticated"])
self.assertEqual(json["user"], "treq")