Created
February 25, 2022 15:18
-
-
Save BastinRobin/0552815e9cd0d6ee28381ac66a5235ef to your computer and use it in GitHub Desktop.
Ambee API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type Response struct { | |
Message string | |
Stations Stations | |
} | |
type Stations []Station | |
type Station struct { | |
Id string `json:"_id"` | |
CO float32 | |
NO2 float32 | |
OZONE float32 | |
PM10 float32 | |
PM25 float32 | |
SO2 float32 | |
Division string | |
} | |
func main() { | |
url := "https://api.ambeedata.com/latest/by-country-code?countryCode=IN" | |
// Create a http request object | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
log.Println(err) | |
} | |
headers := make(map[string][]string) | |
headers["x-api-key"] = []string{"3eb2e9e011816e5c9d05565d0127c35733b7c38c94f06e4f410e23c53a25e0d2"} | |
headers["Content-Type"] = []string{"application/json"} | |
// Attach the headers information into requst header | |
req.Header = headers | |
// Client to execute this request | |
client := http.Client{} | |
// Call the API here | |
response, err := client.Do(req) | |
if err != nil { | |
log.Println(err) | |
} | |
data, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
var responseData Response | |
err = json.Unmarshal(data, &responseData) | |
if err != nil { | |
log.Println(err) | |
} | |
for _, station := range responseData.Stations { | |
fmt.Println("CO", station.CO, "NO2", station.NO2, "City", station.Division) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment