-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresubmit_tx.go
More file actions
74 lines (58 loc) · 1.85 KB
/
resubmit_tx.go
File metadata and controls
74 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package venly
import (
"bytes"
"context"
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// ResubmitTXRequest fields that required for resubmit tx request.
type ResubmitTXRequest struct {
SecretType string `json:"secretType"`
TransactionHash string `json:"transactionHash"`
Pincode string `json:"pincode"`
}
// ResubmitTXResponse fields that returns from resubmit tx.
type ResubmitTXResponse struct {
Success bool `json:"success"`
Result struct {
TransactionHash string `json:"transactionHash"`
} `json:"result"`
}
// ResubmitTX resubmits transaction.
func (client *Client) ResubmitTX(ctx context.Context, accessToken string, r ResubmitTXRequest) (response ResubmitTXResponse, err error) {
jsonBody, err := json.Marshal(r)
if err != nil {
return ResubmitTXResponse{}, err
}
req, err := http.NewRequest(http.MethodPost, client.config.DefaultURL+"transactions/resubmit", bytes.NewReader(jsonBody))
if err != nil {
return ResubmitTXResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return ResubmitTXResponse{}, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
errorResp := ErrorResponse{}
if err = json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
return ResubmitTXResponse{}, err
}
if !errorResp.Success {
return ResubmitTXResponse{}, errs.New(errorResp.Errors[0].Code)
}
return ResubmitTXResponse{}, errs.New(resp.Status)
}
var resubmitTXResponse ResubmitTXResponse
if err = json.NewDecoder(resp.Body).Decode(&resubmitTXResponse); err != nil {
return ResubmitTXResponse{}, err
}
return resubmitTXResponse, nil
}