-
Notifications
You must be signed in to change notification settings - Fork 0
/
hlf_proxy.go
124 lines (106 loc) · 3.18 KB
/
hlf_proxy.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// RequestPrx struct for request to hlf proxy
type RequestPrx struct {
Args [][]byte `json:"args"`
ChaincodeID string `json:"chaincodeId"`
Fcn string `json:"fcn"`
Opts *Options `json:"options,omitempty"`
}
// Options that can be used to specify target endpoints for the transaction invocation.
type Options struct {
TargetEndpoints []string `json:"targetEndpoints"`
}
// ResponsePrx struct for response from hlf proxy
type ResponsePrx struct {
BlockNumber int64 `json:"blockNumber,omitempty"`
ChaincodeStatus int64 `json:"chaincodeStatus,omitempty"`
Payload []byte `json:"payload,omitempty"`
TransactionID string `json:"transactionId,omitempty"`
TxValidationCode int64 `json:"txValidationCode,omitempty"`
}
// ResponseErrorPrx struct for response error from hlf proxy
type ResponseErrorPrx struct {
Code int64 `json:"code"`
Message string `json:"message"`
}
// Invoke ...
func Invoke(ctx context.Context, url, token, cc, fcn string, endpoints []string, args ...string) (*ResponsePrx, error) {
newCtx, cancel := context.WithTimeout(ctx, InvokeTimeout)
defer cancel()
return doRequest(newCtx, url, token, "invoke", cc, fcn, endpoints, args...)
}
// Query ...
func Query(ctx context.Context, url, token, cc, fcn string, endpoints []string, args ...string) (*ResponsePrx, error) {
newCtx, cancel := context.WithTimeout(ctx, QueryTimeout)
defer cancel()
return doRequest(newCtx, url, token, "query", cc, fcn, endpoints, args...)
}
//nolint:funlen
func doRequest(
ctx context.Context,
url string,
token, reqType, cc, fcn string,
endpoints []string,
args ...string,
) (*ResponsePrx, error) {
requestData := RequestPrx{
Args: AsBytes(args...),
ChaincodeID: cc,
Fcn: fcn,
}
if len(endpoints) != 0 {
requestData = RequestPrx{
Args: AsBytes(args...),
ChaincodeID: cc,
Fcn: fcn,
Opts: &Options{
TargetEndpoints: endpoints,
},
}
}
reqBody, err := json.Marshal(requestData)
if err != nil {
return nil, fmt.Errorf("json unmarshal: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/%s", url, reqType), bytes.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("http new request: %w", err)
}
req.Header.Add("authorization", fmt.Sprintf("Basic %s", token))
req.Header.Add("content-type", "application/json")
httpResponse, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("http client do: %w", err)
}
defer func() {
clErr := httpResponse.Body.Close()
if clErr != nil {
fmt.Printf("body close error: %v\n", err)
}
}()
body, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
if httpResponse.StatusCode != http.StatusOK {
responseError := &ResponseErrorPrx{}
err = json.Unmarshal(body, responseError)
if err != nil {
return nil, fmt.Errorf("json unmarshal: %w", err)
}
return nil, fmt.Errorf(responseError.Message)
}
var resp ResponsePrx
if err = json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("json unmarshal: %w", err)
}
return &resp, nil
}