101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package heliumapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/helium-blockchain-exporter/heliumapi/activity"
|
|
)
|
|
|
|
// query https://docs.helium.com/api/blockchain/hotspots#hotspot-activity
|
|
func GetHotspotActivity(hotspot string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) {
|
|
path := "/v1/hotspots/" + hotspot + "/activity"
|
|
params := map[string]string{}
|
|
if filterTypes != nil {
|
|
params["filter_types"] = strings.Join(filterTypes, ",")
|
|
}
|
|
if minTime != nil {
|
|
params["min_time"] = minTime.UTC().Format(time.RFC3339)
|
|
}
|
|
if maxTime != nil {
|
|
params["max_time"] = maxTime.UTC().Format(time.RFC3339)
|
|
}
|
|
|
|
// query the api
|
|
resBodies, err := getHeliumApiWithCursor(path, ¶ms)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// unmarshal the responses and merge them all together
|
|
combinedResp := activity.ActivityResp{}
|
|
for _, respBody := range resBodies {
|
|
activityResp := activity.ActivityResp{}
|
|
if err := json.Unmarshal(respBody, &activityResp); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
|
|
}
|
|
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
|
|
}
|
|
|
|
return activity.NewActivities(combinedResp)
|
|
}
|
|
|
|
// query https://docs.helium.com/api/blockchain/hotspots#hotspots-activity-counts
|
|
func GetHotspotActivityCounts(hotspot string) (*ActivityCounts, error) {
|
|
path := "/v1/hotspots/" + hotspot + "/activity/count"
|
|
|
|
// query the api
|
|
respBody, err := getHeliumApi(path, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// unmarshal the response
|
|
respobject := ActivityCountsResp{}
|
|
if err := json.Unmarshal(respBody, &respobject); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
|
|
}
|
|
|
|
return &respobject.Data, nil
|
|
}
|
|
|
|
// query https://docs.helium.com/api/blockchain/hotspots#witnesses-for-a-hotspot
|
|
func GetWitnessesForHotspot(hotspot string) ([]Hotspot, error) {
|
|
path := "/v1/hotspots/" + hotspot + "/witnesses"
|
|
|
|
// query the api
|
|
respBody, err := getHeliumApi(path, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// unmarshal the response
|
|
respobject := WitnessesResp{}
|
|
if err := json.Unmarshal(respBody, &respobject); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
|
|
}
|
|
|
|
return respobject.Data, nil
|
|
}
|
|
|
|
// query https://docs.helium.com/api/blockchain/hotspots#witnessed-for-a-hotspot
|
|
func GetWitnessedForHotspot(hotspot string) ([]Hotspot, error) {
|
|
path := "/v1/hotspots/" + hotspot + "/witnessed"
|
|
|
|
// query the api
|
|
respBody, err := getHeliumApi(path, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// unmarshal the response
|
|
respobject := WitnessedResp{}
|
|
if err := json.Unmarshal(respBody, &respobject); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
|
|
}
|
|
|
|
return respobject.Data, nil
|
|
}
|