本文整理汇总了Python中requests.response方法的典型用法代码示例。如果您正苦于以下问题:Python requests.response方法的具体用法?Python requests.response怎么用?Python requests.response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests
的用法示例。
在下文中一共展示了requests.response方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def post(self, type_name, *args, **kwargs):
"""
Issue POST request to API.
:param type_name: is a string specifying the type of the object according to the API.
:param *args: The rest of the positional arguments will be appended to the post URL
:param *kwargs: all the keyword arguments will become post data.
:returns: response object from requests.
"""
url = '%s/%s/' % (self.host, type_name)
if len(args) > 0:
url = url + '/'.join(args) + '/'
if self.debug:
self.logger.debug('POST %s %s %s' % (url, json.dumps(kwargs),
self.headers))
results = requests.post(url, data=json.dumps(kwargs),
headers=self.headers)
self.status_code = results.status_code
if self.debug:
self.debug_result(results)
return results
示例2: head
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def head(self, *args, **kwargs):
"""
Issue HEAD request to API.
:param *args: All positional arguments are appended to the request URL.
:param **kwargs: These are not used at the moment, since this is a get request TODO
:returns: requests.response object.
"""
self.url = '%s/' % (self.host)
if len(args) > 0:
self.url = self.url + '/'.join(args) + '/'
if self.debug:
self.logger.debug('HEAD %s %s %s' % (self.url, json.dumps(kwargs), self.headers))
# look for optional keyword argument params for constructing URL param
# i.e. ?f1=v1&f2=v2
params = kwargs.get('params')
results = requests.head(self.url, params=params,
headers=self.headers)
self.status_code = results.status_code
if self.debug:
self.debug_result(results)
return results
示例3: get_json
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def get_json(self, *args):
"""
Smarter GET request when you really want a json object back.
Note: This is experimental -- not sure if this is the right abstraction.
:param *args: All positional arguments are appended to the request URL.
:returns: json string or None if error.
:exception: Will raise exception if response status code is not 200.
"""
results = self.get(*args)
if results.status_code != requests.codes.ok:
results.raise_for_status()
if len(results.content) > 0:
return results.json()
else:
return None
# TODO: Retire get_by_url?
示例4: update_resource_version
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def update_resource_version(self, owner_type, owner_id,
resource_id, version_id, resource_type, base_data):
"""
Update source version. Limits update to only the description and released fields for now.
:param owner_type: 'orgs' or 'users'
:param owner_id: ID of the org/user owner
:param resource_id: ID of the source/collection
:param version_id: ID of the source/collection_version
:param resource_type: 'source' or 'collection'
:param base_data: Dictionary of fields to update
:returns: response object
"""
data = {}
if 'description' in base_data:
data['description'] = base_data['description']
if 'released' in base_data:
data['released'] = base_data['released']
if 'retired' in base_data:
data['retired'] = base_data['retired']
if 'version_external_id' in base_data:
data['version_external_id'] = base_data['version_external_id']
result = self.put(owner_type, owner_id, resource_type, resource_id, version_id, **data)
return result
示例5: test_shorten
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def test_shorten(self):
data = self.data
response = self.client.call('/shorten', data=data, headers=self.headers)
short_url = self._get_short_url_from_response(response)
self.assertEqual(response.status_code, 200)
self.assertTrue('value="{}"'.format(short_url) in response.text)
self.assertTrue('Copy To Clipboard' in response.text)
self.assertTrue('Shorten Another Link' in response.text)
self.assertTrue('{}+'.format(short_url) in response.text)
# Repeat shorten should return the same result
response = requests.post(self.url + '/shorten', data=data, headers=self.headers)
self.assertEqual(response.status_code, 200)
self.assertTrue('value="{}"'.format(short_url) in response.text)
# Test a different url
data['long_url'] = 'https://www.python.org/'
response = requests.post(self.url + '/shorten', data=data, headers=self.headers)
short_url = self._get_short_url_from_response(response)
self.assertEqual(response.status_code, 200)
self.assertTrue('value="{}"'.format(short_url) in response.text)
示例6: test_login_shorten
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def test_login_shorten(self):
response = requests.post(self.url + '/signup', data=self.user_data, headers=self.headers)
with requests.Session() as sess:
response = sess.post(self.url + '/login', data=self.login_data, headers=self.headers)
self.cookies = sess.cookies
self.assertTrue('Welcome Test' in response.text)
# Shorten the URL
data = self.data
data['long_url'] = 'https://example.com/1'
response = sess.post(self.url + '/shorten', data=data, headers=self.headers)
short_url = self._get_short_url_from_response(response)
self.assertEqual(response.status_code, 200)
self.assertTrue('value="{}"'.format(short_url) in response.text)
self.assertTrue('Welcome Test' in response.text)
self.assertTrue('Copy To Clipboard' in response.text)
self.assertTrue('Shorten Another Link' in response.text)
# verify its on dashboard
response = sess.get(self.url + '/dashboard')
short_code = short_url.split('/')[-1]
self.assertEqual(response.status_code, 200)
self.assertTrue('{}+'.format(short_code) in response.text)
self.assertTrue('https://example.com/1' in response.text)
示例7: test_link_hits
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def test_link_hits(self):
data = self.data
data['long_url'] = 'http://example.com/index'
response = requests.post(self.url + '/shorten', data=data, headers=self.headers)
short_url = self._get_short_url_from_response(response)
# Open link
for i in range(2):
requests.get(short_url)
stats_page = requests.get(short_url + '+')
self.assertEqual(stats_page.status_code, 200)
self.assertTrue('Total Hits: {}'.format(i+1) in stats_page.text)
# def test_link_stats(self):
# pass
#
# def test_secret_link_stats(self):
# pass
#
# def test_expired_link_stats(self):
# pass
#
示例8: test_custom_link_stats
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def test_custom_link_stats(self):
data = self.data
data['custom_url'] = 'ninja'
response = requests.post(self.url + '/shorten', data=data, headers=self.headers)
short_url = self._get_short_url_from_response(response)
# Open link
for i in range(2):
requests.get(short_url)
stats_page = requests.get(short_url + '+')
self.assertEqual(stats_page.status_code, 200)
self.assertTrue('Total Hits: {}'.format(i+1) in stats_page.text)
# #######################
# # Test static resources
# #######################
示例9: response_data
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def response_data(response, bad_json=False):
"""
Extracts data from a requests response object
:param response: The response from which we need to extract information
:type response: requests.response
:param bad_json: Default False: If true, uses ast eval instead of json
load
:type bad_json bool
:return: response result in Pythonic form, if it can get it, Else None
:raises Exception
"""
data = None
if response:
if not bad_json:
try:
data = json_to_python(response.text)
except Exception:
data = parse_literals(response.text)
else:
data = parse_literals(response.text)
return data
示例10: check_response
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def check_response(self, response):
"""
Checks the status code and raise an AirflowException exception on non 2XX or 3XX
status codes
:param response: A requests response object
:type response: requests.response
"""
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
self.log.error("HTTP error: %s", response.reason)
self.log.error(response.text)
raise AirflowException(str(response.status_code) + ":" + response.reason)
示例11: run_and_check
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def run_and_check(self, session, prepped_request, extra_options):
"""
Grabs extra options like timeout and actually runs the request,
checking for the result
:param session: the session to be used to execute the request
:type session: requests.Session
:param prepped_request: the prepared request generated in run()
:type prepped_request: session.prepare_request
:param extra_options: additional options to be used when executing the request
i.e. {'check_response': False} to avoid checking raising exceptions on non 2XX
or 3XX status codes
:type extra_options: dict
"""
extra_options = extra_options or {}
try:
response = session.send(
prepped_request,
stream=extra_options.get("stream", False),
verify=extra_options.get("verify", True),
proxies=extra_options.get("proxies", {}),
cert=extra_options.get("cert"),
timeout=extra_options.get("timeout"),
allow_redirects=extra_options.get("allow_redirects", True))
if extra_options.get('check_response', True):
self.check_response(response)
return response
except requests.exceptions.ConnectionError as ex:
self.log.warning('%s Tenacity will retry to execute the operation', ex)
raise ex
示例12: get
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def get(self, *args, **kwargs):
"""
Issue get request to API.
:param *args: All positional arguments are appended to the request URL.
Note: To pass query parameters to the GET function,
use a params={k:v} keyword argument.
:param **kwargs: These are not used at the moment, since this is a get request TODO
:returns: requests.response object.
"""
# Build the URL
self.url = '%s/' % (self.host)
if len(args) > 0:
self.url = self.url + '/'.join(args)
if self.url[-1] != '/':
self.url += '/'
# Look for optional keyword argument params for constructing URL param e.g. ?f1=v1&f2=v2
params = kwargs.get('params')
if self.debug:
self.logger.debug('GET %s %s %s' % (self.url, params, self.headers))
results = requests.get(self.url, params=params, headers=self.headers)
self.status_code = results.status_code
if self.debug:
self.debug_result(results)
return results
# TODO: Retire get_json?
示例13: get_by_url
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def get_by_url(self, url, **kwargs):
"""
Issue get request to API.
:param url: is a string specifying the request url. Useful
for urls contained in OCL response data like members_url.
"""
url = '%s/%s' % (self.host, url)
if self.debug:
self.logger.debug('GET %s %s %s' % (url, json.dumps(kwargs), self.headers))
results = requests.get(url, data=json.dumps(kwargs),
headers=self.headers)
return results
示例14: update_org
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def update_org(self, org_id, base_data, extras=[]):
"""
Update organization
:param org_id: is the ID for the organization being updated.
:param base_data: is a dictionary of fields.
:returns: response object.
"""
data = {}
data.update(base_data)
result = self.post('orgs', org_id, **data)
return result
示例15: create_source
# 需要导入模块: import requests [as 别名]
# 或者: from requests import response [as 别名]
def create_source(self, owner_type, owner_id, base_data, extras=[]):
"""
Create source.
:param owner_type: 'orgs' or 'users'
:param owner_id: ID of the org/user/ owner
:param base_data: Dictionary of fields for the new source version
:param extras: Extras to save to the resource
:returns: response object
TODO(paynejd): create_sources extras not implemented
"""
data = {}
data.update(base_data)
result = self.post(owner_type, owner_id, 'sources', **data)
return result