From 10c3ef6bcfe2a4f6d77441285327130f1900e9a4 Mon Sep 17 00:00:00 2001 From: Don Cote Date: Mon, 30 Sep 2019 13:23:03 -0400 Subject: [PATCH] ability to publish a record to a topic --- README.md | 5 ++++- record.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a4cbf7..26f25ef 100644 --- a/README.md +++ b/README.md @@ -41,5 +41,8 @@ err := client.CompleteRecord(record) err := client.FailRecord(record) ``` - +### Publish New Record +``` +err := client.PublishRecord(data) +``` diff --git a/record.go b/record.go index 424bd13..001d902 100644 --- a/record.go +++ b/record.go @@ -1,6 +1,7 @@ package databridge import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -11,7 +12,12 @@ import ( // Record represents a Data Bridge data record type Record struct { - ID string `json:"id"` + ID string `json:"id"` + Data +} + +// Data represents the data portion of a record, can be used on its own when publishing +type Data struct { Data interface{} `json:"data"` } @@ -89,6 +95,33 @@ func (c Client) GetNextRecord(consumerID string) (*Record, error) { return &record, nil } +// PublishRecord allows for the publishing of a record on the client's topic +func (c Client) PublishRecord(d Data) error { + url := fmt.Sprintf("%s/topics/%s/records?api_key=%s", c.BaseURL, c.TopicSlug, c.APIKey) + + // marshal body to byte array + body, err := json.Marshal(d) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("error marshaling json for data %v", d)) + } + + // make request + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body)) + req.Header.Add("content-type", "application/json") + req.Header.Add("api_key", c.APIKey) + + res, err := http.DefaultClient.Do(req) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("error attempting to publish a record for %s", url)) + } + + if res.StatusCode != 201 { + return errors.New(fmt.Sprintf("databridge publish record responded with non-201 for %s", url)) + } + + return nil +} + // CompleteRecord allows the record to be marked as completed func (c Client) CompleteRecord(r Record) error { return r.updateRecordState(c, "completed")