Пример #1
0
    def __init__(self, service, environment=config.get_default_environ(),
                 base_path=None, token=None, app_name=None):
        self.environment = environment

        self.base_url = config.get_service_url(environment, service)
        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
        }

        # load a token for the client's service if it is not given as a param
        # assign the result
        if not token:
            token = self.config_load_token()
        self.set_token(token)

        # verify SSL? Usually true
        self._verify = config.get_ssl_verify(environment)

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)
Пример #2
0
def test_create_job(timer_client, start, interval):
    meta = load_response(timer_client.create_job).metadata
    transfer_client = TransferClient()
    transfer_client.get_submission_id = lambda *_0, **_1: {"value": "mock"}
    transfer_data = TransferData(transfer_client, GO_EP1_ID, GO_EP2_ID)
    timer_job = TimerJob.from_transfer_data(transfer_data, start, interval)
    response = timer_client.create_job(timer_job)
    assert response.http_status == 201
    assert response.data["job_id"] == meta["job_id"]
    timer_job = TimerJob.from_transfer_data(dict(transfer_data), start,
                                            interval)
    response = timer_client.create_job(timer_job)
    assert response.http_status == 201
    assert response.data["job_id"] == meta["job_id"]
    req_body = json.loads(get_last_request().body)
    if isinstance(start, datetime):
        assert req_body["start"] == start.isoformat()
    else:
        assert req_body["start"] == start
    if isinstance(interval, timedelta):
        assert req_body["interval"] == interval.total_seconds()
    else:
        assert req_body["interval"] == interval
    assert req_body["callback_url"] == slash_join(get_service_url("actions"),
                                                  "/transfer/transfer/run")
Пример #3
0
    def __init__(self, service, environment=config.get_default_environ(), base_path=None, token=None, app_name=None):
        self.environment = environment

        self.base_url = config.get_service_url(environment, service)
        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}

        # load a token for the client's service if it is not given as a param
        # assign the result
        if not token:
            token = self.config_load_token()
        self.set_token(token)

        # verify SSL? Usually true
        self._verify = config.get_ssl_verify(environment)

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)
Пример #4
0
    def from_transfer_data(
        cls,
        transfer_data: Union[TransferData, Dict[str, Any]],
        start: Union[datetime.datetime, str],
        interval: Union[datetime.timedelta, int, None],
        *,
        name: Optional[str] = None,
        stop_after: Optional[datetime.datetime] = None,
        stop_after_n: Optional[int] = None,
        scope: Optional[str] = None,
        environment: Optional[str] = None,
    ) -> "TimerJob":
        r"""
        Specify data to create a Timer job using the parameters for a transfer. Timer
        will use those parameters to run the defined transfer operation, recurring at
        the given interval.

        :param transfer_data: A :class:`TransferData <globus_sdk.TransferData>` object.
            Construct this object exactly as you would normally; Timer will use this to
            run the recurring transfer.
        :type transfer_data: globus_sdk.TransferData
        :param start: The datetime at which to start the Timer job.
        :type start: datetime.datetime or str
        :param interval: The interval at which the Timer job should recur. Interpreted
            as seconds if specified as an integer. If ``stop_after_n == 1``, i.e. the
            job is set to run only a single time, then interval *must* be None.
        :type interval: datetime.timedelta or int
        :param name: A (not necessarily unique) name to identify this job in Timer
        :type name: str, optional
        :param stop_after: A date after which the Timer job will stop running
        :type stop_after: datetime.datetime, optional
        :param stop_after_n: A number of executions after which the Timer job will stop
        :type stop_after_n: int
        :param scope: Timer defaults to the Transfer 'all' scope. Use this parameter to
            change the scope used by Timer when calling the Transfer Action Provider.
        :type scope: str, optional
        :param environment: For internal use: because this method needs to generate a
            URL for the Transfer Action Provider, this argument can control which
            environment the Timer job is sent to.
        :type environment: str, optional
        """
        transfer_action_url = slash_join(
            get_service_url("actions", environment=environment),
            "transfer/transfer/run")
        # dict will either convert a `TransferData` object or leave us with a dict here
        callback_body = {"body": dict(transfer_data)}
        return cls(
            transfer_action_url,
            callback_body,
            start,
            interval,
            name=name,
            stop_after=stop_after,
            stop_after_n=stop_after_n,
            scope=scope,
        )
Пример #5
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)
Пример #6
0
    def __init__(self, service, environment=None,
                 base_path=None, authorizer=None, app_name=None):
        # 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

        self.base_url = config.get_service_url(environment, service)
        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)

        # set application name if given
        self.app_name = None
        if app_name is not None:
            self.set_app_name(app_name)
Пример #7
0
    def __init__(
        self,
        *,
        environment: Optional[str] = None,
        base_url: Optional[str] = None,
        authorizer: Optional[GlobusAuthorizer] = None,
        app_name: Optional[str] = None,
        transport_params: Optional[Dict[str, Any]] = None,
    ):
        # explicitly check the `service_name` to ensure that it was set
        #
        # unfortunately, we can't rely on declaring BaseClient as an ABC because it
        # doesn't have any abstract methods
        #
        # if we declare `service_name` without a value, we get AttributeError on access
        # instead of the (desired) TypeError when instantiating a BaseClient because
        # it's abstract
        if self.service_name == "_base":
            raise NotImplementedError(
                "Cannot instantiate clients which do not set a 'service_name'")
        log.info(
            f'Creating client of type {type(self)} for service "{self.service_name}"'
        )

        # 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_environment_name(environment)

        self.transport = self.transport_class(**(transport_params or {}))
        log.debug(f"initialized transport of type {type(self.transport)}")

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

        self.authorizer = authorizer

        # set application name if given
        self._app_name = None
        if app_name is not None:
            self.app_name = app_name

        # setup paginated methods
        self.paginated = PaginatorTable(self)
Пример #8
0
    def __init__(self, service, environment=config.get_default_environ(),
                 base_path=None):
        self.environment = environment
        self.base_url = config.get_service_url(environment, service)
        if base_path is not None:
            self.base_url = slash_join(self.base_url, base_path)
        self._session = requests.Session()
        self._headers = dict(Accept="application/json")
        self._auth = None

        # potentially add an Authorization header, if a token is specified
        auth_token = config.get_auth_token(environment)
        if auth_token:
            warnings.warn(
                ('Providing raw Auth Tokens is not recommended, and is slated '
                 'for deprecation. If you use this feature, be ready to '
                 'transition to using a new authentication mechanism after we '
                 'announce its availability.'),
                PendingDeprecationWarning)
            self.set_auth_token(auth_token)

        self._verify = config.get_ssl_verify(environment)
Пример #9
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)))

        # 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)