@@ -18,6 +18,8 @@ import (
18
18
"github.com/everFinance/goar/utils"
19
19
)
20
20
21
+ // arweave HTTP API: https://docs.arweave.org/developers/server/http-api
22
+
21
23
type Client struct {
22
24
client * http.Client
23
25
url string
@@ -43,7 +45,7 @@ func NewClient(nodeUrl string, proxyUrl ...string) *Client {
43
45
func (c * Client ) GetInfo () (info * types.NetworkInfo , err error ) {
44
46
body , _ , err := c .httpGet ("info" )
45
47
if err != nil {
46
- return
48
+ return nil , ErrBadGateway
47
49
}
48
50
49
51
info = & types.NetworkInfo {}
@@ -55,46 +57,64 @@ func (c *Client) GetInfo() (info *types.NetworkInfo, err error) {
55
57
func (c * Client ) GetTransactionByID (id string ) (tx * types.Transaction , err error ) {
56
58
body , statusCode , err := c .httpGet (fmt .Sprintf ("tx/%s" , id ))
57
59
if err != nil {
58
- return
60
+ return nil , ErrBadGateway
59
61
}
60
62
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 )
63
68
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
64
77
}
65
-
66
- // json unmarshal
67
- tx = & types.Transaction {}
68
- err = json .Unmarshal (body , tx )
69
- return
70
78
}
71
79
72
80
// GetTransactionStatus
73
81
func (c * Client ) GetTransactionStatus (id string ) (* types.TxStatus , error ) {
74
82
body , code , err := c .httpGet (fmt .Sprintf ("tx/%s/status" , id ))
75
83
if err != nil {
76
- return nil , err
77
- }
78
- if code != 200 {
79
- return nil , errors .New (string (body ))
84
+ return nil , ErrBadGateway
80
85
}
81
86
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
+ }
86
98
}
87
99
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
94
104
}
95
105
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
+ }
98
118
}
99
119
100
120
func (c * Client ) GetTransactionTags (id string ) ([]types.Tag , error ) {
@@ -115,19 +135,25 @@ func (c *Client) GetTransactionTags(id string) ([]types.Tag, error) {
115
135
}
116
136
117
137
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" )
119
139
if extension != nil {
120
- url = url + "." + extension [0 ]
140
+ urlPath = urlPath + "." + extension [0 ]
121
141
}
122
- body , statusCode , err := c .httpGet (url )
142
+ body , statusCode , err := c .httpGet (urlPath )
123
143
144
+ // When data is bigger than 12MiB statusCode == 400 NOTE: Data bigger than that has to be downloaded chunk by chunk.
124
145
if statusCode == 400 || len (body ) == 0 {
125
146
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
128
156
}
129
-
130
- return
131
157
}
132
158
133
159
func (c * Client ) GetTransactionPrice (data []byte , target * string ) (reward int64 , err error ) {
0 commit comments