-
-
Notifications
You must be signed in to change notification settings - Fork 12
Add webhooks for OURA #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
61a0934
599118c
48c809f
01c769f
aea76a4
e29bcbf
da418c4
9a99e16
80d6b51
0242687
20a5de8
a4b8333
a951e35
27ec01d
d0d7ec6
a3aa538
9995641
a9e257a
6b33383
8af6266
d0e99a7
7b057b0
002e3c0
e52c935
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package customerio | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/tidepool-org/platform/client" | ||
| "github.com/tidepool-org/platform/errors" | ||
| "github.com/tidepool-org/platform/log" | ||
| "github.com/tidepool-org/platform/request" | ||
| ) | ||
|
|
||
| type Client struct { | ||
| appClient *client.Client | ||
| trackClient *client.Client | ||
| config Config | ||
| logger log.Logger | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| type Config struct { | ||
| AppAPIBaseURL string `envconfig:"TIDEPOOL_CUSTOMERIO_APP_API_BASE_URL" default:"https://api.customer.io"` | ||
| AppAPIKey string `envconfig:"TIDEPOOL_CUSTOMERIO_APP_API_KEY"` | ||
| SiteID string `envconfig:"TIDEPOOL_CUSTOMERIO_SITE_ID"` | ||
| TrackAPIBaseURL string `envconfig:"TIDEPOOL_CUSTOMERIO_TRACK_API_BASE_URL" default:"https://track.customer.io"` | ||
| TrackAPIKey string `envconfig:"TIDEPOOL_CUSTOMERIO_TRACK_API_KEY"` | ||
| } | ||
|
|
||
| func NewClient(config Config, logger log.Logger) (*Client, error) { | ||
| errorParser := newErrorResponseParser() | ||
|
|
||
| appClient, err := client.NewWithErrorParser(&client.Config{ | ||
| Address: config.AppAPIBaseURL, | ||
| }, errorParser) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to create app API client") | ||
| } | ||
|
|
||
| trackClient, err := client.NewWithErrorParser(&client.Config{ | ||
| Address: config.TrackAPIBaseURL, | ||
| }, errorParser) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to create track API client") | ||
| } | ||
|
|
||
| return &Client{ | ||
| appClient: appClient, | ||
| trackClient: trackClient, | ||
| config: config, | ||
| logger: logger, | ||
| httpClient: http.DefaultClient, | ||
| }, nil | ||
| } | ||
|
|
||
| // appAPIAuthMutator returns a request mutator for App API authentication (Bearer token) | ||
| func (c *Client) appAPIAuthMutator() *request.HeaderMutator { | ||
| return request.NewHeaderMutator("Authorization", fmt.Sprintf("Bearer %s", c.config.AppAPIKey)) | ||
| } | ||
|
|
||
| // trackAPIAuthMutator returns a request mutator for Track API authentication (Basic auth) | ||
| func (c *Client) trackAPIAuthMutator() *request.HeaderMutator { | ||
| auth := c.config.SiteID + ":" + c.config.TrackAPIKey | ||
| basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) | ||
| return request.NewHeaderMutator("Authorization", basicAuth) | ||
| } | ||
|
|
||
| // errorResponseParser implements client.ErrorResponseParser for Customer.io API errors | ||
| type errorResponseParser struct{} | ||
|
|
||
| func newErrorResponseParser() *errorResponseParser { | ||
| return &errorResponseParser{} | ||
| } | ||
|
|
||
| func (p *errorResponseParser) ParseErrorResponse(ctx context.Context, res *http.Response, req *http.Request) error { | ||
| var errResp errorResponse | ||
| if err := json.NewDecoder(res.Body).Decode(&errResp); err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| if len(errResp.Errors) > 0 { | ||
| return errors.Newf("API error (status %d): %s", res.StatusCode, errResp.Errors[0].Message) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has there ever been more than one error here? If so, it might be useful to in some way at least log the other errors—they might provide useful hints as to what went wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've only seen a single error during testing |
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type errorResponse struct { | ||
| Errors []struct { | ||
| Reason string `json:"reason,omitempty"` | ||
| Field string `json:"field,omitempty"` | ||
| Message string `json:"message,omitempty"` | ||
| } `json:"errors,omitempty"` | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.