Skip to content

Commit

Permalink
Merge branch 'release/v1.5.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Oct 22, 2020
2 parents 225dad1 + 37ab833 commit d2744e8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.5.1
# Added
- Add `BlockRaw` method to algod API V2 client.
# 1.5.0
# Added
- Support for Applications
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func main() {
if wallet.Name == "testwallet" {
fmt.Printf("found wallet '%s' with ID: %s\n", wallet.Name, wallet.ID)
exampleWalletID = wallet.ID
break
}
}
// Get a wallet handle
Expand Down
21 changes: 15 additions & 6 deletions client/v2/algod/algod.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ const algodAuthHeader = "X-Algo-API-Token"
type Client common.Client

// get performs a GET request to the specific path against the server, assumes JSON response
func (c *Client) get(ctx context.Context, response interface{}, path string, request interface{}, headers []*common.Header) error {
return (*common.Client)(c).Get(ctx, response, path, request, headers)
func (c *Client) get(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).Get(ctx, response, path, body, headers)
}

// getMsgpack performs a GET request to the specific path against the server, assumes msgpack response
func (c *Client) getMsgpack(ctx context.Context, response interface{}, path string, request interface{}, headers []*common.Header) error {
return (*common.Client)(c).GetRawMsgpack(ctx, response, path, request, headers)
func (c *Client) getMsgpack(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).GetRawMsgpack(ctx, response, path, body, headers)
}

// getMsgpack performs a GET request to the specific path against the server, assumes msgpack response
func (c *Client) getRaw(ctx context.Context, path string, body interface{}, headers []*common.Header) ([]byte, error) {
return (*common.Client)(c).GetRaw(ctx, path, body, headers)
}

// post sends a POST request to the given path with the given request object.
// No query parameters will be sent if request is nil.
// response must be a pointer to an object as post writes the response there.
func (c *Client) post(ctx context.Context, response interface{}, path string, request interface{}, headers []*common.Header) error {
return (*common.Client)(c).Post(ctx, response, path, request, headers)
func (c *Client) post(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).Post(ctx, response, path, body, headers)
}

// MakeClient is the factory for constructing a ClientV2 for a given endpoint.
Expand All @@ -42,6 +47,10 @@ func (c *Client) Block(round uint64) *Block {
return &Block{c: c, round: round}
}

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

func (c *Client) HealthCheck() *HealthCheck {
return &HealthCheck{c: c}
}
Expand Down
22 changes: 22 additions & 0 deletions client/v2/algod/blockRaw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package algod

import (
"context"
"fmt"

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

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

// Do executes the BlockRaw query and gets the results.
func (s *BlockRaw) Do(ctx context.Context, headers ...*common.Header) (result []byte, err error) {
s.p.Format = "msgpack"
return s.c.getRaw(ctx, fmt.Sprintf("/v2/blocks/%d", s.round), s.p, headers)
}
56 changes: 35 additions & 21 deletions client/v2/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,35 @@ func mergeRawQueries(q1, q2 string) string {
return q1 + "&" + q2
}

// submitForm is a helper used for submitting (ex.) GETs and POSTs to the server
func (client *Client) submitFormRaw(ctx context.Context, path string, request interface{}, requestMethod string, encodeJSON bool, headers []*Header) (resp *http.Response, err error) {
// submitFormRaw is a helper used for submitting (ex.) GETs and POSTs to the server
func (client *Client) submitFormRaw(ctx context.Context, path string, body interface{}, requestMethod string, encodeJSON bool, headers []*Header) (resp *http.Response, err error) {
queryURL := client.serverURL
queryURL.Path += path

var req *http.Request
var body io.Reader
if request != nil {
var bodyReader io.Reader
if body != nil {
if requestMethod == "POST" && rawRequestPaths[path] {
reqBytes, ok := request.([]byte)
reqBytes, ok := body.([]byte)
if !ok {
return nil, fmt.Errorf("couldn't decode raw request as bytes")
return nil, fmt.Errorf("couldn't decode raw body as bytes")
}
body = bytes.NewBuffer(reqBytes)
bodyReader = bytes.NewBuffer(reqBytes)
} else {
v, err := query.Values(request)
v, err := query.Values(body)
if err != nil {
return nil, err
}

queryURL.RawQuery = mergeRawQueries(queryURL.RawQuery, v.Encode())
if encodeJSON {
jsonValue, _ := json.Marshal(request)
body = bytes.NewBuffer(jsonValue)
jsonValue, _ := json.Marshal(body)
bodyReader = bytes.NewBuffer(jsonValue)
}
}
}

req, err = http.NewRequest(requestMethod, queryURL.String(), body)
req, err = http.NewRequest(requestMethod, queryURL.String(), bodyReader)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,8 +166,8 @@ func (client *Client) submitFormRaw(ctx context.Context, path string, request in
return resp, nil
}

func (client *Client) submitForm(ctx context.Context, response interface{}, path string, request interface{}, requestMethod string, encodeJSON bool, headers []*Header) error {
resp, err := client.submitFormRaw(ctx, path, request, requestMethod, encodeJSON, headers)
func (client *Client) submitForm(ctx context.Context, response interface{}, path string, body interface{}, requestMethod string, encodeJSON bool, headers []*Header) error {
resp, err := client.submitFormRaw(ctx, path, body, requestMethod, encodeJSON, headers)
if err != nil {
return err
}
Expand All @@ -178,24 +178,38 @@ func (client *Client) submitForm(ctx context.Context, response interface{}, path
}

// Get performs a GET request to the specific path against the server
func (client *Client) Get(ctx context.Context, response interface{}, path string, request interface{}, headers []*Header) error {
return client.submitForm(ctx, response, path, request, "GET", false /* encodeJSON */, headers)
func (client *Client) Get(ctx context.Context, response interface{}, path string, body interface{}, headers []*Header) error {
return client.submitForm(ctx, response, path, body, "GET", false /* encodeJSON */, headers)
}

func (client *Client) GetRawMsgpack(ctx context.Context, response interface{}, path string, request interface{}, headers []*Header) error {
resp, err := client.submitFormRaw(ctx, path, request, "GET", false /* encodeJSON */, headers)
// GetRaw performs a GET request to the specific path against the server and returns the raw body bytes.
func (client *Client) GetRaw(ctx context.Context, path string, body interface{}, headers []*Header) (response []byte, err error) {
var resp *http.Response
resp, err = client.submitFormRaw(ctx, path, body, "GET", false /* encodeJSON */, headers)
if err != nil {
return nil, err
}

defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

// GetRawMsgpack performs a GET request to the specific path against the server and returns the decoded messagepack response.
func (client *Client) GetRawMsgpack(ctx context.Context, response interface{}, path string, body interface{}, headers []*Header) error {
resp, err := client.submitFormRaw(ctx, path, body, "GET", false /* encodeJSON */, headers)
if err != nil {
return err
}

defer resp.Body.Close()

dec := msgpack.NewDecoder(resp.Body)
return dec.Decode(&response)
}

// Post sends a POST request to the given path with the given request object.
// No query parameters will be sent if request is nil.
// Post sends a POST request to the given path with the given body object.
// No query parameters will be sent if body is nil.
// response must be a pointer to an object as post writes the response there.
func (client *Client) Post(ctx context.Context, response interface{}, path string, request interface{}, headers []*Header) error {
return client.submitForm(ctx, response, path, request, "POST", true /* encodeJSON */, headers)
func (client *Client) Post(ctx context.Context, response interface{}, path string, body interface{}, headers []*Header) error {
return client.submitForm(ctx, response, path, body, "POST", true /* encodeJSON */, headers)
}
4 changes: 2 additions & 2 deletions client/v2/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const indexerAuthHeader = "X-Indexer-API-Token"
type Client common.Client

// get performs a GET request to the specific path against the server
func (c *Client) get(ctx context.Context, response interface{}, path string, request interface{}, headers []*common.Header) error {
return (*common.Client)(c).Get(ctx, response, path, request, headers)
func (c *Client) get(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).Get(ctx, response, path, body, headers)
}

// MakeClient is the factory for constructing an IndexerClient for a given endpoint.
Expand Down

0 comments on commit d2744e8

Please sign in to comment.