diff --git a/README.md b/README.md index 807d8e1..7ba1bf4 100644 --- a/README.md +++ b/README.md @@ -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) +``` + + + diff --git a/client.go b/client.go new file mode 100644 index 0000000..6a22ccd --- /dev/null +++ b/client.go @@ -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") +} diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..ecf1d4a --- /dev/null +++ b/client_test.go @@ -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) +} diff --git a/record.go b/record.go new file mode 100644 index 0000000..c20ad93 --- /dev/null +++ b/record.go @@ -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 +}