Skip to content

Commit

Permalink
refactor: lib client api #9
Browse files Browse the repository at this point in the history
  • Loading branch information
mkungla authored Mar 15, 2022
2 parents db8dbb1 + e679845 commit f542a7e
Show file tree
Hide file tree
Showing 16 changed files with 594 additions and 281 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import (
- [Revert](#revert)
- [Type](#type)
- [Scope](#scope)
- [| **markdown** | Markdown files |](#-markdown--markdown-files-)
- [Subject](#subject)
- [Body](#body)
- [Footer](#footer)
Expand Down Expand Up @@ -429,6 +428,7 @@ Samples:
```
docs(markdown): update readme examples
```
```
Expand Down Expand Up @@ -482,6 +482,7 @@ The following is the list of supported scopes:
| **endpoint** | Changes related to api endpoints |
| **godoc** | Go documentation |
| **markdown** | Markdown files |

---

#### Subject
Expand Down
47 changes: 29 additions & 18 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ type (
// GetAccountList returns a list of all accounts.
func (c *Client) GetAccountList(ctx context.Context) (res *AccountListResponse, err error) {
res = &AccountListResponse{}
rsp, _ := c.request(ctx, &res.Response, "GET", "/account_list", nil, nil, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/account_list", nil, nil, nil)

if err != nil {
return
}

accs := []struct {
ID StakeAddress `json:"id"`
Expand Down Expand Up @@ -145,15 +149,17 @@ func (c *Client) GetAccountInfo(ctx context.Context, addr Address) (res *Account
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_info", nil, params, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/account_info", nil, params, nil)
if err != nil {
return
}

addrs := []AccountInfo{}
err = readAndUnmarshalResponse(rsp, &res.Response, &addrs)

if len(addrs) == 1 {
res.Data = &addrs[0]
}
res.ready()
return
}

Expand All @@ -171,10 +177,11 @@ func (c *Client) GetAccountRewards(
params.Set("_epoch_no", fmt.Sprint(*epoch))
}

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_rewards", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/account_rewards", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)
res.ready()
return
}

Expand All @@ -188,10 +195,11 @@ func (c *Client) GetAccountUpdates(
params := url.Values{}
params.Set("_stake_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_updates", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/account_updates", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)
res.ready()
return
}

Expand All @@ -204,8 +212,10 @@ func (c *Client) GetAccountAddresses(
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_addresses", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/account_addresses", nil, params, nil)
if err != nil {
return
}
addrs := []struct {
Addr Address `json:"address"`
}{}
Expand All @@ -217,7 +227,6 @@ func (c *Client) GetAccountAddresses(
res.Data = append(res.Data, a.Addr)
}
}
res.ready()
return
}

Expand All @@ -230,10 +239,11 @@ func (c *Client) GetAccountAssets(
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_assets", nil, params, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/account_assets", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)

res.ready()
return
}

Expand All @@ -246,9 +256,10 @@ func (c *Client) GetAccountHistory(
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/account_history", nil, params, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/account_history", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)

res.ready()
return
}
40 changes: 22 additions & 18 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,27 @@ func (c *Client) GetAddressInfo(ctx context.Context, addr Address) (res *Address
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/address_info", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/address_info", nil, params, nil)
if err != nil {
return
}
addrs := []AddressInfo{}
err = readAndUnmarshalResponse(rsp, &res.Response, &addrs)
if len(addrs) == 1 {
res.Data = &addrs[0]
}
res.ready()
return
}

// GetAddressTxs returns the transaction hash list of input address array,
// optionally filtering after specified block height (inclusive).
//nolint: dupl
func (c *Client) GetAddressTxs(ctx context.Context, addrs []Address, h uint64) (res *AddressTxsResponse, err error) {
res = &AddressTxsResponse{}

func (c *Client) GetAddressTxs(ctx context.Context, addrs []Address, h uint64) (*AddressTxsResponse, error) {
res := &AddressTxsResponse{}
if len(addrs) == 0 {
err = ErrNoAddress
err := ErrNoAddress
res.applyError(nil, err)
return
return res, err
}

var payload = struct {
Expand All @@ -136,8 +137,10 @@ func (c *Client) GetAddressTxs(ctx context.Context, addrs []Address, h uint64) (
defer w.Close()
}()

rsp, _ := c.request(ctx, &res.Response, "POST", "/address_txs", rpipe, nil, nil)

rsp, err := c.request(ctx, &res.Response, "POST", "/address_txs", rpipe, nil, nil)
if err != nil {
return res, err
}
atxs := []struct {
Hash TxHash `json:"tx_hash"`
}{}
Expand All @@ -149,7 +152,6 @@ func (c *Client) GetAddressTxs(ctx context.Context, addrs []Address, h uint64) (
res.Data = append(res.Data, tx.Hash)
}
}
res.ready()
return res, err
}

Expand All @@ -165,16 +167,17 @@ func (c *Client) GetAddressAssets(ctx context.Context, addr Address) (res *Addre
params := url.Values{}
params.Set("_address", string(addr))

rsp, _ := c.request(ctx, &res.Response, "GET", "/address_assets", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/address_assets", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)
res.ready()
return
}

// GetCredentialTxs returns the transaction hash list of input
// payment credential array, optionally filtering after specified block height (inclusive).
//nolint: dupl

func (c *Client) GetCredentialTxs(
ctx context.Context,
creds []PaymentCredential,
Expand All @@ -201,8 +204,10 @@ func (c *Client) GetCredentialTxs(
defer w.Close()
}()

rsp, _ := c.request(ctx, &res.Response, "POST", "/credential_txs", rpipe, nil, nil)

rsp, err := c.request(ctx, &res.Response, "POST", "/credential_txs", rpipe, nil, nil)
if err != nil {
return
}
atxs := []struct {
Hash TxHash `json:"tx_hash"`
}{}
Expand All @@ -214,6 +219,5 @@ func (c *Client) GetCredentialTxs(
res.Data = append(res.Data, tx.Hash)
}
}
res.ready()
return res, err
}
32 changes: 21 additions & 11 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ type (
// GetAssetList returns the list of all native assets (paginated).
func (c *Client) GetAssetList(ctx context.Context) (res *AssetListResponse, err error) {
res = &AssetListResponse{}
rsp, _ := c.request(ctx, &res.Response, "GET", "/asset_list", nil, nil, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/asset_list", nil, nil, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
Expand All @@ -173,8 +176,12 @@ func (c *Client) GetAssetAddressList(
params.Set("_asset_policy", string(policy))
params.Set("_asset_name", string(name))

rsp, _ := c.request(ctx, &res.Response, "GET", "/asset_address_list", nil, params, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/asset_address_list", nil, params, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)

return
}

Expand All @@ -192,15 +199,16 @@ func (c *Client) GetAssetInfo(
params.Set("_asset_policy", string(policy))
params.Set("_asset_name", string(name))

rsp, _ := c.request(ctx, &res.Response, "GET", "/asset_info", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/asset_info", nil, params, nil)
if err != nil {
return
}
info := []AssetInfo{}
err = readAndUnmarshalResponse(rsp, &res.Response, &info)

if len(info) == 1 {
res.Data = &info[0]
}
res.ready()
return
}

Expand All @@ -219,15 +227,16 @@ func (c *Client) GetAssetSummary(
params.Set("_asset_policy", string(policy))
params.Set("_asset_name", string(name))

rsp, _ := c.request(ctx, &res.Response, "GET", "/asset_summary", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/asset_summary", nil, params, nil)
if err != nil {
return
}
summary := []AssetSummary{}
err = readAndUnmarshalResponse(rsp, &res.Response, &summary)

if len(summary) == 1 {
res.Data = &summary[0]
}
res.ready()
return
}

Expand All @@ -244,14 +253,15 @@ func (c *Client) GetAssetTxs(
params.Set("_asset_policy", string(policy))
params.Set("_asset_name", string(name))

rsp, _ := c.request(ctx, &res.Response, "GET", "/asset_txs", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/asset_txs", nil, params, nil)
if err != nil {
return
}
atxs := []AssetTxs{}
err = readAndUnmarshalResponse(rsp, &res.Response, &atxs)

if len(atxs) == 1 {
res.Data = &atxs[0]
}
res.ready()
return
}
20 changes: 12 additions & 8 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ type (
// GetBlocks returns summarised details about all blocks (paginated - latest first).
func (c *Client) GetBlocks(ctx context.Context) (res *BlocksResponse, err error) {
res = &BlocksResponse{}
rsp, _ := c.request(ctx, &res.Response, "GET", "/blocks", nil, nil, nil)
rsp, err := c.request(ctx, &res.Response, "GET", "/blocks", nil, nil, nil)
if err != nil {
return
}
err = readAndUnmarshalResponse(rsp, &res.Response, &res.Data)
res.ready()
return
}

Expand All @@ -99,15 +101,16 @@ func (c *Client) GetBlockInfo(ctx context.Context, hash BlockHash) (res *BlockIn
params := url.Values{}
params.Set("_block_hash", string(hash))

rsp, _ := c.request(ctx, &res.Response, "GET", "/block_info", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/block_info", nil, params, nil)
if err != nil {
return
}
blockpl := []Block{}
err = readAndUnmarshalResponse(rsp, &res.Response, &blockpl)

if len(blockpl) == 1 {
res.Data = &blockpl[0]
}
res.ready()
return
}

Expand All @@ -118,8 +121,10 @@ func (c *Client) GetBlockTxHashes(ctx context.Context, hash BlockHash) (res *Blo
params := url.Values{}
params.Set("_block_hash", string(hash))

rsp, _ := c.request(ctx, &res.Response, "GET", "/block_txs", nil, params, nil)

rsp, err := c.request(ctx, &res.Response, "GET", "/block_txs", nil, params, nil)
if err != nil {
return
}
blockTxs := []struct {
Hash TxHash `json:"tx_hash"`
}{}
Expand All @@ -130,6 +135,5 @@ func (c *Client) GetBlockTxHashes(ctx context.Context, hash BlockHash) (res *Blo
res.Data = append(res.Data, tx.Hash)
}
}
res.ready()
return
}
Loading

0 comments on commit f542a7e

Please sign in to comment.