本文整理汇总了Python中common.utilities.inversion_of_control.Dependency.get方法的典型用法代码示例。如果您正苦于以下问题:Python Dependency.get方法的具体用法?Python Dependency.get怎么用?Python Dependency.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common.utilities.inversion_of_control.Dependency
的用法示例。
在下文中一共展示了Dependency.get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BAOnlineAuthContext
# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import get [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: TestRetailMDSMonitor
# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import get [as 别名]
class TestRetailMDSMonitor(mox.MoxTestBase):
def setUp(self):
# call setUp on super class
super(TestRetailMDSMonitor, self).setUp()
# register mox dependencies
register_common_mox_dependencies(self.mox)
# get mock dependencies that we need to use for recording
self.mock_rest_provider = Dependency("RestProvider").value
self.mock_email_provider = Dependency("EmailProvider").value
# helper vars
self.stats_url = "http://localhost:5001/connection_stats"
def tearDown(self):
# call tearDown on super class
super(TestRetailMDSMonitor, self).tearDown()
def test_initialize(self):
"""
Verify that initialize sets the correct variables
"""
monitor = RetailMDSMonitor()
self.assertEqual(monitor.server_regex, re.compile("^coredata-us-[a-zA-Z][0-9]:27017", re.IGNORECASE))
self.assertEqual(monitor.connection_stats_url, "http://localhost:5001/connection_stats")
self.assertEqual(monitor.loops, 10)
def test_monitor__success(self):
# create monitor and set loops to 2
monitor = RetailMDSMonitor()
monitor.loops = 2
# create mocks
mock_response = self.mox.CreateMock(Response)
mock_response_json = self._get_mock_response_json("CoreData-US-B1:27017")
mock_cells = [None] * 3296
# stub out stuff
self.mox.StubOutClassWithMocks(redis, "StrictRedis")
# record
mock_redis_connection = redis.StrictRedis("localhost", port = 6379, db = 0)
mock_redis_connection.lrange("white_space_cells__SquareMiles50__TOTPOP_CY", 0, -1).AndReturn(mock_cells)
self.mock_rest_provider.get(self.stats_url).AndReturn(mock_response)
mock_response.ok = True
mock_response.json().AndReturn(mock_response_json)
self.mock_rest_provider.get(self.stats_url).AndReturn(mock_response)
mock_response.ok = True
mock_response.json().AndReturn(mock_response_json)
# replay all
self.mox.ReplayAll()
# go!
status = monitor.monitor()
self.assertEqual(status, "success")
def test_monitor__redis_not_loaded(self):
# create monitor. Leave loops as if, to make sure it "breaks" out of the loop
monitor = RetailMDSMonitor()
# stub out
self.mox.StubOutWithMock(monitor, "send_redis_down_email")
self.mox.StubOutClassWithMocks(redis, "StrictRedis")
# record
mock_redis_connection = redis.StrictRedis("localhost", port = 6379, db = 0)
mock_redis_connection.lrange("white_space_cells__SquareMiles50__TOTPOP_CY", 0, -1).AndReturn([])
monitor.send_redis_down_email()
# replay all
self.mox.ReplayAll()
# go!
status = monitor.monitor()
self.assertEqual(status, "failed")
def test_monitor__wrong_server(self):
# create monitor. Leave loops as if, to make sure it "breaks" out of the loop
monitor = RetailMDSMonitor()
# get mocks
mock_response = self.mox.CreateMock(Response)
mock_response_json = self._get_mock_response_json("CoreData-SG-B1:27017")
mock_cells = [None] * 3296
# stub out
#.........这里部分代码省略.........