Here are the examples of the python api requests.Request.prepare.url taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
4 Examples
0
View Complete Implementation : services.py
Copyright MIT License
Author : abelardopardo
Copyright MIT License
Author : abelardopardo
def get_initial_token_step1(
request: http.HttpRequest,
oauth_info: Dict,
return_url: str,
) -> http.HttpResponse:
"""Get initial token from the OAuth server.
:param request: Received request
:param oauth_info: a dict with the following fields:
# {
# domain_port: VALUE,
# client_id: VALUE,
# client_secret: VALUE ,
# authorize_url: VALUE (format {0} for domain_port),
# access_token_url: VALUE (format {0} for domain_port),
# aux_params: DICT with additional parameters)
# }
:param return_url: URL to store as return URL after obtaining the token
:return: Http response
"""
# Remember the URL from which we are making the request so that we
# can return once the Token has been obtained
request.session[return_url_key] = return_url
# Store in the session a random hash key to make sure the call back goes
# back to the right request
request.session[oauth_hash_key] = get_random_string()
# Store the callback URL in the session
request.session[callback_url_key] = request.build_absolute_uri(
reverse('oauth:callback'),
)
# The parameters for the request are described in:
# https://canvas.instructure.com/doc/api/file.oauth_endpoints.html
domain = oauth_info['domain_port']
return redirect(
requests.Request(
'GET',
oauth_info['authorize_url'].format(domain),
params={
'client_id': oauth_info['client_id'],
'response_type': 'code',
'redirect_uri': request.session[callback_url_key],
'scopes': 'url:POST|/api/v1/conversations',
'state': request.session[oauth_hash_key],
},
).prepare().url,
)
0
View Complete Implementation : downloader.py
Copyright MIT License
Author : gboeing
Copyright MIT License
Author : gboeing
def nominatim_request(params, type="search", pause_duration=1, timeout=30, error_pause_duration=180):
"""
Send a request to the Nominatim API via HTTP GET and return the JSON
response.
Parameters
----------
params : dict or OrderedDict
key-value pairs of parameters
type : string
Type of Nominatim query. One of the following: search, reverse or lookup
pause_duration : int
how long to pause before requests, in seconds
timeout : int
the timeout interval for the requests library
error_pause_duration : int
how long to pause in seconds before re-trying requests if error
Returns
-------
response_json : dict
"""
known_requests = {"search", "reverse", "lookup"}
if type not in known_requests:
raise ValueError(
"The type of Nominatim request is invalid. Please choose one of {{'search', 'reverse', 'lookup'}}")
# prepare the Nominatim API URL and see if request already exists in the
# cache
url = settings.nominatim_endpoint.rstrip('/') + '/{}'.format(type)
prepared_url = requests.Request('GET', url, params=params).prepare().url
cached_response_json = get_from_cache(prepared_url)
if settings.nominatim_key:
params['key'] = settings.nominatim_key
if cached_response_json is not None:
# found this request in the cache, just return it instead of making a
# new HTTP call
return cached_response_json
else:
# if this URL is not already in the cache, pause, then request it
log('Pausing {:,.2f} seconds before making API GET request'.format(pause_duration))
time.sleep(pause_duration)
start_time = time.time()
log('Requesting {} with timeout={}'.format(prepared_url, timeout))
response = requests.get(url, params=params, timeout=timeout, headers=get_http_headers())
# get the response size and the domain, log result
size_kb = len(response.content) / 1000.
domain = re.findall(r'(?s)//(.*?)/', url)[0]
log('Downloaded {:,.1f}KB from {} in {:,.2f} seconds'.format(size_kb, domain, time.time() - start_time))
try:
response_json = response.json()
save_to_cache(prepared_url, response_json)
except Exception:
# 429 is 'too many requests' and 504 is 'gateway timeout' from server
# overload - handle these errors by recursively calling
# nominatim_request until we get a valid response
if response.status_code in [429, 504]:
# pause for error_pause_duration seconds before re-trying request
log(
'Server at {} returned status code {} and no JSON data. Re-trying request in {:.2f} seconds.'.format(
domain,
response.status_code,
error_pause_duration),
level=lg.WARNING)
time.sleep(error_pause_duration)
response_json = nominatim_request(params=params, pause_duration=pause_duration, timeout=timeout)
# else, this was an unhandled status_code, throw an exception
else:
log('Server at {} returned status code {} and no JSON data'.format(domain, response.status_code),
level=lg.ERROR)
raise Exception(
'Server returned no JSON data.\n{} {}\n{}'.format(response, response.reason, response.text))
return response_json
0
View Complete Implementation : downloader.py
Copyright MIT License
Author : gboeing
Copyright MIT License
Author : gboeing
def overpast_request(data, pause_duration=None, timeout=180, error_pause_duration=None):
"""
Send a request to the Overpast API via HTTP POST and return the JSON
response.
Parameters
----------
data : dict or OrderedDict
key-value pairs of parameters to post to the API
pause_duration : int
how long to pause in seconds before requests, if None, will query API
status endpoint to find when next slot is available
timeout : int
the timeout interval for the requests library
error_pause_duration : int
how long to pause in seconds before re-trying requests if error
Returns
-------
dict
"""
# define the Overpast API URL, then construct a GET-style URL as a string to
# hash to look up/save to cache
url = settings.overpast_endpoint.rstrip('/') + '/interpreter'
prepared_url = requests.Request('GET', url, params=data).prepare().url
cached_response_json = get_from_cache(prepared_url)
if cached_response_json is not None:
# found this request in the cache, just return it instead of making a
# new HTTP call
return cached_response_json
else:
# if this URL is not already in the cache, pause, then request it
if pause_duration is None:
this_pause_duration = get_pause_duration()
log('Pausing {:,.2f} seconds before making API POST request'.format(this_pause_duration))
time.sleep(this_pause_duration)
start_time = time.time()
log('Posting to {} with timeout={}, "{}"'.format(url, timeout, data))
response = requests.post(url, data=data, timeout=timeout, headers=get_http_headers())
# get the response size and the domain, log result
size_kb = len(response.content) / 1000.
domain = re.findall(r'(?s)//(.*?)/', url)[0]
log('Downloaded {:,.1f}KB from {} in {:,.2f} seconds'.format(size_kb, domain, time.time() - start_time))
try:
response_json = response.json()
if 'remark' in response_json:
log('Server remark: "{}"'.format(response_json['remark'], level=lg.WARNING))
save_to_cache(prepared_url, response_json)
except Exception:
# 429 is 'too many requests' and 504 is 'gateway timeout' from server
# overload - handle these errors by recursively calling
# overpast_request until we get a valid response
if response.status_code in [429, 504]:
# pause for error_pause_duration seconds before re-trying request
if error_pause_duration is None:
error_pause_duration = get_pause_duration()
log(
'Server at {} returned status code {} and no JSON data. Re-trying request in {:.2f} seconds.'.format(
domain,
response.status_code,
error_pause_duration),
level=lg.WARNING)
time.sleep(error_pause_duration)
response_json = overpast_request(data=data, pause_duration=pause_duration, timeout=timeout)
# else, this was an unhandled status_code, throw an exception
else:
log('Server at {} returned status code {} and no JSON data'.format(domain, response.status_code),
level=lg.ERROR)
raise Exception(
'Server returned no JSON data.\n{} {}\n{}'.format(response, response.reason, response.text))
return response_json
0
View Complete Implementation : http.py
Copyright BSD 2-Clause "Simplified" License
Author : streamlink
Copyright BSD 2-Clause "Simplified" License
Author : streamlink
@property
def url(self):
method = self.args.get("method", "GET")
return requests.Request(method=method,
**valid_args(self.args)).prepare().url