1
0
Fork 0

add option to supplu api url
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Massaki Archambault 2021-09-26 00:08:10 -04:00
parent 592fbea0e6
commit 42e2a23e26
6 changed files with 25 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
@ -607,12 +608,14 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
}
func main() {
fApiUrl := flag.String("apiUrl", "https://api.helium.io", "The helium api url")
fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape (optional)")
fMetricsPath := flag.String("metricpath", "/metrics", "The metrics path")
fListenAddress := flag.String("listenAddress", "0.0.0.0", "The http server listen address")
fListenPort := flag.String("listenPort", "9865", "The http server listen port")
flag.Parse()
heliumapi.ApiUrl = *fApiUrl
heliumAccounts := strings.Split(*fHeliumAccounts, ",")
serverAddr := *fListenAddress + ":" + *fListenPort
@ -637,5 +640,8 @@ func main() {
http.Handle(*fMetricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
fmt.Printf("listening on %v\n", serverAddr)
http.ListenAndServe(serverAddr, nil)
if err = http.ListenAndServe(serverAddr, nil); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

View File

@ -21,7 +21,7 @@ func GetAccountForAddress(account string) (*Account, error) {
respobject := AccountResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
@ -49,7 +49,7 @@ func GetActivityForAccount(account string, filterTypes []string, minTime *time.T
activityResp := activity.ActivityResp{}
err = json.Unmarshal(respBody, &activityResp)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
}
@ -70,7 +70,7 @@ func GetActivityCountsForAccount(account string) (*ActivityCounts, error) {
respobject := ActivityCountsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
@ -96,7 +96,7 @@ func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time
respobject := RewardTotalResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
@ -115,7 +115,7 @@ func GetHotspotsForAccount(account string) (*[]AccountHotspot, error) {
respobject := AccountHotspotsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil

View File

@ -19,7 +19,7 @@ func GetHotspotActivityCount(hotspot string) (*ActivityCounts, error) {
respobject := ActivityCountsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
@ -45,7 +45,7 @@ func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time
respobject := RewardTotalResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil

View File

@ -6,7 +6,7 @@ import (
)
func GetCurrentOraclePrice() (*CurrentOraclePrice, error) {
const path = "/v1/oracle/prices/current"
path := "/v1/oracle/prices/current"
// query the api
respBody, err := getHeliumApi(path, nil)
@ -18,7 +18,7 @@ func GetCurrentOraclePrice() (*CurrentOraclePrice, error) {
respobject := CurrentOraclePriceResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil

View File

@ -7,16 +7,13 @@ import (
"net/http"
)
const (
apiUrl = "https://api.helium.io"
)
var (
ApiUrl = "https://api.helium.io"
client = &http.Client{}
)
func createGetRequest(path string, params map[string]string) (*http.Request, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", apiUrl, path), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", ApiUrl, path), nil)
// setup headers
req.Header.Add("Accept", "application/json")
@ -50,14 +47,14 @@ func getHeliumApi(path string, params *map[string]string) ([]byte, error) {
// query the api
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
return nil, fmt.Errorf("failed to query %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 nil, fmt.Errorf("failed to read response body of %v: %v", path, err)
}
return body, nil
@ -84,14 +81,14 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
// query the api
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
return nil, fmt.Errorf("failed to query %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)
return nil, fmt.Errorf("failed to read response body of %v: %v", path, err)
}
res = append(res, body)
@ -99,7 +96,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
respCursor.Cursor = ""
err = json.Unmarshal(body, &respCursor)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
// continue querying until there is no longer a cursor

View File

@ -6,7 +6,7 @@ import (
)
func GetBlockchainStats() (*BlockchainStats, error) {
const path = "/v1/stats"
path := "/v1/stats"
// query the api
respBody, err := getHeliumApi(path, nil)
@ -18,7 +18,7 @@ func GetBlockchainStats() (*BlockchainStats, error) {
respobject := BlockchainStatsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil