當前位置: 首頁>>代碼示例>>Python>>正文


Python Dependency.get方法代碼示例

本文整理匯總了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):
#.........這裏部分代碼省略.........
開發者ID:erezrubinstein,項目名稱:aa,代碼行數:103,代碼來源:ba_online_helper.py

示例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
#.........這裏部分代碼省略.........
開發者ID:erezrubinstein,項目名稱:aa,代碼行數:103,代碼來源:test_retail_mds_monitor.py


注:本文中的common.utilities.inversion_of_control.Dependency.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。