Skip to content

Commit

Permalink
apiclient: add support for /info & /strategies/{strategyID}/holders
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan committed Apr 11, 2024
1 parent b0feb9b commit e434723
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,19 @@ func (c *HTTPclient) Request(method string, jsonBody any, urlPath ...string) ([]
}
return data, resp.StatusCode, nil
}

// Info returns the API server info.
func (c *HTTPclient) Info() (*api.APIInfo, error) {
data, status, err := c.Request(HTTPGET, nil, "info")
if err != nil {
return nil, err
}
if status != apirest.HTTPstatusOK {
return nil, fmt.Errorf("API error: %d (%s)", status, data)
}
info := &api.APIInfo{}
if err := json.Unmarshal(data, info); err != nil {
return nil, err
}
return info, nil
}
2 changes: 2 additions & 0 deletions apiclient/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
// CreateStrategyURI is the URI for creating a strategy, it accepts no
// parameters
CreateStrategyURI = "/strategies"
// GetTokenHoldersByStrategyURI is the URI for getting token holders of a given strategy
GetTokenHoldersByStrategyURI = "/strategies/%d/holders"

// Censuses endpoints:

Expand Down
34 changes: 34 additions & 0 deletions apiclient/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ func (c *HTTPclient) Strategy(strategyID uint64) (*api.Strategy, error) {
return strategyResponse, nil
}

// HoldersByStrategy returns the holders of a strategy
func (c *HTTPclient) HoldersByStrategy(strategyID uint64) (*api.GetStrategyHoldersResponse, error) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetTokenHoldersByStrategyURI, strategyID)
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)))
}
holdersResponse := &api.GetStrategyHoldersResponse{}
if err := json.NewDecoder(res.Body).Decode(holdersResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return holdersResponse, nil
}

func (c *HTTPclient) CreateStrategy(request *api.Strategy) (uint64, error) {
// construct the URL to the API
u, err := c.constructURL(CreateStrategyURI)
Expand Down

0 comments on commit e434723

Please sign in to comment.