Skip to content

Commit

Permalink
Merge branch 'main' into f/multiple_endpoints_by_chain
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Apr 11, 2024
2 parents d0752b3 + e434723 commit 4a6cee6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
8 changes: 4 additions & 4 deletions apiclient/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"go.vocdoni.io/dvote/log"
)

// GetCensus method returns a census by its ID from the API, it receives the
// Census method returns a census by its ID from the API, it receives the
// censusID and returns a pointer to a GetCensusResponse and an error if something
// went wrong.
func (c *HTTPclient) GetCensus(censusID uint64) (*api.Census, error) {
func (c *HTTPclient) Census(censusID uint64) (*api.Census, error) {
if censusID == 0 {
return nil, fmt.Errorf("%w: censusID is required", ErrBadInputs)
}
Expand Down Expand Up @@ -133,10 +133,10 @@ func (c *HTTPclient) CreateCensusQueue(queueID string) (*api.CensusQueue, error)
return response, nil
}

// GetCensusesByStrategy method returns the censuses of a strategy from the API,
// CensusesByStrategy method returns the censuses of a strategy from the API,
// it receives the strategyID and returns a slice of GetCensusResponse pointers
// and an error if something went wrong.
func (c *HTTPclient) GetCensusesByStrategy(strategyID uint64) ([]*api.Census, error) {
func (c *HTTPclient) CensusesByStrategy(strategyID uint64) ([]*api.Census, error) {
if strategyID == 0 {
return nil, fmt.Errorf("%w: strategyID is required", ErrBadInputs)
}
Expand Down
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
38 changes: 36 additions & 2 deletions apiclient/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.vocdoni.io/dvote/log"
)

func (c *HTTPclient) GetStrategies(pageSize int, nextCursor, prevCursor string) (
func (c *HTTPclient) Strategies(pageSize int, nextCursor, prevCursor string) (
[]*api.Strategy, error,
) {
// construct the URL to the API with the given parameters
Expand Down Expand Up @@ -45,7 +45,7 @@ func (c *HTTPclient) GetStrategies(pageSize int, nextCursor, prevCursor string)
return strategiesResponse.Strategies, nil
}

func (c *HTTPclient) GetStrategy(strategyID uint64) (*api.Strategy, error) {
func (c *HTTPclient) Strategy(strategyID uint64) (*api.Strategy, error) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetStrategyURI, strategyID)
u, err := c.constructURL(endpoint)
Expand Down Expand Up @@ -78,6 +78,40 @@ func (c *HTTPclient) GetStrategy(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
16 changes: 8 additions & 8 deletions apiclient/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"go.vocdoni.io/dvote/log"
)

// GetTokens method returns a list of tokens from the API, it accepts a
// Tokens method returns a list of tokens from the API, it accepts a
// pageSize, nextCursor and prevCursor. If the pageSize is -1 and cursors are
// empty, it will return all the tokens. If something goes wrong, it will return
// an error.
func (c *HTTPclient) GetTokens(pageSize int, nextCursor, prevCursor string) ([]*api.TokenListItem, error) {
func (c *HTTPclient) Tokens(pageSize int, nextCursor, prevCursor string) ([]*api.TokenListItem, error) {
// construct the URL to the API with the pageSize, nextCursor and prevCursor
endpoint := fmt.Sprintf(GetTokensURI, pageSize, nextCursor, prevCursor)
u, err := c.constructURL(endpoint)
Expand Down Expand Up @@ -48,9 +48,9 @@ func (c *HTTPclient) GetTokens(pageSize int, nextCursor, prevCursor string) ([]*
return tokensResponse.Tokens, nil
}

// GetToken method returns a token from the API, it accepts the tokenID, chainID
// Token method returns a token from the API, it accepts the tokenID, chainID
// and externalID. If something goes wrong, it will return an error.
func (c *HTTPclient) GetToken(tokenID string, chainID uint64, externalID string) (*api.Token, error) {
func (c *HTTPclient) Token(tokenID string, chainID uint64, externalID string) (*api.Token, error) {
if tokenID == "" || chainID == 0 {
return nil, fmt.Errorf("%w: tokenID and chainID are required", ErrBadInputs)
}
Expand Down Expand Up @@ -211,10 +211,10 @@ func (c *HTTPclient) DeleteTokenQueue(tokenID string, chainID uint64, externalID
return queueResponse, nil
}

// GetTokenHolder method returns the balance of a token holder from the API, it
// TokenHolder method returns the balance of a token holder from the API, it
// accepts the tokenID, chainID, externalID and holderID. If something goes
// wrong, it will return an error.
func (c *HTTPclient) GetTokenHolder(tokenID string, chainID uint64, externalID, holderID string) (*big.Int, error) {
func (c *HTTPclient) TokenHolder(tokenID string, chainID uint64, externalID, holderID string) (*big.Int, error) {
if tokenID == "" || chainID == 0 || holderID == "" {
return nil, fmt.Errorf("%w: tokenID, chainID and holderID are required", ErrBadInputs)
}
Expand Down Expand Up @@ -255,10 +255,10 @@ func (c *HTTPclient) GetTokenHolder(tokenID string, chainID uint64, externalID,
return bBalance, nil
}

// GetTokenTypes method returns the supported token types from the API. If
// TokenTypes method returns the supported token types from the API. If
// something goes wrong, it will return an error. It returns the supported token
// types as a slice of strings.
func (c *HTTPclient) GetTokenTypes() ([]string, error) {
func (c *HTTPclient) TokenTypes() ([]string, error) {
// construct the URL to the API
u, err := c.constructURL(GetTokenTypes)
if err != nil {
Expand Down

0 comments on commit 4a6cee6

Please sign in to comment.