Skip to content

Commit

Permalink
Go-ws streaming SDK (#111)
Browse files Browse the repository at this point in the history
* Added go-ws web streaming SDK.

* Added missing go.sum

* Updated Readme for go-ws

* Renamed Reconnect to ResetConnection to reflect intent!

* Applied fix for Issue #110

* Updated sample code

* Removed test covering volume bug

* Updated code comment.
  • Loading branch information
marvin-hansen committed Jul 9, 2021
1 parent dd87296 commit 1c5f92b
Show file tree
Hide file tree
Showing 19 changed files with 1,328 additions and 0 deletions.
23 changes: 23 additions & 0 deletions data-api/go-ws/README.md
@@ -0,0 +1,23 @@


## Installation

Install the following dependencies:

```shell
go mod download github.com/bitly/go-simplejson
go mod download github.com/shopspring/decimal
go mod download github.com/gorilla/websocket
```


## Run examples:

```bash
cd /path/to/workspace
export GOPATH=/path/to/workspace
go get -u github.com/CoinAPI/coinapi-sdk/data-api/go-ws
wget -v https://raw.githubusercontent.com/coinapi/coinapi-sdk/master/go-ws/main.go
# update api key inside main.go
go run ./main.go
```
10 changes: 10 additions & 0 deletions data-api/go-ws/api/sdk.go
@@ -0,0 +1,10 @@
package api

import . "go-ws/api/types"

func NewSDK(apiKey string) (sdk SDK) {
validateApiKey(apiKey)
config := getSDKConfig(apiKey)
sdk = getSDK(config)
return sdk
}
16 changes: 16 additions & 0 deletions data-api/go-ws/api/types/config.go
@@ -0,0 +1,16 @@
package types

type SdkConfig struct {
ApiKey string
ApiVersion ApiVersion
EnvironmentType EnvironmentType
ReconnectType ReconnectType
}

// WsConfig webservice configuration
type WsConfig struct {
ApiKey string
Endpoint string
WebsocketKeepalive bool
WebsocketTimeout int
}
48 changes: 48 additions & 0 deletions data-api/go-ws/api/types/enums.go
@@ -0,0 +1,48 @@
package types

// ApiVersion custom ENUM for SDK forward compatibility
type ApiVersion int

const (
ApiV1 ApiVersion = iota
)

// EnvironmentType
// https://docs.coinapi.io/#endpoints-2
type EnvironmentType int

const (
ProdEncrypted EnvironmentType = iota
ProdInsecure
TestEncrypted
TestInsecure
)

// MessageType replicates the official incoming message types as (kinda) string enum.
// https://docs.coinapi.io/#messages
type MessageType string

const (
TRADE MessageType = "trade"
QUOTE MessageType = "quote"
BOOK_L2_FULL MessageType = "book" // Orderbook L2 (Full)
BOOK_L2_TOP_5 MessageType = "book5" // Orderbook L2 (5 best Bid / Ask)
BOOK_L2_TOP_20 MessageType = "book20" // Orderbook L2 (20 best Bid / Ask)
BOOK_L2_TOP_50 MessageType = "book50" // Orderbook L2 (50 best Bid / Ask)
BOOK_L3_FULL MessageType = "book_l3" // Orderbook L3 (Full) https://docs.coinapi.io/#orderbook-l3-full-in
OHLCV MessageType = "ohlcv"
VOLUME MessageType = "volume"
HEARTBEAT MessageType = "hearbeat" // DO NOT FIX! it's a typo in the official msg spec!
ERROR MessageType = "error" // Otherwise processMessage(.) fails to handle heartbeat messages!
EXCHANGERATE MessageType = "exrate"
RECONNECT MessageType = "reconnect"
)

// ReconnectType defines the reconnect behavior upon receiving a reconnect message
// https://docs.coinapi.io/#reconnect-in
type ReconnectType int

const (
OnConnectionClose ReconnectType = iota
OnReconnectMessage
)
11 changes: 11 additions & 0 deletions data-api/go-ws/api/types/functions.go
@@ -0,0 +1,11 @@
package types

// InvokeFunction is a unified function type for all event handlers.
// https://yourbasic.org/golang/function-pointer-type-declaration/
type InvokeFunction func(message *DataMessage) (err error)

// WsHandler handle raw websocket message
type WsHandler func(message []byte)

// WsErrHandler handles raw websocket errors
type WsErrHandler func(err error)
23 changes: 23 additions & 0 deletions data-api/go-ws/api/types/interface.go
@@ -0,0 +1,23 @@
package types

type SDK interface {
SendHello(hello *Hello) (err error)
OpenConnection() (err error)
CloseConnection() (err error)
// ResetConnection hard reset: closes current connection, opens a new one,
// and resends the last hello message. No message buffering!
ResetConnection() (err error)

// sys handlers
SetErrorInvoke(function InvokeFunction)
SetHeartBeatInvoke(function InvokeFunction)
SetReconnectInvoke(function InvokeFunction)

// Data handlers
SetExRateInvoke(function InvokeFunction)
SetTradesInvoke(function InvokeFunction)
SetQuoteInvoke(function InvokeFunction)
SetBookInvoke(function InvokeFunction)
SetOHLCVInvoke(function InvokeFunction)
SetVolumeInvoke(function InvokeFunction)
}

0 comments on commit 1c5f92b

Please sign in to comment.