forked from fadion/gofixerio
/
fixerio.go
141 lines (114 loc) · 2.54 KB
/
fixerio.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Package Fixerio provides a simple interface to the
fixer.io API, a service for currency exchange rates.
*/
package fixerio
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
)
// Holds the request parameters.
type Request struct {
base string
protocol string
date string
symbols []string
apiKey string
}
// JSON response object.
type Response struct {
Base string `json:"base"`
Date string `json:"date"`
Rates rates `json:"rates"`
}
type rates map[string]float32
var baseUrl = "api.fixer.io"
var authBaseUrl = "data.fixer.io/api"
// Initializes fixerio.
func New() *Request {
return &Request{
base: EUR,
protocol: "https",
date: "",
symbols: make([]string, 0),
apiKey: "",
}
}
func (f *Request) APIKey(key string) {
f.apiKey = key
}
// Sets base currency.
func (f *Request) Base(currency string) {
f.base = currency
}
// Make the connection secure or not by setting the
// secure argument to true or false.
func (f *Request) Secure(secure bool) {
if secure {
f.protocol = "https"
} else {
f.protocol = "http"
}
}
// List of currencies that should be returned.
func (f *Request) Symbols(currencies ...string) {
f.symbols = currencies
}
// Specify a date in the past to retrieve historical records.
func (f *Request) Historical(date time.Time) {
f.date = date.Format("2006-01-02")
}
// Retrieve the exchange rates.
func (f *Request) GetRates() (rates, error) {
url := f.GetUrl()
response, err := f.makeRequest(url)
if err != nil {
return rates{}, err
}
return response, nil
}
// Formats the URL correctly for the API Request.
func (f *Request) GetUrl() string {
var url bytes.Buffer
url.WriteString(f.protocol)
url.WriteString("://")
if f.apiKey == "" {
url.WriteString(baseUrl)
} else {
url.WriteString(authBaseUrl)
}
url.WriteString("/")
if f.date == "" {
url.WriteString("latest")
} else {
url.WriteString(f.date)
}
url.WriteString("?base=")
url.WriteString(string(f.base))
if len(f.symbols) >= 1 {
url.WriteString("&symbols=")
url.WriteString(strings.Join(f.symbols, ","))
}
if f.apiKey != "" {
url.WriteString("&access_key=")
url.WriteString(f.apiKey)
}
return url.String()
}
func (f *Request) makeRequest(url string) (rates, error) {
var response Response
body, err := http.Get(url)
if err != nil {
return rates{}, errors.New("Couldn't connect to server")
}
defer body.Body.Close()
err = json.NewDecoder(body.Body).Decode(&response)
if err != nil {
return rates{}, errors.New("Couldn't parse Response")
}
return response.Rates, nil
}