From 42e2a23e2651eb12c2a4035486cf6ea59774eded Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Sun, 26 Sep 2021 00:08:10 -0400 Subject: [PATCH] add option to supplu api url --- exporter.go | 8 +++++++- heliumapi/accounts.go | 10 +++++----- heliumapi/hotspots.go | 4 ++-- heliumapi/oracleprices.go | 4 ++-- heliumapi/query.go | 17 +++++++---------- heliumapi/stats.go | 4 ++-- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/exporter.go b/exporter.go index 2a21e20..e204943 100644 --- a/exporter.go +++ b/exporter.go @@ -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) + } } diff --git a/heliumapi/accounts.go b/heliumapi/accounts.go index 8e29ebd..67ad492 100644 --- a/heliumapi/accounts.go +++ b/heliumapi/accounts.go @@ -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 diff --git a/heliumapi/hotspots.go b/heliumapi/hotspots.go index f29edfd..2cd630d 100644 --- a/heliumapi/hotspots.go +++ b/heliumapi/hotspots.go @@ -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 diff --git a/heliumapi/oracleprices.go b/heliumapi/oracleprices.go index 20e7d79..aaa9229 100644 --- a/heliumapi/oracleprices.go +++ b/heliumapi/oracleprices.go @@ -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 diff --git a/heliumapi/query.go b/heliumapi/query.go index 4a4dced..c0df6b9 100644 --- a/heliumapi/query.go +++ b/heliumapi/query.go @@ -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 diff --git a/heliumapi/stats.go b/heliumapi/stats.go index b63ba1c..bd50948 100644 --- a/heliumapi/stats.go +++ b/heliumapi/stats.go @@ -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