-
Notifications
You must be signed in to change notification settings - Fork 29
/
api.go
180 lines (146 loc) · 4.71 KB
/
api.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package magmasc
import (
"encoding/json"
"github.com/0chain/gosdk/zmagmacore/http"
)
// GetAllConsumers makes smart contract rest api call to magma smart contract
// GetAllConsumersRP rest point to retrieve all registered consumer.Consumer.
func GetAllConsumers() ([]Consumer, error) {
resp, err := http.MakeSCRestAPICall(Address, GetAllConsumersRP, nil)
if err != nil {
return nil, err
}
consumers := make([]Consumer, 0)
if err = json.Unmarshal(resp, &consumers); err != nil {
return nil, err
}
return consumers, err
}
// GetAllProviders makes smart contract rest api call to magma smart contract
// GetAllProvidersRP rest point to retrieve all registered provider.Provider.
func GetAllProviders() ([]Provider, error) {
resp, err := http.MakeSCRestAPICall(Address, GetAllProvidersRP, nil)
if err != nil {
return nil, err
}
providers := make([]Provider, 0)
if err = json.Unmarshal(resp, &providers); err != nil {
return nil, err
}
return providers, err
}
// RequestAcknowledgment makes smart contract rest api call to magma smart contract
// AcknowledgmentRP rest point to retrieve Acknowledgment.
func RequestAcknowledgment(sessionID string) (*Acknowledgment, error) {
params := map[string]string{
"id": sessionID,
}
blob, err := http.MakeSCRestAPICall(Address, AcknowledgmentRP, params)
if err != nil {
return nil, err
}
ackn := Acknowledgment{}
if err = json.Unmarshal(blob, &ackn); err != nil {
return nil, err
}
return &ackn, nil
}
// IsAcknowledgmentExist makes smart contract rest api call to magma smart contract
// IsAcknowledgmentExistRP rest point to ensure that Acknowledgment with provided session ID exist in the blockchain.
func IsAcknowledgmentExist(sessionID string) (bool, error) {
params := map[string]string{
"id": sessionID,
}
blob, err := http.MakeSCRestAPICall(Address, IsAcknowledgmentExistRP, params)
if err != nil {
return false, err
}
var exist bool
if err = json.Unmarshal(blob, &exist); err != nil {
return false, err
}
return exist, nil
}
// VerifyAcknowledgmentAccepted makes smart contract rest api call to magma smart contract
// VerifyAcknowledgmentAcceptedRP rest point to ensure that Acknowledgment with provided IDs was accepted.
func VerifyAcknowledgmentAccepted(sessionID, accessPointID, consumerExtID, providerExtID string) (*Acknowledgment, error) {
params := map[string]string{
"session_id": sessionID,
"access_point_id": accessPointID,
"provider_ext_id": providerExtID,
"consumer_ext_id": consumerExtID,
}
blob, err := http.MakeSCRestAPICall(Address, VerifyAcknowledgmentAcceptedRP, params)
if err != nil {
return nil, err
}
ackn := Acknowledgment{}
if err = json.Unmarshal(blob, &ackn); err != nil {
return nil, err
}
return &ackn, nil
}
// ConsumerFetch makes smart contract rest api call to magma smart contract
// ConsumerFetchRP rest point to fetch Consumer info.
func ConsumerFetch(id string) (*Consumer, error) {
params := map[string]string{
"ext_id": id,
}
blob, err := http.MakeSCRestAPICall(Address, ConsumerFetchRP, params)
if err != nil {
return nil, err
}
cons := Consumer{}
if err = json.Unmarshal(blob, &cons); err != nil {
return nil, err
}
return &cons, nil
}
// ProviderFetch makes smart contract rest api call to magma smart contract
// ProviderFetchRP rest point to fetch Provider info.
func ProviderFetch(id string) (*Provider, error) {
params := map[string]string{
"ext_id": id,
}
blob, err := http.MakeSCRestAPICall(Address, ProviderFetchRP, params)
if err != nil {
return nil, err
}
prov := Provider{}
if err = json.Unmarshal(blob, &prov); err != nil {
return nil, err
}
return &prov, nil
}
// IsConsumerRegisteredRP makes smart contract rest api call to magma smart contract
// ConsumerRegisteredRP rest point to check registration of the consumer with provided external ID.
func IsConsumerRegisteredRP(extID string) (bool, error) {
params := map[string]string{
"ext_id": extID,
}
registeredByt, err := http.MakeSCRestAPICall(Address, ConsumerRegisteredRP, params)
if err != nil {
return false, err
}
var registered bool
if err := json.Unmarshal(registeredByt, ®istered); err != nil {
return false, err
}
return registered, nil
}
// IsProviderRegisteredRP makes smart contract rest api call to magma smart contract
// ProviderRegisteredRP rest point to check registration of the provider with provided external ID.
func IsProviderRegisteredRP(extID string) (bool, error) {
params := map[string]string{
"ext_id": extID,
}
registeredByt, err := http.MakeSCRestAPICall(Address, ProviderRegisteredRP, params)
if err != nil {
return false, err
}
var registered bool
if err := json.Unmarshal(registeredByt, ®istered); err != nil {
return false, err
}
return registered, nil
}