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.
2021-10-07 14:24:46 +00:00
|
|
|
package heliumapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// query https://docs.helium.com/api/blockchain/blocks/#height
|
|
|
|
func GetHeight(maxTime *time.Time) (int, error) {
|
2021-10-12 13:19:58 +00:00
|
|
|
// TODO: the result of this query could be cached
|
2021-10-07 14:24:46 +00:00
|
|
|
path := "/v1/blocks/height"
|
|
|
|
params := map[string]string{}
|
|
|
|
if maxTime != nil {
|
2021-10-12 13:35:31 +00:00
|
|
|
params["max_time"] = maxTime.UTC().Format(time.RFC3339)
|
2021-10-07 14:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// query the api
|
|
|
|
respBody, err := getHeliumApi(path, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the response
|
|
|
|
respobject := HeightResp{}
|
|
|
|
err = json.Unmarshal(respBody, &respobject)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return respobject.Data.Height, nil
|
|
|
|
}
|