Esempio n. 1
0
    def __init__(
        self,
        service,
        environment=None,
        base_url=None,
        base_path=None,
        authorizer=None,
        app_name=None,
        http_timeout=None,
        *args,
        **kwargs,
    ):
        self._init_logger_adapter()
        self.logger.info('Creating client of type {} for service "{}"'.format(
            type(self), service))

        # if an environment was passed, it will be used, but otherwise lookup
        # the env var -- and in the special case of `production` translate to
        # `default`, regardless of the source of that value
        # logs the environment when it isn't `default`
        self.environment = config.get_globus_environ(inputenv=environment)

        self.authorizer = authorizer

        if base_url is None:
            self.base_url = config.get_service_url(self.environment, service)
        else:
            self.base_url = base_url
        self.base_url = slash_join(self.base_url, base_path)

        # setup the basics for wrapping a Requests Session
        # including basics for internal header dict
        self._session = requests.Session()
        self._headers = {
            "Accept": "application/json",
            "User-Agent": self.BASE_USER_AGENT,
        }

        # verify SSL? Usually true
        self._verify = config.get_ssl_verify(self.environment)
        # HTTP connection timeout
        # this is passed verbatim to `requests`, and we therefore technically
        # support a tuple for connect/read timeouts, but we don't need to
        # advertise that... Just declare it as an float value
        if http_timeout is None:
            http_timeout = config.get_http_timeout(self.environment)
        self._http_timeout = http_timeout
        # handle -1 by passing None to requests
        if self._http_timeout == -1:
            self._http_timeout = None

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)
Esempio n. 2
0
    def __init__(
        self,
        verify_ssl: Optional[bool] = None,
        http_timeout: Optional[float] = None,
        retry_backoff: Callable[[RetryContext], float] = _exponential_backoff,
        retry_checks: Optional[List[RetryCheck]] = None,
        max_sleep: int = 10,
        max_retries: Optional[int] = None,
    ):
        self.session = requests.Session()
        self.verify_ssl = config.get_ssl_verify(verify_ssl)
        self.http_timeout = config.get_http_timeout(http_timeout)
        self._user_agent = self.BASE_USER_AGENT

        # retry parameters
        self.retry_backoff = retry_backoff
        self.max_sleep = max_sleep
        self.max_retries = (max_retries if max_retries is not None else
                            self.DEFAULT_MAX_RETRIES)
        self.retry_checks = list(retry_checks if retry_checks else [])  # copy
        # register internal checks
        self.register_default_retry_checks()
Esempio n. 3
0
    def __init__(self,
                 service,
                 environment=None,
                 base_url=None,
                 base_path=None,
                 authorizer=None,
                 app_name=None,
                 http_timeout=None,
                 *args,
                 **kwargs):
        # get the fully qualified name of the client class, so that it's a
        # child of globus_sdk
        self.logger = ClientLogAdapter(
            logging.getLogger(self.__module__ + '.' + self.__class__.__name__),
            {'client': self})
        self.logger.info('Creating client of type {} for service "{}"'.format(
            type(self), service))
        # if restrictions have been placed by a child class on the allowed
        # authorizer types, make sure we are not in violation of those
        # constraints
        if self.allowed_authorizer_types is not None and (
                authorizer is not None
                and type(authorizer) not in self.allowed_authorizer_types):
            self.logger.error("{} doesn't support authorizer={}".format(
                type(self), type(authorizer)))
            raise ValueError(("{0} can only take authorizers from {1}, "
                              "but you have provided {2}").format(
                                  type(self), self.allowed_authorizer_types,
                                  type(authorizer)))

        # defer this default until instantiation time so that logging can
        # capture the execution of the config load
        if environment is None:
            environment = config.get_default_environ()

        self.environment = environment
        self.authorizer = authorizer

        if base_url is None:
            self.base_url = config.get_service_url(environment, service)
        else:
            self.base_url = base_url
        if base_path is not None:
            self.base_url = slash_join(self.base_url, base_path)

        # setup the basics for wrapping a Requests Session
        # including basics for internal header dict
        self._session = requests.Session()
        self._headers = {
            'Accept': 'application/json',
            'User-Agent': self.BASE_USER_AGENT
        }

        # verify SSL? Usually true
        self._verify = config.get_ssl_verify(environment)
        # HTTP connection timeout
        # this is passed verbatim to `requests`, and we therefore technically
        # support a tuple for connect/read timeouts, but we don't need to
        # advertise that... Just declare it as an float value
        if http_timeout is None:
            http_timeout = config.get_http_timeout(environment)
        self._http_timeout = http_timeout
        # handle -1 by passing None to requests
        if self._http_timeout == -1:
            self._http_timeout = None

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)
Esempio n. 4
0
    def __init__(self,
                 service,
                 environment=None,
                 base_url=None,
                 base_path=None,
                 authorizer=None,
                 app_name=None,
                 http_timeout=None,
                 *args,
                 **kwargs):
        self._init_logger_adapter()
        self.logger.info('Creating client of type {} for service "{}"'.format(
            type(self), service))
        # if restrictions have been placed by a child class on the allowed
        # authorizer types, make sure we are not in violation of those
        # constraints
        if self.allowed_authorizer_types is not None and (
                authorizer is not None
                and type(authorizer) not in self.allowed_authorizer_types):
            self.logger.error("{} doesn't support authorizer={}".format(
                type(self), type(authorizer)))
            raise exc.GlobusSDKUsageError(
                ("{0} can only take authorizers from {1}, "
                 "but you have provided {2}").format(
                     type(self), self.allowed_authorizer_types,
                     type(authorizer)))

        # if an environment was passed, it will be used, but otherwise lookup
        # the env var -- and in the special case of `production` translate to
        # `default`, regardless of the source of that value
        # logs the environment when it isn't `default`
        self.environment = config.get_globus_environ(inputenv=environment)

        self.authorizer = authorizer

        if base_url is None:
            self.base_url = config.get_service_url(self.environment, service)
        else:
            self.base_url = base_url
        if base_path is not None:
            self.base_url = slash_join(self.base_url, base_path)

        # setup the basics for wrapping a Requests Session
        # including basics for internal header dict
        self._session = requests.Session()
        self._headers = {
            'Accept': 'application/json',
            'User-Agent': self.BASE_USER_AGENT
        }

        # verify SSL? Usually true
        self._verify = config.get_ssl_verify(self.environment)
        # HTTP connection timeout
        # this is passed verbatim to `requests`, and we therefore technically
        # support a tuple for connect/read timeouts, but we don't need to
        # advertise that... Just declare it as an float value
        if http_timeout is None:
            http_timeout = config.get_http_timeout(self.environment)
        self._http_timeout = http_timeout
        # handle -1 by passing None to requests
        if self._http_timeout == -1:
            self._http_timeout = None

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)