title | parent | nav_order |
---|---|---|
Go SDK |
SDKs |
0 |
The Anko Go SDK provides access to the Anko Investor forecasts stream over gRPC. It handles reconnection, authentication/ authorization, and
Initialise, if not already initialised, a go project.
$ go mod init my-anko-app
$ go get github.com/anglo-korean/anko-go-sdk
Create the file main.go
with the contents:
package main
import (
"log"
"os"
"time"
"github.com/anglo-korean/anko-go-sdk"
)
var Token = os.Getenv("ANKO_TOKEN")
func main() {
anko.ConnectionTimeout = time.Second // default: 5s
client, err := anko.New(Token, "my-client")
if err != nil {
panic(err)
}
panic(client.Handle(handler))
}
func handler(f *anko.Forecast) error {
log.Printf("ID: %s", f.Id)
log.Printf("Symbol: %v", f.Symbol)
log.Printf("Label: %s", f.Label.String())
log.Printf("Confidence Score: %.3f", f.Score)
log.Print()
return nil
}
Generate an Anko Token and export it as per:
$ export ANKO_TOKEN=anko-token
Where anko-token
is the value of the token generated in the previous step.
From there, the application can be run in the usual go manner; as either:
$ go run main.go
or:
$ go build
$ ./my-anko-app
There may be very little output here, and there be very little for a while; the SDK will fail if there's a problem, and so no output is good output.
This SDK accepts an Anko Token and a per-connection name (which may aid debugging where a single token is used across auto-scaled services) and returns a channel of forecasts for ready consumption.
client.Handle accepts an anko.Handler function, a convention borrowed from packages such as net/http. This function looks like:
type Handler func(*anko.Forecast) error
This function will be called on every Forecast received from the Anko Investor Forecasts gRPC Service. Any returned error will halt the processing of Forecasts. By default these functions are called sequentially; this is the opposite behaviour to http.HandlerFunc, where each handler runs in a new go function.
If this behaviour is important to you, then simply write your anko.Handler to provide its own sync/ dispatch/ gofunc logic.