go-sdk

module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2018 License: Apache-2.0

README

Watson Developer Cloud Go SDK

Build Status semantic-release

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 github.com/watson-developer-cloud/go-sdk

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 or click Create resource > AI and create a service instance.
  3. Copy the url and either apikey or username and password. Click Show if the credentials are masked.
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
discovery, discoveryErr := NewDiscoveryV1(&DiscoveryV1Options{
		URL:       "<service_url>",
		Version:   "2018-03-05",
		IAMApiKey: "<apikey>",
	})

Supplying the access token

// In the constructor, assuming control of managing IAM token
discovery, discoveryErr := NewDiscoveryV1(&DiscoveryV1Options{
		URL:            "<service_url>",
		Version:        "2018-03-05",
		IAMAccessToken: "<IAM_access_token>",
	})
Username and password
// In the constructor
discovery, discoveryErr := NewDiscoveryV1(&DiscoveryV1Options{
		URL:      "<service_url>",
		Version:  "2018-03-05",
		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{
    URL:       "YOUR SERVICE URL",
    Version:   "2018-03-05",
    IAMApiKey: "YOUR APIKEY",
  })

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

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

// Call the discovery ListEnvironments method
response, responseErr := service.ListEnvironments(listEnvironmentsOptions)
// Or you can directly pass in the model options
// 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()

// Cast response to the specific dataType returned by ListEnvironments
// NOTE: most methods have a corresponding Get<methodName>Result() function
listEnvironmentResult := service.GetListEnvironmentsResult(response)

if listEnvironmentResult != nil {
  fmt.Println(listEnvironmentResult.Environments[0])
  }
}

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

Contributing

See CONTRIBUTING.

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 discoveryv1 : Operations and models for the DiscoveryV1 service
Package discoveryv1 : Operations and models for the DiscoveryV1 service
examples
assistantv1 command
assistantv2 command
discoveryv1 command
speechtotextv1 command
texttospeechv1 command
toneanalyzerv3 command
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

Jump to

Keyboard shortcuts

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