32 lines
669 B
Go
32 lines
669 B
Go
|
package heliumapi
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// query https://docs.helium.com/api/blockchain/blocks/#height
|
||
|
func GetHeight(maxTime *time.Time) (int, error) {
|
||
|
path := "/v1/blocks/height"
|
||
|
params := map[string]string{}
|
||
|
if maxTime != nil {
|
||
|
params["max_time"] = maxTime.UTC().Format("2006-01-02T15:04:05Z")
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|