-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (73 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
const APIurl = "http://api.openweathermap.org/data/2.5/weather?q="
type APIResp struct {
Weather []struct {
Description string `json:"description"`
} `json:"weather"`
Main struct {
Temp float64 `json:"temp"`
} `json:"main"`
}
var location string
var WeatherInfo APIResp
var dotenv string
// GetAPIkey -> string
// Retrieves the API key stored within the .env file
func GetAPIkey(key string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
return os.Getenv(key)
}
// KtoC -> String
// Convert a given Kelvin metric to Celsius
func KtoC(k float64) string {
return fmt.Sprintf("%.2f", k-273.15)
}
// GetAPI -> *http.Response, error
// Uses the HTTP GET method to make a GET request to the API
func GetAPI(loc, key string) (*http.Response, error) {
resp, err := http.Get(APIurl + location + "&appid=" + dotenv)
if err != nil {
log.Fatalln(err)
}
return resp, err
}
// ReadAPI -> []byte, error
// Reads the response returned from GetApi's GET method call
func ReadAPI(resp *http.Response) ([]byte, error) {
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatalln(err)
}
return body, err
}
func main() {
dotenv = GetAPIkey("APIKEY")
flag.StringVar(&location, "location", "London", "Flagname should specify a location e.g. London")
flag.Parse()
r, err := GetAPI(location, dotenv)
if err != nil {
fmt.Println(err)
}
rb, err := ReadAPI(r)
if err != nil {
fmt.Println(err)
}
json.Unmarshal(rb, &WeatherInfo)
Desc := WeatherInfo.Weather[0].Description
Temp := WeatherInfo.Main.Temp
fmt.Println("The weather in " + location + " is currently " + KtoC(Temp) + " degrees Celsius. With a description of " + Desc)
}