1
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
helium-blockchain-exporter/heliumapi/accounts.go

105 lines
2.8 KiB
Go
Raw Normal View History

2021-09-14 03:51:47 +00:00
package heliumapi
import (
"encoding/json"
"fmt"
2021-10-03 22:02:25 +00:00
"strings"
"time"
2021-09-14 03:51:47 +00:00
2021-09-25 01:30:22 +00:00
"github.com/helium-blockchain-exporter/heliumapi/activity"
)
2021-09-15 05:04:32 +00:00
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#account-for-address
func GetAccountForAddress(account string) (*Account, error) {
2021-09-14 03:51:47 +00:00
path := "/v1/accounts/" + account
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := AccountResp{}
2021-09-14 03:51:47 +00:00
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-14 03:51:47 +00:00
}
return &respobject.Data, nil
2021-09-14 03:51:47 +00:00
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#activity-for-account
2021-09-25 01:30:22 +00:00
func GetActivityForAccount(account string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) {
path := "/v1/accounts/" + account + "/activity"
2021-10-12 13:19:58 +00:00
params := map[string]string{}
if filterTypes != nil {
2021-10-14 19:24:06 +00:00
params["filter_types"] = strings.Join(filterTypes, ",")
2021-10-03 22:02:25 +00:00
}
2021-09-25 01:30:22 +00:00
if minTime != nil {
params["min_time"] = minTime.UTC().Format(time.RFC3339)
2021-09-25 01:30:22 +00:00
}
if maxTime != nil {
params["max_time"] = maxTime.UTC().Format(time.RFC3339)
2021-09-25 01:30:22 +00:00
}
// query the api
resBodies, err := getHeliumApiWithCursor(path, &params)
if err != nil {
return nil, err
}
// unmarshal the responses and merge them all together
combinedResp := activity.ActivityResp{}
for _, respBody := range resBodies {
activityResp := activity.ActivityResp{}
err = json.Unmarshal(respBody, &activityResp)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-25 01:30:22 +00:00
}
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
}
return activity.NewActivities(combinedResp)
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#activity-counts-for-account
func GetActivityCountsForAccount(account string) (*ActivityCounts, error) {
path := "/v1/accounts/" + account + "/activity/count"
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := ActivityCountsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#hotspots-for-account
2021-10-12 13:19:58 +00:00
func GetHotspotsForAccount(account string) ([]Hotspot, error) {
2021-09-15 05:04:32 +00:00
path := "/v1/accounts/" + account + "/hotspots"
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := AccountHotspotsResp{}
2021-09-15 05:04:32 +00:00
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-15 05:04:32 +00:00
}
2021-10-12 13:19:58 +00:00
return respobject.Data, nil
}