Skip to content

Commit

Permalink
Code generation for more of the http client (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
winder authored Mar 31, 2021
1 parent 24309c0 commit adb6d4b
Show file tree
Hide file tree
Showing 84 changed files with 3,765 additions and 4,107 deletions.
22 changes: 18 additions & 4 deletions client/v2/algod/accountInformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ package algod
import (
"context"
"fmt"

"github.com/algorand/go-algorand-sdk/client/v2/common"
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
)

// AccountInformationParams contains all of the query parameters for url serialization.
type AccountInformationParams struct {

// Format configures whether the response object is JSON or MessagePack encoded.
Format string `url:"format,omitempty"`
}

// AccountInformation given a specific account public key, this call returns the
// accounts status, balance and spendable amounts
type AccountInformation struct {
c *Client
account string
c *Client

address string

p AccountInformationParams
}

func (s *AccountInformation) Do(ctx context.Context, headers ...*common.Header) (result models.Account, err error) {
err = s.c.get(ctx, &result, fmt.Sprintf("/v2/accounts/%s", s.account), nil, headers)
// Do performs the HTTP request
func (s *AccountInformation) Do(ctx context.Context, headers ...*common.Header) (response models.Account, err error) {
err = s.c.get(ctx, &response, fmt.Sprintf("/v2/accounts/%v", s.address), s.p, headers)
return
}
73 changes: 45 additions & 28 deletions client/v2/algod/algod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"

"github.com/algorand/go-algorand-sdk/client/v2/common"
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
)

const algodAuthHeader = "X-Algo-API-Token"
const authHeader = "X-Algo-API-Token"

type Client common.Client

Expand Down Expand Up @@ -34,67 +35,83 @@ func (c *Client) post(ctx context.Context, response interface{}, path string, bo

// MakeClient is the factory for constructing a ClientV2 for a given endpoint.
func MakeClient(address string, apiToken string) (c *Client, err error) {
commonClient, err := common.MakeClient(address, algodAuthHeader, apiToken)
commonClient, err := common.MakeClient(address, authHeader, apiToken)
c = (*Client)(commonClient)
return
}

func (c *Client) AccountInformation(account string) *AccountInformation {
return &AccountInformation{c: c, account: account}
func (c *Client) HealthCheck() *HealthCheck {
return &HealthCheck{c: c}
}

func (c *Client) Block(round uint64) *Block {
return &Block{c: c, round: round}
func (c *Client) GetGenesis() *GetGenesis {
return &GetGenesis{c: c}
}

func (c *Client) BlockRaw(round uint64) *BlockRaw {
return &BlockRaw{c: c, round: round}
func (c *Client) Versions() *Versions {
return &Versions{c: c}
}

func (c *Client) HealthCheck() *HealthCheck {
return &HealthCheck{c: c}
func (c *Client) AccountInformation(address string) *AccountInformation {
return &AccountInformation{c: c, address: address}
}

func (c *Client) PendingTransactionInformation(txid string) *PendingTransactionInformation {
return &PendingTransactionInformation{c: c, txid: txid}
func (c *Client) PendingTransactionsByAddress(address string) *PendingTransactionsByAddress {
return &PendingTransactionsByAddress{c: c, address: address}
}

func (c *Client) PendingTransactionsByAddress(address string) *PendingTransactionInformationByAddress {
return &PendingTransactionInformationByAddress{c: c, address: address}
func (c *Client) Block(round uint64) *Block {
return &Block{c: c, round: round}
}

func (c *Client) PendingTransactions() *PendingTransactions {
return &PendingTransactions{c: c}
func (c *Client) GetProof(round uint64, txid string) *GetProof {
return &GetProof{c: c, round: round, txid: txid}
}

func (c *Client) Supply() *Supply {
return &Supply{c: c}
}

func (c *Client) SendRawTransaction(tx []byte) *SendRawTransaction {
return &SendRawTransaction{c: c, stx: tx}
func (c *Client) Status() *Status {
return &Status{c: c}
}

func (c *Client) StatusAfterBlock(round uint64) *StatusAfterBlock {
return &StatusAfterBlock{c: c, round: round}
}

func (c *Client) Status() *Status {
return &Status{c: c}
func (c *Client) SendRawTransaction(rawtxn []byte) *SendRawTransaction {
return &SendRawTransaction{c: c, rawtxn: rawtxn}
}

func (c *Client) SuggestedParams() *SuggestedParams {
return &SuggestedParams{c: c}
}

func (c *Client) Supply() *Supply {
return &Supply{c: c}
func (c *Client) PendingTransactions() *PendingTransactions {
return &PendingTransactions{c: c}
}

func (c *Client) Versions() *Versions {
return &Versions{c: c}
func (c *Client) PendingTransactionInformation(txid string) *PendingTransactionInformation {
return &PendingTransactionInformation{c: c, txid: txid}
}

func (c *Client) GetGenesis() *GetGenesis {
return &GetGenesis{c: c}
func (c *Client) GetApplicationByID(applicationId uint64) *GetApplicationByID {
return &GetApplicationByID{c: c, applicationId: applicationId}
}

func (c *Client) GetProof(round uint64, txid string) *GetProof {
return &GetProof{c: c, round: round, txid: txid}
func (c *Client) GetAssetByID(assetId uint64) *GetAssetByID {
return &GetAssetByID{c: c, assetId: assetId}
}

func (c *Client) TealCompile(source []byte) *TealCompile {
return &TealCompile{c: c, source: source}
}

func (c *Client) TealDryrun(request models.DryrunRequest) *TealDryrun {
return &TealDryrun{c: c, request: request}
}

func (c *Client) BlockRaw(round uint64) *BlockRaw {
return &BlockRaw{c: c, round: round}
}
33 changes: 0 additions & 33 deletions client/v2/algod/application_client.go

This file was deleted.

9 changes: 7 additions & 2 deletions client/v2/algod/blockRaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import (
"fmt"

"github.com/algorand/go-algorand-sdk/client/v2/common"
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
)

// GetBlockParams defines parameters for GetBlock.
type GetBlockParams struct {
// Return raw msgpack block bytes or json
Format string `url:"format,omitempty"`
}

// BlockRaw contains metadata required to execute a BlockRaw query.
type BlockRaw struct {
c *Client
round uint64
p models.GetBlockParams
p GetBlockParams
}

// Do executes the BlockRaw query and gets the results.
Expand Down
17 changes: 8 additions & 9 deletions client/v2/algod/getApplicationByID.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
)

// GetApplicationByID /v2/applications/{application-id}
// Given a application id, it returns application information including creator,
// approval and clear programs, global and local schemas, and global state.
// GetApplicationByID given a application id, it returns application information
// including creator, approval and clear programs, global and local schemas, and
// global state.
type GetApplicationByID struct {
c *Client
c *Client

applicationId uint64
}

// Do performs HTTP request
func (s *GetApplicationByID) Do(ctx context.Context,
headers ...*common.Header) (response models.Application, err error) {
err = s.c.get(ctx, &response,
fmt.Sprintf("/v2/applications/%d", s.applicationId), nil, headers)
// Do performs the HTTP request
func (s *GetApplicationByID) Do(ctx context.Context, headers ...*common.Header) (response models.Application, err error) {
err = s.c.get(ctx, &response, fmt.Sprintf("/v2/applications/%v", s.applicationId), nil, headers)
return
}
16 changes: 7 additions & 9 deletions client/v2/algod/getAssetByID.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import (
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
)

// GetAssetByID /v2/assets/{asset-id}
// Given a asset id, it returns asset information including creator, name, total
// supply and special addresses.
// GetAssetByID given a asset id, it returns asset information including creator,
// name, total supply and special addresses.
type GetAssetByID struct {
c *Client
c *Client

assetId uint64
}

// Do performs HTTP request
func (s *GetAssetByID) Do(ctx context.Context,
headers ...*common.Header) (response models.Asset, err error) {
err = s.c.get(ctx, &response,
fmt.Sprintf("/v2/assets/%d", s.assetId), nil, headers)
// Do performs the HTTP request
func (s *GetAssetByID) Do(ctx context.Context, headers ...*common.Header) (response models.Asset, err error) {
err = s.c.get(ctx, &response, fmt.Sprintf("/v2/assets/%v", s.assetId), nil, headers)
return
}
26 changes: 16 additions & 10 deletions client/v2/algod/block.go → client/v2/algod/getBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ package algod
import (
"context"
"fmt"

"github.com/algorand/go-algorand-sdk/client/v2/common"
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/types"
)

type Block struct {
c *Client
round uint64
p models.GetBlockParams
// BlockParams contains all of the query parameters for url serialization.
type BlockParams struct {

// Format configures whether the response object is JSON or MessagePack encoded.
Format string `url:"format,omitempty"`
}

type generatedBlockResponse struct {
// Block header data.
Block types.Block `json:"block"`
// Block get the block for the given round.
type Block struct {
c *Client

// Optional certificate object. This is only included when the format is set to message pack.
Cert *map[string]interface{} `json:"cert,omitempty"`
round uint64

p BlockParams
}

// Do performs the HTTP request
func (s *Block) Do(ctx context.Context, headers ...*common.Header) (result types.Block, err error) {
var response models.BlockResponse

s.p.Format = "msgpack"
var response generatedBlockResponse
err = s.c.getMsgpack(ctx, &response, fmt.Sprintf("/v2/blocks/%d", s.round), s.p, headers)
if err != nil {
return
}

result = response.Block
return
}
2 changes: 2 additions & 0 deletions client/v2/algod/getGenesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/algorand/go-algorand-sdk/client/v2/common"
)

// GetGenesis returns the entire genesis file in json.
type GetGenesis struct {
c *Client
}

// Do performs the HTTP request
func (s *GetGenesis) Do(ctx context.Context, headers ...*common.Header) (response string, err error) {
err = s.c.get(ctx, &response, "/genesis", nil, headers)
return
Expand Down
5 changes: 5 additions & 0 deletions client/v2/algod/getPendingTransactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/algorand/go-algorand-sdk/types"
)

// PendingTransactionsParams contains all of the query parameters for url serialization.
type PendingTransactionsParams struct {

// Format configures whether the response object is JSON or MessagePack encoded.
Expand All @@ -18,6 +19,9 @@ type PendingTransactionsParams struct {
Max uint64 `url:"max,omitempty"`
}

// PendingTransactions get the list of pending transactions, sorted by priority, in
// decreasing order, truncated at the end at MAX. If MAX = 0, returns all pending
// transactions.
type PendingTransactions struct {
c *Client

Expand All @@ -31,6 +35,7 @@ func (s *PendingTransactions) Max(Max uint64) *PendingTransactions {
return s
}

// Do performs the HTTP request
func (s *PendingTransactions) Do(ctx context.Context, headers ...*common.Header) (total uint64, topTransactions []types.SignedTxn, err error) {
s.p.Format = "msgpack"
response := models.PendingTransactionsResponse{}
Expand Down
15 changes: 10 additions & 5 deletions client/v2/algod/getPendingTransactionsByAddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/algorand/go-algorand-sdk/types"
)

type PendingTransactionInformationByAddressParams struct {
// PendingTransactionsByAddressParams contains all of the query parameters for url serialization.
type PendingTransactionsByAddressParams struct {

// Format configures whether the response object is JSON or MessagePack encoded.
Format string `url:"format,omitempty"`
Expand All @@ -19,22 +20,26 @@ type PendingTransactionInformationByAddressParams struct {
Max uint64 `url:"max,omitempty"`
}

type PendingTransactionInformationByAddress struct {
// PendingTransactionsByAddress get the list of pending transactions by address,
// sorted by priority, in decreasing order, truncated at the end at MAX. If MAX =
// 0, returns all pending transactions.
type PendingTransactionsByAddress struct {
c *Client

address string

p PendingTransactionInformationByAddressParams
p PendingTransactionsByAddressParams
}

// Max truncated number of transactions to display. If max=0, returns all pending
// txns.
func (s *PendingTransactionInformationByAddress) Max(Max uint64) *PendingTransactionInformationByAddress {
func (s *PendingTransactionsByAddress) Max(Max uint64) *PendingTransactionsByAddress {
s.p.Max = Max
return s
}

func (s *PendingTransactionInformationByAddress) Do(ctx context.Context, headers ...*common.Header) (total uint64, topTransactions []types.SignedTxn, err error) {
// Do performs the HTTP request
func (s *PendingTransactionsByAddress) Do(ctx context.Context, headers ...*common.Header) (total uint64, topTransactions []types.SignedTxn, err error) {
s.p.Format = "msgpack"
response := models.PendingTransactionsResponse{}
err = s.c.getMsgpack(ctx, &response, fmt.Sprintf("/v2/accounts/%s/transactions/pending", s.address), s.p, headers)
Expand Down
Loading

0 comments on commit adb6d4b

Please sign in to comment.