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


Python Dependency.post方法代碼示例

本文整理匯總了Python中common.utilities.inversion_of_control.Dependency.post方法的典型用法代碼示例。如果您正苦於以下問題:Python Dependency.post方法的具體用法?Python Dependency.post怎麽用?Python Dependency.post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在common.utilities.inversion_of_control.Dependency的用法示例。


在下文中一共展示了Dependency.post方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: BAOnlineAuthContext

# 需要導入模塊: from common.utilities.inversion_of_control import Dependency [as 別名]
# 或者: from common.utilities.inversion_of_control.Dependency import post [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


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