This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
104 lines (86 loc) · 2.23 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
package o3
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)
const apiEndpoint = "https://platform.o3.network/api"
type NEONetWork string
var NEOMainNet = "main"
var NEOTestNet = "test"
type O3APIInterface interface {
GetNEOUTXO(address string) UTXOResponse
GetNEOClimableGAS(address string) ClaimableGASResponse
}
type O3Client struct {
APIBaseEndpoint url.URL
neoNetwork string
}
func DefaultO3APIClient() *O3Client {
u, err := url.Parse(apiEndpoint)
if err != nil {
return nil
}
return &O3Client{APIBaseEndpoint: *u}
}
func APIClientWithNEOTestnet() *O3Client {
u, err := url.Parse(apiEndpoint)
if err != nil {
return nil
}
return &O3Client{APIBaseEndpoint: *u, neoNetwork: "test"}
}
func APIClientWithNEOPrivateNet() *O3Client {
u, err := url.Parse(apiEndpoint)
if err != nil {
return nil
}
return &O3Client{APIBaseEndpoint: *u, neoNetwork: "private"}
}
//make sure all method interface is implemented
var _ O3APIInterface = (*O3Client)(nil)
func (n *O3Client) makeGETRequest(endpoint string, out interface{}) error {
fullEndpointString := fmt.Sprintf("%v%v", n.APIBaseEndpoint.String(), endpoint)
fullEndpoint, _ := url.Parse(fullEndpointString)
if n.neoNetwork == "test" || n.neoNetwork == "private" {
q := fullEndpoint.Query()
q.Set("network", n.neoNetwork)
fullEndpoint.RawQuery = q.Encode()
}
log.Printf("%v", fullEndpoint.String())
req, err := http.NewRequest("GET", fullEndpoint.String(), nil)
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
req.Header.Set("Connection", "close")
req.Close = true
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&out)
if err != nil {
return err
}
return nil
}
func (o *O3Client) GetNEOUTXO(address string) UTXOResponse {
response := UTXOResponse{}
err := o.makeGETRequest(fmt.Sprintf("/v1/neo/%v/utxo", address), &response)
if err != nil {
return response
}
return response
}
func (o *O3Client) GetNEOClimableGAS(address string) ClaimableGASResponse {
response := ClaimableGASResponse{}
err := o.makeGETRequest(fmt.Sprintf("/v1/neo/%v/claimablegas", address), &response)
if err != nil {
return response
}
return response
}