go-sdk

module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2020 License: Apache-2.0

README

Watson Developer Cloud Go SDK v1.7.0

Build Status Release GitHub go.mod Go version License semantic-release wdc-community.slack.com

Go client library to quickly get started with the various Watson APIs services.

Table of Contents

Before you begin

Installation

Get SDK package:

go get -u github.com/watson-developer-cloud/go-sdk@v1.7.0

Note: For the latest tag release, look into examples folder for basic and advanced examples.

Discovery v2 only on CP4D

Discovery v2 is only available on Cloud Pak for Data.

Running in IBM Cloud

If you run your app in IBM Cloud, the SDK gets credentials from the VCAP_SERVICES environment variable.

Authentication

Watson services are migrating to token-based Identity and Access Management (IAM) authentication.

  • With some service instances, you authenticate to the API by using IAM.
  • In other instances, you authenticate by providing the username and password for the service instance.
Getting credentials

To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:

  1. Go to the IBM Cloud Dashboard page.
  2. Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
  3. Click on the Manage item in the left nav bar of your service instance.

On this page, you should be able to see your credentials for accessing your service instance.

Supplying credentials

There are two ways to supply the credentials you found above to the SDK for authentication.

Credential file

With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing and authenticating. You can get this file by clicking the Download button for the credentials in the Manage tab of your service instance.

The file downloaded will be called ibm-credentials.env. This is the name the SDK will search for and must be preserved unless you want to configure the file path (more on that later). The SDK will look for your ibm-credentials.env file in the following places (in order):

  • The top-level directory of the project you're using the SDK in
  • Your system's home directory

As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following:

import "github.com/watson-developer-cloud/go-sdk/servicev1"

service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
	Version:   "2018-03-05",
})

And that's it!

If you're using more than one service at a time in your code and get two different ibm-credentials.env files, just put the contents together in one ibm-credentials.env file and the SDK will handle assigning credentials to their appropriate services.

If you would like to configure the location/name of your credential file, you can set an environment variable called IBM_CREDENTIALS_FILE. This will take precedence over the locations specified above. Here's how you can do that:

export IBM_CREDENTIALS_FILE="<path>"

where <path> is something like /home/user/Downloads/<file_name>.env.

Manually

If you'd prefer to set authentication values manually in your code, the SDK supports that as well. The way you'll do this depends on what type of credentials your service instance gives you.

IAM

IBM Cloud is migrating to token-based Identity and Access Management (IAM) authentication. IAM authentication uses a service API key to get an access token that is passed with the call. Access tokens are valid for approximately one hour and must be regenerated.

You supply either an IAM service API key or an access token:

  • Use the API key to have the SDK manage the lifecycle of the access token. The SDK requests an access token, ensures that the access token is valid, and refreshes it if necessary.

  • Use the access token if you want to manage the lifecycle yourself. For details, see Authenticating with IAM tokens.

Supplying the IAM API key

// In the constructor, letting the SDK manage the IAM token
import "github.com/watson-developer-cloud/go-sdk/servicev1"
authenticator := &core.IamAuthenticator{
	ApiKey: "<apikey>",
}
service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
		URL:       "<service_url>",
		Authenticator: authenticator,
	})
Username and password
// In the constructor
import "github.com/watson-developer-cloud/go-sdk/servicev1"

service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
		URL:      "<service_url>",
		Authenticator: &core.BasicAuthenticator{
			Username: "<username>",
			Password: "<password>",
		},
	})

Use

Apply these general steps for services present in various packages

  1. Import the service package
  2. Create a new service instance and pass in credentials using either of authentication methods
  3. Invoke API methods using the service instance. For a successful response, it will contain the HTTP status code, response headers and API result
  4. Handle responses and errors

package main

import (
"fmt"
"github.com/watson-developer-cloud/go-sdk/discoveryv1"
)

// Creates a Discovery service instance and does a list of environments
func main() {
// Instantiate the Watson Discovery service
service, serviceErr := discoveryv1.
  NewDiscoveryV1(&discoveryv1.DiscoveryV1Options{
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
service.SetServiceURL("YOUR SERVICE URL")

// Check successful instantiation
if serviceErr != nil {
  panic(serviceErr)
}

// Create a new ListEnvironmentsOptions, these are helper methods.
listEnvironmentsOptions := service.NewListEnvironmentsOptions()

// Call the discovery ListEnvironments method
listEnvironmentResult, response, responseErr := service.ListEnvironments(listEnvironmentsOptions)
// Or you can directly pass in the model options
// listEnvironmentResult, response, responseErr := service.ListEnvironments(&discoveryv1.ListEnvironmentsOptions{})

// Check successful call
if responseErr != nil {
  panic(responseErr)
}

// This will return the `DetailedResponse`
fmt.Println(response)
// Get the HTTP status code
response.GetStatusCode()
// Get the response headers
response.GetHeaders()
// Get the API response
response.GetResult()

// Directly access the result
if listEnvironmentResult != nil {
  fmt.Println(listEnvironmentResult.Environments[0])
  }
}

Configuring the HTTP Client

To change client configs like timeout, setting proxy, etc, pass in your own client using the SetHTTPClient() method. Below is an example to pass a proxy

package main

import (
  "fmt"
  "log"
  "net/http"
  "net/url"

  discovery "github.com/watson-developer-cloud/go-sdk/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:       "YOUR SERVICE URL",
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  //creating the proxyURL
  proxyStr := "http://<proxy host>:<port>"
  proxyURL, err := url.Parse(proxyStr)
  if err != nil {
    log.Println(err)
  }

  //adding the proxy settings to the Transport object
  transport := &http.Transport{
    Proxy: http.ProxyURL(proxyURL),
  }

  //adding the Transport object to the http Client
  client := &http.Client{
    Transport: transport,
  }

  discoveryService.Service.SetHTTPClient(client)
}

Disable SSL certificate verification

Disable the SSL verification using DisableSSLVerification() method

package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:       "YOUR SERVICE URL",
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.Service.DisableSSLVerification()
}

Or can set it from external sources. For example, environment variables:

export <YOUR SERVICE NAME>_DISABLE_SSL=True

## Set Service URL
To set the service URL,

```go
service.SetServiceURL("my new url")

Or can set it from external sources. For example, environment variables:

export <YOUR SERVICE NAME>_URL="my new url"

Getting the transaction ID

Every SDK call returns a response with a transaction ID in the x-global-transaction-id header. This transaction ID is useful for troubleshooting and accessing relevant logs from your service instance.

result, response, responseErr := service.MyServiceCall(myServiceOptions)
fmt.Println(response.GetHeaders().Get("X-Global-Transaction-Id"))

Cloud Pak for Data(CP4D)

If your service instance is of ICP4D, below are two ways of initializing the assistant service.

  1. Supplying the Username, Password, ICP4DURL and AuthenticationType
package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
	authenticator := &core.CloudPakForDataAuthenticator{
		URL:                    "CP4D URL FOR AUTHENTICATION", // should be of the form https://{icp_cluster_host}
		Username:               "YOUR USERNAME", 
		Password:               "YOUR PASSWORD",
		DisableSSLVerification: true,
	}
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:                "YOUR SERVICE URL", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api
    Version:            "2018-03-05",
	Authenticator: authenticator,
  })

  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED
}
  1. Supplying the access token
package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/discoveryv1"
)

func main() {
	authenticator := &core.BearerTokenAuthenticator{
		BearerToken: "YOUR BEARER TOKEN",
	}

  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:              "YOUR SERVICE URL", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api
    Version:          "2018-03-05",
    Authenticator: authenticator,
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED
}

Examples

The examples folder has basic and advanced examples. The examples within each service assume that you already have service credentials.

Tests

Run all test suites:

go test ./...

Get code coverage for each test suite:

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Run a specific test suite:

go test ./assistantv1

Questions

If you have issues with the APIs or have a question about the Watson services, see Stack Overflow.

Migrating

Contributing

See CONTRIBUTING.

We'd love to highlight cool open-source projects that use this SDK! If you'd like to get your project added to the list, feel free to make an issue linking us to it.

License

This library is licensed under the Apache 2.0 license.

Directories

Path Synopsis
Package assistantv1 : Operations and models for the AssistantV1 service
Package assistantv1 : Operations and models for the AssistantV1 service
Package assistantv2 : Operations and models for the AssistantV2 service
Package assistantv2 : Operations and models for the AssistantV2 service
Package comparecomplyv1 : Operations and models for the CompareComplyV1 service
Package comparecomplyv1 : Operations and models for the CompareComplyV1 service
Package discoveryv1 : Operations and models for the DiscoveryV1 service
Package discoveryv1 : Operations and models for the DiscoveryV1 service
Package discoveryv2 : Operations and models for the DiscoveryV2 service
Package discoveryv2 : Operations and models for the DiscoveryV2 service
examples
Package languagetranslatorv3 : Operations and models for the LanguageTranslatorV3 service
Package languagetranslatorv3 : Operations and models for the LanguageTranslatorV3 service
Package naturallanguageclassifierv1 : Operations and models for the NaturalLanguageClassifierV1 service
Package naturallanguageclassifierv1 : Operations and models for the NaturalLanguageClassifierV1 service
Package naturallanguageunderstandingv1 : Operations and models for the NaturalLanguageUnderstandingV1 service
Package naturallanguageunderstandingv1 : Operations and models for the NaturalLanguageUnderstandingV1 service
Package personalityinsightsv3 : Operations and models for the PersonalityInsightsV3 service
Package personalityinsightsv3 : Operations and models for the PersonalityInsightsV3 service
Package speechtotextv1 : Operations and models for the SpeechToTextV1 service
Package speechtotextv1 : Operations and models for the SpeechToTextV1 service
Package texttospeechv1 : Operations and models for the TextToSpeechV1 service
Package texttospeechv1 : Operations and models for the TextToSpeechV1 service
Package toneanalyzerv3 : Operations and models for the ToneAnalyzerV3 service
Package toneanalyzerv3 : Operations and models for the ToneAnalyzerV3 service
Package visualrecognitionv3 : Operations and models for the VisualRecognitionV3 service
Package visualrecognitionv3 : Operations and models for the VisualRecognitionV3 service
Package visualrecognitionv4 : Operations and models for the VisualRecognitionV4 service
Package visualrecognitionv4 : Operations and models for the VisualRecognitionV4 service

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL