Skip to content

Commit

Permalink
apiclient: add support for TokenStrategies
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan committed May 13, 2024
1 parent c3ac49e commit b5419e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions apiclient/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
GetTokenTypes = "/tokens/types"

// Strategies endpoints:
GetTokenStrategies = "/strategies/token/%s?chainID=%d&externalID=%s"

// GetStrategiesURI is the URI for getting strategies, it accepts a pageSize,
// nextCursor and prevCursor
Expand Down
34 changes: 34 additions & 0 deletions apiclient/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,37 @@ func (c *HTTPclient) CreateStrategy(request *api.Strategy) (uint64, error) {
}
return strategyID, nil
}

// TokenStrategies returns the strategies of a given token
func (c *HTTPclient) TokenStrategies(tokenID string, chainID uint64, externalID string) (*api.Strategies, error) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetTokenStrategies, tokenID, chainID, externalID)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
strategiesResponse := api.Strategies{Strategies: []*api.Strategy{}}
if err := json.NewDecoder(res.Body).Decode(&strategiesResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return &strategiesResponse, nil
}

0 comments on commit b5419e5

Please sign in to comment.