Skip to content

Commit 06f03cc

Browse files
Merge pull request #20 from everFinance/upgrade-client
feat(): fix client api return err
2 parents 277bb08 + aeb6355 commit 06f03cc

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

client.go

+58-32
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/everFinance/goar/utils"
1919
)
2020

21+
// arweave HTTP API: https://docs.arweave.org/developers/server/http-api
22+
2123
type Client struct {
2224
client *http.Client
2325
url string
@@ -43,7 +45,7 @@ func NewClient(nodeUrl string, proxyUrl ...string) *Client {
4345
func (c *Client) GetInfo() (info *types.NetworkInfo, err error) {
4446
body, _, err := c.httpGet("info")
4547
if err != nil {
46-
return
48+
return nil, ErrBadGateway
4749
}
4850

4951
info = &types.NetworkInfo{}
@@ -55,46 +57,64 @@ func (c *Client) GetInfo() (info *types.NetworkInfo, err error) {
5557
func (c *Client) GetTransactionByID(id string) (tx *types.Transaction, err error) {
5658
body, statusCode, err := c.httpGet(fmt.Sprintf("tx/%s", id))
5759
if err != nil {
58-
return
60+
return nil, ErrBadGateway
5961
}
6062

61-
if statusCode != 200 {
62-
err = errors.New(string(body))
63+
switch statusCode {
64+
case 200:
65+
// json unmarshal
66+
tx = &types.Transaction{}
67+
err = json.Unmarshal(body, tx)
6368
return
69+
case 202:
70+
return nil, ErrPendingTx
71+
case 400:
72+
return nil, ErrInvalidId
73+
case 404:
74+
return nil, ErrNotFound
75+
default:
76+
return nil, ErrBadGateway
6477
}
65-
66-
// json unmarshal
67-
tx = &types.Transaction{}
68-
err = json.Unmarshal(body, tx)
69-
return
7078
}
7179

7280
// GetTransactionStatus
7381
func (c *Client) GetTransactionStatus(id string) (*types.TxStatus, error) {
7482
body, code, err := c.httpGet(fmt.Sprintf("tx/%s/status", id))
7583
if err != nil {
76-
return nil, err
77-
}
78-
if code != 200 {
79-
return nil, errors.New(string(body))
84+
return nil, ErrBadGateway
8085
}
8186

82-
// json unmarshal
83-
txStatus := &types.TxStatus{}
84-
err = json.Unmarshal(body, txStatus)
85-
return txStatus, err
87+
switch code {
88+
case 200:
89+
// json unmarshal
90+
txStatus := &types.TxStatus{}
91+
err = json.Unmarshal(body, txStatus)
92+
return txStatus, err
93+
case 404:
94+
return nil, ErrNotFound
95+
default:
96+
return nil, ErrBadGateway
97+
}
8698
}
8799

88-
func (c *Client) GetTransactionField(id string, field string) (f string, err error) {
89-
url := fmt.Sprintf("tx/%v/%v", id, field)
90-
91-
body, statusCode, err := c.httpGet(url)
92-
if statusCode != 200 {
93-
err = fmt.Errorf("not found data")
100+
func (c *Client) GetTransactionField(id string, field string) (string, error) {
101+
body, statusCode, err := c.httpGet(fmt.Sprintf("tx/%v/%v", id, field))
102+
if err != nil {
103+
return "", ErrBadGateway
94104
}
95105

96-
f = string(body)
97-
return
106+
switch statusCode {
107+
case 200:
108+
return string(body), nil
109+
case 202:
110+
return "", ErrPendingTx
111+
case 400:
112+
return "", ErrInvalidId
113+
case 404:
114+
return "", ErrNotFound
115+
default:
116+
return "", ErrBadGateway
117+
}
98118
}
99119

100120
func (c *Client) GetTransactionTags(id string) ([]types.Tag, error) {
@@ -115,19 +135,25 @@ func (c *Client) GetTransactionTags(id string) ([]types.Tag, error) {
115135
}
116136

117137
func (c *Client) GetTransactionData(id string, extension ...string) (body []byte, err error) {
118-
url := fmt.Sprintf("tx/%v/%v", id, "data")
138+
urlPath := fmt.Sprintf("tx/%v/%v", id, "data")
119139
if extension != nil {
120-
url = url + "." + extension[0]
140+
urlPath = urlPath + "." + extension[0]
121141
}
122-
body, statusCode, err := c.httpGet(url)
142+
body, statusCode, err := c.httpGet(urlPath)
123143

144+
// When data is bigger than 12MiB statusCode == 400 NOTE: Data bigger than that has to be downloaded chunk by chunk.
124145
if statusCode == 400 || len(body) == 0 {
125146
body, err = c.DownloadChunkData(id)
126-
} else if statusCode != 200 {
127-
err = fmt.Errorf("not found data")
147+
return
148+
} else if statusCode == 200 {
149+
return body, nil
150+
} else if statusCode == 202 {
151+
return nil, ErrPendingTx
152+
} else if statusCode == 404 {
153+
return nil, ErrNotFound
154+
} else {
155+
return nil, ErrBadGateway
128156
}
129-
130-
return
131157
}
132158

133159
func (c *Client) GetTransactionPrice(data []byte, target *string) (reward int64, err error) {

error.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package goar
2+
3+
import "errors"
4+
5+
var (
6+
ErrNotFound = errors.New("Not Found")
7+
ErrPendingTx = errors.New("Pending")
8+
ErrInvalidId = errors.New("Invalid ArId")
9+
ErrBadGateway = errors.New("Bad Gateway")
10+
)

0 commit comments

Comments
 (0)