本文整理汇总了Python中common.utilities.inversion_of_control.Dependency.download_file方法的典型用法代码示例。如果您正苦于以下问题:Python Dependency.download_file方法的具体用法?Python Dependency.download_file怎么用?Python Dependency.download_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common.utilities.inversion_of_control.Dependency
的用法示例。
在下文中一共展示了Dependency.download_file方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BAOnlineAuthContext
# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import download_file [as 别名]
class BAOnlineAuthContext(object):
"""
A context helper to wrap REST requests to Business Analyst Online.
BA Online requires authenticating with a username/password, which returns a token.
This token must be stored and managed on our (client) side, and sent in as a param in subsequent requests.
"""
# static token so that it can be shared among instances
token = ""
def __init__(self):
# get dependencies
self.logger = Dependency("LogManager").value
self.config = Dependency("Config", HasAttributes("ba_online_username", "ba_online_password")).value
self.rest_provider = Dependency("RestProvider").value
# get username and password
self.user_name = self.config.ba_online_username
self.password = self.config.ba_online_password
# various internal vars
self.num_auth_requests = 0
def __enter__(self):
self.__get_token()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def post(self, url, data):
#inject token
self.__log_request(url)
data.update({ "Token": BAOnlineAuthContext.token })
return self.rest_provider.post(url, data, 60)
def get(self, url, params):
#inject token
self.__log_request(url)
params.update({ "Token": BAOnlineAuthContext.token })
return self.rest_provider.get(url, params, 60)
def generate_report(self, request_format, url, try_again = True):
"""
Adaptor method to make this fit into ArcGIS_report_helper.py
"""
with self:
response = self.get(url, request_format)
if not response.ok:
#inject potentially useful stuff in the response: the url and params
response.reason += " | url: %s | GET data: %s" % (url, request_format)
response.raise_for_status()
else:
response_rec = response.json()
if "error" in response_rec:
# if try again, get a new token and try again
if try_again:
BAOnlineAuthContext.token = ""
self.__get_token()
return self.generate_report(request_format, url, False)
# otherwise raise an error
raise RuntimeError("BA Online Error - get_report_templates(): %s." % response_rec["error"])
return response
#this is being used in GP10, query params get too large with detailed trade areas so need to post
def generate_report_with_post(self, request_format, url, try_again = True):
"""
Adaptor method to make this fit into ArcGIS_report_helper.py
"""
with self:
response = self.post(url, request_format)
if not response.ok:
#inject potentially useful stuff in the response: the url and params
response.reason += " | url: %s | GET data: %s" % (url, request_format)
response.raise_for_status()
else:
response_rec = response.json()
if "error" in response_rec:
# if try again, get a new token and try again
if try_again:
BAOnlineAuthContext.token = ""
self.__get_token()
return self.generate_report_with_post(request_format, url, False)
# otherwise raise an error
raise RuntimeError("BA Online Error - get_report_templates(): %s." % response_rec["error"])
return response
def download_file(self, url):
#.........这里部分代码省略.........
示例2: ArcGISConnection
# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import download_file [as 别名]
class ArcGISConnection(object):
"""
This class is in charge of keeping track of an ArcGIS server and making calls to it
"""
def __init__(self, server_ip_address):
self._rest_provider = Dependency("RestProvider", HasMethods("download_file")).value
self._config = Dependency("Config", HasAttributes("ArcGIS_timeout", "ArcGIS_max_errors")).value
self._logger = Dependency("LogManager", HasMethods("error")).value
self._server_ip_address = server_ip_address
self._timeout = self._config.ArcGIS_timeout
self._max_error_count = self._config.ArcGIS_max_errors
def download_file(self, url):
try:
# append the server ip to the url
url = self._append_ip_address_to_url(url)
# download the file
return self._rest_provider.download_file(url)
except Exception as ex:
# this has been timing out a lot, so I'm adding some logging...
self._logger.critical("Exception on ESRI Server: " % str(self._server_ip_address))
self._logger.critical(str(ex))
self._logger.critical(traceback.format_exc())
raise
def generate_report(self, request_format, url):
# append the server ip to the url
url = self._append_ip_address_to_url(url)
# the ArcGIS server has weird errors. This ensures that we loop several times until a good response is returned
successful_response = False
request_counter = 0
response = None
while not successful_response:
# try to get the request and handle errors properly (so that we can retry)
try:
response = self._rest_provider.make_post_request(url, request_format, time_out = self._timeout)
except Exception as e:
# if there's an error, log it and try again
response = None
error_text = str(e)
self._logger.critical("error send ArcGIS report (exception): %s" + str(error_text))
# if it's a timeout, register the server ip address, so that it keeps count
# treat token required as a time out, so that those servers are taken out of the pool
if "Request timed out" in error_text or "timeout" in error_text or "Token Required" in error_text:
ArcGISConnectionManager.instance.register_timeout(self._server_ip_address)
# once we get a response back, check it's type
if response is not None:
# this signals a successful response
gp19_success_response_text = '{"paramName":"OutputStatus","dataType":"GPBoolean","value":true}'
if response.text.find('arcgisoutput') != -1 or response.text.find('stops') != -1 \
or response.text.find("rings") != -1 or response.text.find(gp19_success_response_text) != -1:
# mark for exit and reset the timeout count
successful_response = True
ArcGISConnectionManager.instance.reset_timeout_count_on_successful_connection(self._server_ip_address)
# if it's a timeout, register the server ip address
# treat token required as a time out, so that those servers are taken out of the pool
elif "Request timed out" in response.text or "timeout" in response.text or "Token Required" in response.text:
ArcGISConnectionManager.instance.register_timeout(self._server_ip_address)
elif 'No solution found.' in response.text and 'useHierarchy' in request_format and request_format['useHierarchy'] == 'true':
request_format['useHierarchy'] = 'false'
else:
self._logger.critical("error send ArcGIS report (no arcgisoutput): %s" + response.text)
else:
self._logger.critical("error send ArcGIS report (response is None)")
if request_counter >= self._max_error_count:
raise Exception('too many requests - %s' % url)
# increment the request counter
request_counter += 1
return response
def _append_ip_address_to_url(self, url):
if url is not None and url[:4] != 'http':
url = ''.join(['http://', self._server_ip_address, '/arcgis', url])
return url