-
Notifications
You must be signed in to change notification settings - Fork 1
/
go_exchange.go
97 lines (78 loc) · 2.2 KB
/
go_exchange.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
91
92
93
94
95
96
97
// go_exchange... simple command line access to exchange rates
//
// by: nicholas ward
// http://github.com/enndott
//
// note: mostly just to familiarize myself w/ how to deal with JSON in go.
// definitely not stable :)
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
func fetchExchangeRatesJSON(apikey string) []byte {
//gets the latest values from openexchangerates
res, err := http.Get("http://openexchangerates.org/api/latest.json?app_id=" + apikey)
if err != nil {
panic(err)
}
//read the response into a variable, jsonData
jsonData, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
//close it
res.Body.Close()
return jsonData
}
func parseExchangeRatesJSON(jsonData []byte) map[string]interface{} {
//parses the exchange rate JSON downloaded in fetchExchangeRatesJSON
//returns a map of [currency code]:[exchange rate] ex: "USD:1"
response := map[string]interface{}{}
json.Unmarshal(jsonData, &response)
//check for the error response in the JSON
//echo and quit with details if found.
if response["error"] != nil {
fmt.Println("There was an error: " + response["message"].(string))
fmt.Println("Details: " + response["description"].(string))
fmt.Println("")
os.Exit(1)
}
//skip to the exchange rate map and return
return response["rates"].(map[string]interface{})
}
func main() {
//set the API key
//to get an API key go to: https://openexchangerates.org/signup
var apikey string = ""
if apikey == "" {
fmt.Println("There was an error: no api key defined.")
fmt.Println("Please add an API key to line 64.")
os.Exit(1)
}
//get the latest values and parse
jsonData := fetchExchangeRatesJSON(apikey)
rates := parseExchangeRatesJSON(jsonData)
//check to see of an argument was passed. If so,
//parse the list
if len(os.Args) > 1 {
//split it by comma
currencyList := strings.Split(os.Args[1], ",")
for _, value := range currencyList {
if rates[value] != nil {
fmt.Println(value, ": ", rates[value])
} else {
fmt.Println(value, ": currency not found.")
}
}
} else {
fmt.Println("Printing all available exchange rates.")
for key, value := range rates {
fmt.Println(key, ": ", value)
}
}
}