forked from d6o/GoCurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgocurrency.go
More file actions
69 lines (55 loc) · 1.73 KB
/
Copy pathgocurrency.go
File metadata and controls
69 lines (55 loc) · 1.73 KB
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
package gocurrency
import (
"encoding/json"
"errors"
"fmt"
"github.com/shopspring/decimal"
)
const availableCurrenciesURL string = "http://currencyconverter.kund.nu/api/availablecurrencies/"
const convertCurrencyURL string = "http://currencyconverter.kund.nu/api/?from=%s&from_amount=%s&to=%s"
// Currency contains ID and Description of an Currency.
type Currency struct {
ID string `json:"id"`
Description string `json:"description"`
}
// NewCurrency Create an instance of Currency.
func NewCurrency(currency string) Currency {
return Currency{ID: currency}
}
// AvailableCurrencies returns an array with all currencies that can be used.
func AvailableCurrencies() ([]Currency, error) {
resp, err := newRequest().Get(availableCurrenciesURL)
if err != nil {
return nil, err
}
curList := []Currency{}
err = json.Unmarshal(resp, &curList)
if err != nil {
return nil, err
}
return curList, nil
}
type convertCurrencyResponse struct {
From string `json:"from"`
To string `json:"to"`
FromAmount float64 `json:"from_amount"`
ToAmount float64 `json:"to_amount"`
Error string `json:"error"`
}
// ConvertCurrency Converts an amount (Decimal) from one currency to another currency.
func ConvertCurrency(from, to Currency, amount decimal.Decimal) (decimal.Decimal, error) {
endpoint := fmt.Sprintf(convertCurrencyURL, from.ID, amount, to.ID)
resp, err := newRequest().Get(endpoint)
if err != nil {
return decimal.NewFromFloat(0), err
}
ccResp := convertCurrencyResponse{}
err = json.Unmarshal(resp, &ccResp)
if err != nil {
return decimal.NewFromFloat(0), err
}
if ccResp.Error != "" {
return decimal.NewFromFloat(0), errors.New(ccResp.Error)
}
return decimal.NewFromFloat(ccResp.ToAmount), nil
}