diff --git a/apiclient/client.go b/apiclient/client.go index 14eaf36d..4db28487 100644 --- a/apiclient/client.go +++ b/apiclient/client.go @@ -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 +} diff --git a/apiclient/routes.go b/apiclient/routes.go index 2ac06744..2db46880 100644 --- a/apiclient/routes.go +++ b/apiclient/routes.go @@ -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: diff --git a/apiclient/strategies.go b/apiclient/strategies.go index 3f1901ea..f8114dc4 100644 --- a/apiclient/strategies.go +++ b/apiclient/strategies.go @@ -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)