Go client library to quickly get started with the various Watson APIs services.
Table of Contents
- You need an IBM Cloud account.
Get SDK package:
go get -u github.com/watson-developer-cloud/[email protected]
Note: For the latest tag release, look into examples folder for basic and advanced examples.
Discovery v2 is only available on Cloud Pak for Data.
If you run your app in IBM Cloud, the SDK gets credentials from the VCAP_SERVICES
environment variable.
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.
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:
- Go to the IBM Cloud Dashboard page.
- Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
- 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.
There are two ways to supply the credentials you found above to the SDK for authentication.
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
.
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.
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,
})
// 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>",
},
})
Apply these general steps for services present in various packages
- Import the service package
- Create a new service instance and pass in credentials using either of authentication methods
- Invoke API methods using the service instance. For a successful response, it will contain the HTTP status code, response headers and API result
- 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])
}
}
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 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"
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"))
If your service instance is of ICP4D, below are two ways of initializing the assistant service.
- Supplying the
Username
,Password
,ICP4DURL
andAuthenticationType
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
}
- 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
}
The examples folder has basic and advanced examples. The examples within each service assume that you already have service credentials.
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
If you have issues with the APIs or have a question about the Watson services, see Stack Overflow.
- To move from
v0.11.0
tov1.0.0
, refer to the MIGRATION-V1
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.
This library is licensed under the Apache 2.0 license.