Compare commits

...

3 Commits

Author SHA1 Message Date
Jim Myers
7a8513e754 Allow baseurl for databridge to be altered. 2021-11-08 11:51:51 -05:00
Don Cote
cd9c4b3233 capture 404 and return nils 2019-10-07 11:04:35 -04:00
Don Cote
53c24ea952 skip getting unread count 2019-10-07 10:55:32 -04:00
2 changed files with 11 additions and 8 deletions

View File

@ -4,6 +4,7 @@ package databridge
type Config struct {
APIKey string
TopicSlug string
BaseURL string
}
// Client allows access to the Databridge API
@ -17,7 +18,11 @@ type Client struct {
func NewClient(config Config) (Client, error) {
c := Client{}
c.APIKey = config.APIKey
c.BaseURL = "https://data-bridge.flipsidecrypto.com/api/v1"
if config.BaseURL != "" {
c.BaseURL = config.BaseURL
} else {
c.BaseURL = "https://data-bridge.flipsidecrypto.com/api/v1"
}
c.TopicSlug = config.TopicSlug
return c, nil

View File

@ -58,13 +58,6 @@ func (c Client) GetUnreadCount() (*int32, error) {
// GetNextRecord returns the topic's next record. Will return nil without an error when there are no more records.
func (c Client) GetNextRecord(consumerID string) (*Record, error) {
count, err := c.GetUnreadCount()
if err != nil {
return nil, err
} else if *count == 0 {
return nil, nil
}
url := fmt.Sprintf("%s/topics/%s/records/next?consumer_id=%s&api_key=%s", c.BaseURL, c.TopicSlug, consumerID, c.APIKey)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("content-type", "application/json")
@ -75,6 +68,11 @@ func (c Client) GetNextRecord(consumerID string) (*Record, error) {
return nil, errors.Wrap(err, fmt.Sprintf("error making databridge http request for %s", url))
}
// capture 404 and return nils
if res.StatusCode == 404 {
return nil, nil
}
if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("databridge responded with non-200 for %s", url))
}