def __init__(
        self,
        authenticator=None,
    ):
        """
        Construct a new client for the Natural Language Classifier service.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """

        service_url = self.default_service_url
        disable_ssl_verification = False

        config = read_external_sources('natural_language_classifier')
        if config.get('URL'):
            service_url = config.get('URL')
        if config.get('DISABLE_SSL'):
            disable_ssl_verification = config.get('DISABLE_SSL')

        if not authenticator:
            authenticator = get_authenticator_from_environment(
                'natural_language_classifier')

        BaseService.__init__(self,
                             service_url=service_url,
                             authenticator=authenticator,
                             disable_ssl_verification=disable_ssl_verification)
    def __init__(
        self,
        crn: str,
        zone_id: str,
        authenticator: Authenticator = None,
    ) -> None:
        """
        Construct a new client for the Page Rule API service.

        :param str crn: instance id.

        :param str zone_id: zone id.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """
        if crn is None:
            raise ValueError('crn must be provided')
        if zone_id is None:
            raise ValueError('zone_id must be provided')

        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator)
        self.crn = crn
        self.zone_id = zone_id
    def __init__(
        self,
        crn: str,
        zone_identifier: str,
        authenticator: Authenticator = None,
    ) -> None:
        """
        Construct a new client for the Range Applications service.

        :param str crn: Full url-encoded cloud resource name (CRN) of resource
               instance.

        :param str zone_identifier: zone identifier.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """
        if crn is None:
            raise ValueError('crn must be provided')
        if zone_identifier is None:
            raise ValueError('zone_identifier must be provided')

        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator)
        self.crn = crn
        self.zone_identifier = zone_identifier
 def __init__(self, api_version, authenticator=None, trace_id=None):
     BaseService.__init__(self,
                          service_url=self.default_service_url,
                          authenticator=authenticator,
                          disable_ssl_verification=False)
     self.api_version = api_version
     self.trace_id = trace_id
     self.configure_service('include-external-config')
示例#5
0
 def __init__(self,
              version: str,
              service_url: str = default_url,
              authenticator: Optional[Authenticator] = None,
              disable_ssl_verification: bool = False) -> None:
     BaseService.__init__(self,
                          service_url=service_url,
                          authenticator=authenticator,
                          disable_ssl_verification=disable_ssl_verification)
     self.version = version
 def __init__(self,
              version,
              service_url=default_url,
              authenticator=None,
              disable_ssl_verification=False):
     BaseService.__init__(self,
                          service_url=service_url,
                          authenticator=authenticator,
                          disable_ssl_verification=disable_ssl_verification)
     self.version = version
示例#7
0
 def __init__(self,
              url=default_url,
              username=None,
              password=None,
              use_vcap_services=True):
     BaseService.__init__(self,
                          'authorization',
                          url,
                          username,
                          password,
                          use_vcap_services,
                          display_name='authorization')
示例#8
0
    def __init__(self,
                 authenticator: Authenticator = None,
                ) -> None:
        """
        Construct a new client for the Resource Records service.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """
        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator)
示例#9
0
    def __init__(
        self,
        url=default_url,
        username=None,
        password=None,
        iam_apikey=None,
        iam_access_token=None,
        iam_url=None,
    ):
        """
        Construct a new client for the Natural Language Classifier service.

        :param str url: The base url to use when contacting the service (e.g.
               "https://gateway.watsonplatform.net/natural-language-classifier/api/natural-language-classifier/api").
               The base url may differ between Bluemix regions.

        :param str username: The username used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of Bluemix. When running on
               Bluemix, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str password: The password used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of Bluemix. When running on
               Bluemix, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str iam_apikey: An API key that can be used to request IAM tokens. If
               this API key is provided, the SDK will manage the token and handle the
               refreshing.

        :param str iam_access_token:  An IAM access token is fully managed by the application.
               Responsibility falls on the application to refresh the token, either before
               it expires or reactively upon receiving a 401 from the service as any requests
               made with an expired token will fail.

        :param str iam_url: An optional URL for the IAM service API. Defaults to
               'https://iam.bluemix.net/identity/token'.
        """

        BaseService.__init__(self,
                             vcap_services_name='natural_language_classifier',
                             url=url,
                             username=username,
                             password=password,
                             iam_apikey=iam_apikey,
                             iam_access_token=iam_access_token,
                             iam_url=iam_url,
                             use_vcap_services=True,
                             display_name='Natural Language Classifier')
示例#10
0
def test_retry_config_non_default():
    service = BaseService(service_url='https://mockurl/',
                          authenticator=NoAuthAuthenticator())
    service.enable_retries(2, 0.3)
    assert service.retry_config.total == 2
    assert service.retry_config.backoff_factor == 0.3

    # Ensure retries fail after 2 retries
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error
示例#11
0
def page_downloader_factory(credentials, data_source, month):
    service_version = "v1"
    service_name = "resource-instance-usage-reports"
    try:
        authenticator = IAMAuthenticator(credentials.get("iam_token", ""))
        service = BaseService(service_url=settings.IBM_SERVICE_URL, authenticator=authenticator)
    except Exception as exp:
        LOG.error(f"Failed setting a base service. Reason: {str(exp)}")
        raise exp
    headers = get_sdk_headers(
        service_name=service_name,
        service_version=service_version.upper(),
        operation_id=f"get_{service_name.replace('-', '_')}",
    )
    params = dict(enterprise_id=data_source.get("enterprise_id", ""), month=month, recurse=True, format="csv")

    def download_page(page=1):
        params["page"] = page
        request = service.prepare_request(
            method="GET", url=f"/{service_version}/{service_name}", headers=headers, params=params
        )
        try:
            response = service.send(request)
            return response
        except ApiException as apiExc:
            if "Total pages" in apiExc.message:
                raise NoMoreReportPages(apiExc.message)
            raise apiExc

    return download_page
示例#12
0
    def __init__(
        self,
        url=default_url,
        iam_apikey=None,
        iam_access_token=None,
        iam_url=None,
        iam_client_id=None,
        iam_client_secret=None,
    ):
        """
        Construct a new client for the ResourceConfiguration service.

        :param str url: The base url to use when contacting the service (e.g.
               "https://config.cloud-object-storage.cloud.ibm.com/v1/v1").
               The base url may differ between IBM Cloud regions.

        :param str iam_apikey: An API key that can be used to request IAM tokens. If
               this API key is provided, the SDK will manage the token and handle the
               refreshing.

        :param str iam_access_token:  An IAM access token is fully managed by the application.
               Responsibility falls on the application to refresh the token, either before
               it expires or reactively upon receiving a 401 from the service as any requests
               made with an expired token will fail.

        :param str iam_url: An optional URL for the IAM service API. Defaults to
               'https://iam.cloud.ibm.com/identity/token'.

        :param str iam_client_id: An optional client_id value to use when interacting with the IAM service.

        :param str iam_client_secret: An optional client_secret value to use when interacting with the IAM service.
        """

        BaseService.__init__(self,
                             vcap_services_name='resource_configuration',
                             url=url,
                             iam_apikey=iam_apikey,
                             iam_access_token=iam_access_token,
                             iam_url=iam_url,
                             iam_client_id=iam_client_id,
                             iam_client_secret=iam_client_secret,
                             use_vcap_services=True,
                             display_name='ResourceConfiguration')
    def __init__(
        self,
        authenticator: Authenticator = None,
        service_name: str = DEFAULT_SERVICE_NAME,
    ) -> None:
        """
        Construct a new client for the Natural Language Classifier service.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """
        if not authenticator:
            authenticator = get_authenticator_from_environment(service_name)
        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator,
                             disable_ssl_verification=False)
        self.configure_service(service_name)
示例#14
0
    def __init__(
        self,
        authenticator: Authenticator = None,
    ) -> None:
        """
        Construct a new client for the vpc service.

        :param str version: Requests the version of the API as of a date in the
               format `YYYY-MM-DD`. Any date up to the current date may be provided.
               Specify the current date to request the latest version.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md
               about initializing the authenticator of your choice.
        """

        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator)
示例#15
0
    def __init__(
        self,
        crn: str,
        authenticator: Authenticator = None,
    ) -> None:
        """
        Construct a new client for the Webhooks service.

        :param str crn: Full url-encoded CRN of the service instance.

        :param Authenticator authenticator: The authenticator specifies the authentication mechanism.
               Get up to date information from https://github.com/IBM/python-sdk-core/blob/main/README.md
               about initializing the authenticator of your choice.
        """
        if crn is None:
            raise ValueError('crn must be provided')

        BaseService.__init__(self,
                             service_url=self.DEFAULT_SERVICE_URL,
                             authenticator=authenticator)
        self.crn = crn
 def __init__(self,
              version,
              url=default_url,
              username=None,
              password=None,
              api_key=None,
              iam_apikey=None,
              iam_access_token=None,
              iam_url=None):
     BaseService.__init__(self,
                          vcap_services_name='test',
                          url=url,
                          api_key=api_key,
                          username=username,
                          password=password,
                          use_vcap_services=True,
                          iam_apikey=iam_apikey,
                          iam_access_token=iam_access_token,
                          iam_url=iam_url,
                          display_name='Watson')
     self.version = version
示例#17
0
def test_setting_proxy():
    service = BaseService(service_url='test',
                          authenticator=IAMAuthenticator('wonder woman'))
    assert service.authenticator is not None
    assert service.authenticator.token_manager.http_config == {}

    http_config = {"proxies": {"http": "user:password@host:port"}}
    service.set_http_config(http_config)
    assert service.authenticator.token_manager.http_config == http_config

    service2 = BaseService(service_url='test',
                           authenticator=BasicAuthenticator(
                               'marvellous', 'mrs maisel'))
    service2.set_http_config(http_config)
    assert service2.authenticator is not None
示例#18
0
def test_retry_config_disable():
    # Test disabling retries
    service = BaseService(service_url='https://mockurl/',
                          authenticator=NoAuthAuthenticator())
    service.enable_retries()
    service.disable_retries()
    assert service.retry_config is None
    assert service.http_client.get_adapter('https://').max_retries.total == 0

    # Ensure retries are not started after one connection attempt
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error
示例#19
0
def test_configure_service_error():
    service = BaseService(service_url='v1',
                          authenticator=NoAuthAuthenticator())
    with pytest.raises(ValueError) as err:
        service.configure_service(None)
    assert str(err.value) == 'Service_name must be of type string.'
    def __init__(
        self,
        url=default_url,
        username=None,
        password=None,
        iam_apikey=None,
        iam_access_token=None,
        iam_url=None,
        iam_client_id=None,
        iam_client_secret=None,
        icp4d_access_token=None,
        icp4d_url=None,
        authentication_type=None,
    ):
        """
        Construct a new client for the Natural Language Classifier service.

        :param str url: The base url to use when contacting the service (e.g.
               "https://gateway.watsonplatform.net/natural-language-classifier/api/natural-language-classifier/api").
               The base url may differ between IBM Cloud regions.

        :param str username: The username used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of IBM Cloud. When running on
               IBM Cloud, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str password: The password used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of IBM Cloud. When running on
               IBM Cloud, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str iam_apikey: An API key that can be used to request IAM tokens. If
               this API key is provided, the SDK will manage the token and handle the
               refreshing.

        :param str iam_access_token:  An IAM access token is fully managed by the application.
               Responsibility falls on the application to refresh the token, either before
               it expires or reactively upon receiving a 401 from the service as any requests
               made with an expired token will fail.

        :param str iam_url: An optional URL for the IAM service API. Defaults to
               'https://iam.cloud.ibm.com/identity/token'.

        :param str iam_client_id: An optional client_id value to use when interacting with the IAM service.

        :param str iam_client_secret: An optional client_secret value to use when interacting with the IAM service.

        :param str icp4d_access_token:  A ICP4D(IBM Cloud Pak for Data) access token is
               fully managed by the application. Responsibility falls on the application to
               refresh the token, either before it expires or reactively upon receiving a 401
               from the service as any requests made with an expired token will fail.

        :param str icp4d_url: In order to use an SDK-managed token with ICP4D authentication, this
               URL must be passed in.

        :param str authentication_type: Specifies the authentication pattern to use. Values that it
               takes are basic, iam or icp4d.
        """

        BaseService.__init__(self,
                             vcap_services_name='natural_language_classifier',
                             url=url,
                             username=username,
                             password=password,
                             iam_apikey=iam_apikey,
                             iam_access_token=iam_access_token,
                             iam_url=iam_url,
                             iam_client_id=iam_client_id,
                             iam_client_secret=iam_client_secret,
                             use_vcap_services=True,
                             display_name='Natural Language Classifier',
                             icp4d_access_token=icp4d_access_token,
                             icp4d_url=icp4d_url,
                             authentication_type=authentication_type)
示例#21
0
def test_service_url_not_set():
    service = BaseService(service_url='', authenticator=NoAuthAuthenticator())
    with pytest.raises(ValueError) as err:
        service.prepare_request('POST', url='')
    assert str(err.value) == 'The service_url is required'
示例#22
0
 def setup_base(self):
     """SetUp the Base class with service_url and authenticator"""
     BaseService.__init__(self,
                          service_url=URLBuilder.get_base_url(),
                          authenticator=URLBuilder.get_iam_authenticator())
示例#23
0
    def __init__(
        self,
        version,
        url=default_url,
        username=None,
        password=None,
        iam_apikey=None,
        iam_access_token=None,
        iam_url=None,
    ):
        """
        Construct a new client for the Language Translator service.

        :param str version: The API version date to use with the service, in
               "YYYY-MM-DD" format. Whenever the API is changed in a backwards
               incompatible way, a new minor version of the API is released.
               The service uses the API version for the date you specify, or
               the most recent version before that date. Note that you should
               not programmatically specify the current date at runtime, in
               case the API has been updated since your application's release.
               Instead, specify a version date that is compatible with your
               application, and don't change it until your application is
               ready for a later version.

        :param str url: The base url to use when contacting the service (e.g.
               "https://gateway.watsonplatform.net/language-translator/api/language-translator/api").
               The base url may differ between Bluemix regions.

        :param str username: The username used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of Bluemix. When running on
               Bluemix, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str password: The password used to authenticate with the service.
               Username and password credentials are only required to run your
               application locally or outside of Bluemix. When running on
               Bluemix, the credentials will be automatically loaded from the
               `VCAP_SERVICES` environment variable.

        :param str iam_apikey: An API key that can be used to request IAM tokens. If
               this API key is provided, the SDK will manage the token and handle the
               refreshing.

        :param str iam_access_token:  An IAM access token is fully managed by the application.
               Responsibility falls on the application to refresh the token, either before
               it expires or reactively upon receiving a 401 from the service as any requests
               made with an expired token will fail.

        :param str iam_url: An optional URL for the IAM service API. Defaults to
               'https://iam.bluemix.net/identity/token'.
        """

        BaseService.__init__(self,
                             vcap_services_name='language_translator',
                             url=url,
                             username=username,
                             password=password,
                             iam_apikey=iam_apikey,
                             iam_access_token=iam_access_token,
                             iam_url=iam_url,
                             use_vcap_services=True,
                             display_name='Language Translator')
        self.version = version