2021-09-13 03:25:47 +00:00
|
|
|
package heliumapi
|
|
|
|
|
|
|
|
import (
|
2021-09-25 01:30:22 +00:00
|
|
|
"encoding/json"
|
2021-09-13 03:25:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
apiUrl = "https://api.helium.io"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = &http.Client{}
|
|
|
|
)
|
|
|
|
|
2021-09-25 01:30:22 +00:00
|
|
|
func createGetRequest(path string, params map[string]string) (*http.Request, error) {
|
2021-09-13 03:25:47 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", apiUrl, path), nil)
|
|
|
|
|
|
|
|
// setup headers
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
// setup query param if there are any
|
|
|
|
if params != nil {
|
|
|
|
query := req.URL.Query()
|
2021-09-25 01:30:22 +00:00
|
|
|
for k, v := range params {
|
2021-09-13 03:25:47 +00:00
|
|
|
query.Add(k, v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.URL.RawQuery = query.Encode()
|
|
|
|
}
|
|
|
|
|
2021-09-25 01:30:22 +00:00
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHeliumApi(path string, params *map[string]string) ([]byte, error) {
|
|
|
|
// if params is nil, set it to an empty map
|
|
|
|
if params == nil {
|
|
|
|
params = &map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := createGetRequest(path, *params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
2021-09-13 03:25:47 +00:00
|
|
|
// query the api
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// read the response body
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response body of path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2021-09-25 01:30:22 +00:00
|
|
|
func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, error) {
|
|
|
|
// if params is nil, set it to an empty map
|
|
|
|
if params == nil {
|
|
|
|
params = &map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseWithCursor struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
}
|
|
|
|
|
|
|
|
res := [][]byte{}
|
|
|
|
respCursor := ResponseWithCursor{}
|
|
|
|
req, err := createGetRequest(path, *params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
// query the api
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// read the response body and add it to the result array
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response body of path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
res = append(res, body)
|
|
|
|
|
|
|
|
// parse the response body for a cursor
|
|
|
|
respCursor.Cursor = ""
|
|
|
|
err = json.Unmarshal(body, &respCursor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue querying until there is no longer a cursor
|
|
|
|
if respCursor.Cursor != "" {
|
|
|
|
params = &map[string]string{"cursor": respCursor.Cursor}
|
|
|
|
req, err = createGetRequest(path, *params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2021-09-13 03:25:47 +00:00
|
|
|
}
|