package heliumapi import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) var ( ApiUrl = "https://api.helium.io" client = &http.Client{} ) // createGetRequest create a GET request to the helium api func createGetRequest(path string, params map[string]string) (*http.Request, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", ApiUrl, path), nil) // setup headers req.Header.Add("Accept", "application/json") // setup query param if there are any if params != nil { query := req.URL.Query() for k, v := range params { query.Add(k, v) } if err != nil { return nil, err } req.URL.RawQuery = query.Encode() } return req, err } // getHeliumApi query a regular helium api endpoint func getHeliumApi(path string, params *map[string]string) ([]byte, error) { // if params is nil, set it to an empty map if params == nil { params = &map[string]string{} } req, err := createGetRequest(path, *params) if err != nil { return nil, fmt.Errorf("failed to create query request %v: %v", path, err) } // query the api resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("failed to query %v: %v", req.URL.RequestURI(), err) } defer resp.Body.Close() // validate the response status if resp.StatusCode != 200 { return nil, fmt.Errorf("failed to query %v: http status %v", req.URL.RequestURI(), resp.StatusCode) } // read the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.RequestURI(), err) } return body, nil } // getHeliumApiWithCursor query a helium api with a cursor // It will continue to query the api until all the cursors are exhausted. Combine all the // responses in a single object. func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, error) { // if params is nil, set it to an empty map if params == nil { params = &map[string]string{} } type ResponseWithCursor struct { Cursor string `json:"cursor"` } res := [][]byte{} respCursor := ResponseWithCursor{} req, err := createGetRequest(path, *params) if err != nil { return nil, fmt.Errorf("failed to create query request %v: %v", path, err) } for { // query the api resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("failed to query %v: %v", req.URL.RequestURI(), 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 %v: %v", req.URL.RequestURI(), err) } res = append(res, body) // validate the response status if resp.StatusCode != 200 { return nil, fmt.Errorf("failed to query %v: http status %v", req.URL.RequestURI(), resp.StatusCode) } // parse the response body for a cursor respCursor.Cursor = "" err = json.Unmarshal(body, &respCursor) if err != nil { return nil, fmt.Errorf("failed to unmarshal response from %v: %v", req.URL.RequestURI(), err) } // continue querying until there is no longer a cursor if respCursor.Cursor != "" { params = &map[string]string{"cursor": respCursor.Cursor} req, err = createGetRequest(path, *params) if err != nil { return nil, fmt.Errorf("failed to create query request %v: %v", req.URL.RequestURI(), err) } } else { break } } return res, nil }