From 3c68e8fea36fdbbc1ca3642ff53ffe9f587c34dd Mon Sep 17 00:00:00 2001 From: bahadylbekov <33404905+bahadylbekov@users.noreply.github.com> Date: Fri, 7 Aug 2020 01:48:28 +0300 Subject: [PATCH] add: filecoin getmessage method for rpc package --- rpc/filecoin.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/rpc/filecoin.go b/rpc/filecoin.go index 3b2bc22..c09fafd 100644 --- a/rpc/filecoin.go +++ b/rpc/filecoin.go @@ -2,6 +2,7 @@ package rpc import ( "bytes" + "encoding/json" "fmt" "net/http" @@ -9,21 +10,54 @@ import ( ) // client implements the `Client` interface. -type lotusClient struct { +type LotusClient struct { host string jwt jwt.Token } +type RequestBody struct { + Jsonrpc string + Method string + Params []string + ID int +} + +func NewRequestBody(method string) *RequestBody { + return &RequestBody{ + Jsonrpc: "2.0", + Method: method, + Params: []string{}, + ID: 0, + } +} + // NewClient returns a new client. func NewLotusClient(host string, token jwt.Token) Client { - return &lotusClient{ + return &LotusClient{ host: host, jwt: token, } } +func (c *LotusClient) GetMessage(r *http.Request, txHash string) (*http.Response, error) { + requestBody := NewRequestBody("Filecoin.GetMessage") + requestBody.Params = append(requestBody.Params, txHash) + body, err := json.Marshal(requestBody) + if err != nil { + return nil, fmt.Errorf("Failed to marshal request body %v", err) + } + client := http.Client{} + req, err := http.NewRequest("POST", c.host, bytes.NewBuffer(body)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authentication", "Bearer "+c.jwt.Raw) + if err != nil { + return nil, fmt.Errorf("Failed to construct lotus node rpc request %v", err) + } + return client.Do(req) +} + // HandleRequest implements the `Client` interface. -func (c *lotusClient) HandleRequest(r *http.Request, data []byte) (*http.Response, error) { +func (c *LotusClient) HandleRequest(r *http.Request, data []byte) (*http.Response, error) { client := http.Client{} req, err := http.NewRequest("POST", c.host, bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json")