initial commit

This commit is contained in:
Don Cote 2019-07-10 11:19:10 -04:00
parent 7be26d27ba
commit fdee15abfb
4 changed files with 145 additions and 0 deletions

View File

@ -1,2 +1,28 @@
# go-data-bridge-client
Go client for accessing Data Bridge
## Usage
### Initialization
```
config := Config{APIKey: "api-key", TopicSlug: "my-topic-slug", ConsumerID: "consumer-id"}
client, err := NewClient(config)
```
### Get Next Record
```
record, err := client.GetNextRecord()
```
### Mark Record Completed
```
err := client.CompleteRecord(record)
```
### Mark Record Failed
```
err := client.FailRecord(record)
```

36
client.go Normal file
View File

@ -0,0 +1,36 @@
package databridge
type Config struct {
APIKey string
ConsumerID string
TopicSlug string
}
type Client struct {
BaseURL string
APIKey string
TopicSlug string
ConsumerID string
}
func NewClient(config Config) (Client, error) {
c := Client{}
c.APIKey = config.APIKey
c.BaseURL = "https://data-bridge.flipsidecrypto.com/api/v1"
c.TopicSlug = config.TopicSlug
c.ConsumerID = config.ConsumerID
return c, nil
}
func (c Client) GetNextRecord() (*Record, error) {
return getNextRecord(c)
}
func (c Client) CompleteRecord(r Record) error {
return r.updateRecordState(c, "completed")
}
func (c Client) FailRecord(r Record) error {
return r.updateRecordState(c, "failed")
}

22
client_test.go Normal file
View File

@ -0,0 +1,22 @@
package databridge
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestClient_NewClient(t *testing.T) {
config := Config{APIKey: "api-key", TopicSlug: "my-topic-slug", ConsumerID: "consumer-id"}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
require.Equal(t, "api-key", client.APIKey)
require.Equal(t, "consumer-id", client.ConsumerID)
require.Equal(t, "my-topic-slug", client.TopicSlug)
require.Equal(t, "https://data-bridge.flipsidecrypto.com/api/v1", client.BaseURL)
}

61
record.go Normal file
View File

@ -0,0 +1,61 @@
package databridge
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Record struct {
ID string `json:"id"`
ConsumerID string `json:"consumer_id"`
TopicSlug string `json:"topic_slug"`
RetryCount int32 `json:"retry_count"`
Partition int32 `json:"partition"`
Offset int32 `json:"offset"`
}
func getNextRecord(c Client) (*Record, error) {
url := fmt.Sprintf("%s/topics/%s/records/next?consumer_id=%s&api_key=%s", c.BaseURL, c.TopicSlug, c.ConsumerID, c.APIKey)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("content-type", "application/json")
req.Header.Add("api_key", c.APIKey)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var record Record
err = json.Unmarshal([]byte(body), &record)
if err != nil {
return nil, err
}
return &record, nil
}
func (r Record) updateRecordState(c Client, state string) error {
url := fmt.Sprintf("%s/records/%s/state/%s?api_key=%s", c.BaseURL, r.ID, state, c.APIKey)
req, _ := http.NewRequest("PUT", url, nil)
req.Header.Add("content-type", "application/json")
req.Header.Add("api_key", c.APIKey)
_, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
// todo - probably get more granular on checking this response
return nil
}