-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:robotomize/gokuu
- Loading branch information
Showing
1 changed file
with
69 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,92 @@ | ||
# Gokuu | ||
Library for getting the latest exchange rates | ||
Gokuu is a library for getting up-to-date exchange rates and converting them on Go. Gokuu is a controller and plug-in data sources that work with a specific financial institution. | ||
|
||
# Installation | ||
Right now, Gokuu can retrieve data from the European central bank, the Russian central bank and the central bank of the United Arab Emirates. Altogether, these sources cover exchange rates for 59 currencies | ||
|
||
## Installation | ||
```bash | ||
go get github.com/robotomize/gokuu | ||
``` | ||
# Features | ||
## Features | ||
* Currently supports 59 currency pairs | ||
* Use your own data sources with provider.Source interface | ||
|
||
# Usage | ||
## Usage | ||
|
||
Get actual exchange rates | ||
```go | ||
package main | ||
If you want to get all current exchange rates, you can use the following GetLatest method. | ||
When you match currency pairs from different providers, Gokuu by default saves the data from the provider who gave it faster. | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
```go | ||
ctx := context.Background() | ||
g := gokuu.New(http.DefaultClient) | ||
|
||
"github.com/robotomize/gokuu" | ||
"github.com/robotomize/gokuu/label" | ||
) | ||
latest := g.GetLatest(ctx) | ||
for _, r := range latest.Result { | ||
fmt.Printf("from: %s, to: %s, rate: %f, date: %v", | ||
r.From().Symbol, r.To().Symbol, r.Rate(), r.Time(), | ||
) | ||
} | ||
``` | ||
|
||
func main() { | ||
ctx := context.Background() | ||
g := gokuu.New(http.DefaultClient) | ||
With options you can change the strategy for merging data from multiple providers. For example, a merger based on priorities | ||
|
||
latest := g.GetLatest(ctx) | ||
for _, r := range latest.Result { | ||
fmt.Printf("from: %s, to: %s, rate: %f, date: %v", r.From().Symbol, r.To().Symbol, r.Rate(), r.Time()) | ||
} | ||
} | ||
```go | ||
g := gokuu.New(http.DefaultClient, gokuu.WithPriorityMergeStrategy()) | ||
``` | ||
|
||
Conversion from one currency to another | ||
Calculate the arithmetic average of the currency pair from all providers | ||
|
||
```go | ||
package main | ||
g := gokuu.New(http.DefaultClient, gokuu.WithAverageMergeStrategy()) | ||
``` | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
You can also use the conversion function | ||
```go | ||
ctx := context.Background() | ||
g := gokuu.New(http.DefaultClient) | ||
|
||
"github.com/robotomize/gokuu" | ||
"github.com/robotomize/gokuu/label" | ||
conv, err := g.Convert( | ||
ctx, gokuu.ConvOpt{ | ||
From: label.USD, | ||
To: label.RUB, | ||
Value: 10, | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
fmt.Println(conv) | ||
``` | ||
|
||
func main() { | ||
ctx := context.Background() | ||
g := gokuu.New(http.DefaultClient) | ||
|
||
conv, err := g.Convert( | ||
ctx, gokuu.ConvOpt{ | ||
From: label.USD, | ||
To: label.RUB, | ||
Value: 10, | ||
Use your caching function for better performance. For example like this | ||
```go | ||
g := gokuu.New(http.DefaultClient, gokuu.WithAverageMergeStrategy()) | ||
latest := g.GetLatest(ctx) | ||
conv, err := g.Convert( | ||
ctx, gokuu.ConvOpt{ | ||
From: label.USD, | ||
To: label.RUB, | ||
Value: 10, | ||
CacheFn: func(ctx context.Context) gokuu.LatestResponse { | ||
return latest | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
fmt.Println(conv) | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
fmt.Println(conv) | ||
``` | ||
|
||
You can also use the helper functions from the package github.com/robotomize/gokuu/label | ||
```go | ||
label.GetSymbols() | ||
label.GetCountries() | ||
label.GetCurrencies() | ||
label.GetCountriesUsingCurrency("currency-symbol") | ||
label.GetCurrenciesUsedCountry("countryname") | ||
``` | ||
|
||
## License | ||
|
||
SOD is under the Apache 2.0 license. See the [LICENSE](LICENSE) file for details. |