Skip to content

Instantly share code, notes, and snippets.

@sacOO7
Last active July 19, 2018 18:31
Show Gist options
  • Select an option

  • Save sacOO7/65e647daacb218e5257c6aa431ef4545 to your computer and use it in GitHub Desktop.

Select an option

Save sacOO7/65e647daacb218e5257c6aa431ef4545 to your computer and use it in GitHub Desktop.
Coinigy API client implementation in Golang using Socketcluster go client (https://github.com/sacOO7/socketcluster-client-go)
// This is a console go application to connect conigy api, please replace api keys with your own coinigy credentials
package main
import (
"github.com/sacOO7/socketcluster-client-go/scclient"
"text/scanner"
"os"
"fmt"
"reflect"
"encoding/json"
"github.com/sacOO7/go-logger"
)
type Empty struct {
}
var logger = logging.GetLogger(reflect.TypeOf(Empty{}).PkgPath())
type AuthData struct {
ApiKey string `json:"apiKey"`
ApiSecret string `json:"apiSecret"`
}
var authCred = AuthData{ ApiKey: "xxxx", ApiSecret: "xxxx"}
func yourCodeStartsHere(client scclient.Client) {
// start writing your code from here
//Code for subscription
client.Subscribe("TRADE-BITF--BTC--USD") // Channel to be subscribed
// This is used for watching messages over channel
client.OnChannel("TRADE-BITF--BTC--USD", func(eventName string, data interface{}) { // Messages will be received here
logger.Info.Println("Data received :: ", JsonDump(data), " from channel "+eventName)
})
// Code for getting exchanges
client.EmitAck("exchanges", nil, func(eventName string, error interface{}, data interface{}) {
logger.Info.Println("Exchange List :: ", JsonDump(data))
})
// Code for getting channels
client.EmitAck("channels", "GDAX", func(eventName string, error interface{}, data interface{}) {
logger.Info.Println("Channel list :: ", JsonDump(data))
})
}
func onConnect(client scclient.Client) {
logger.Info.Println("Connected to server")
}
func onDisconnect(client scclient.Client, err error) {
logger.Info.Println("Disconnected from server")
os.Exit(1)
}
func onConnectError(client scclient.Client, err error) {
logger.Info.Println("Error occured while connecting to server%s\n", err.Error())
os.Exit(1)
}
func onSetAuthentication(client scclient.Client, token string) {
logger.Info.Println("Received auth Token " + token)
}
func onAuthentication(client scclient.Client, isAuthenticated bool) {
logger.Info.Println("Received authentication status ", isAuthenticated)
client.EmitAck("auth", authCred, func(eventName string, error interface{}, data interface{}) {
logger.Info.Println("Token is ", JsonDump(data))
yourCodeStartsHere(client)
})
}
func JsonDump(data interface{}) string {
b, _ := json.MarshalIndent(data, "", " ")
return string(b);
}
func main() {
var reader scanner.Scanner
client := scclient.New("wss://sc-02.coinigy.com/socketcluster/");
client.SetBasicListener(onConnect, onConnectError, onDisconnect)
client.SetAuthenticationListener(onSetAuthentication, onAuthentication)
go client.Connect()
fmt.Println("Enter any key to terminate the program")
reader.Init(os.Stdin)
reader.Next()
// os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment