Skip to content

Commit

Permalink
configurable api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Mar 23, 2022
1 parent 4c1fa3c commit c13ffdd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
19 changes: 10 additions & 9 deletions price/crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type Crawler struct {
Url string
Key string
}

Expand All @@ -31,7 +32,7 @@ type Data struct {
}

func (c *Crawler) GetPrices(base string) {
uri := "https://www.commodities-api.com/api/latest"
uri := c.Url + "/latest"
vals := url.Values{}
vals.Set("access_key", c.Key)
vals.Set("base", base)
Expand Down Expand Up @@ -63,7 +64,7 @@ func (c *Crawler) GetPrices(base string) {
Symbol: symbol,
Currency: rsp.Data.Base,
Timestamp: time.Unix(rsp.Data.Timestamp, 0).Format(time.RFC3339Nano),
Source: "www.commodities-api.com",
Source: c.Url,
Author: "Micro",
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func (c *Crawler) Run() {
vals := url.Values{}
vals.Set("access_key", c.Key)
q := vals.Encode()
uri := "https://www.commodities-api.com/api/symbols?" + q
uri := c.Url + "/symbols?" + q

if err := api.Get(uri, &index); err != nil {
logger.Errorf("Failed to get index symbols: %v", err)
Expand Down Expand Up @@ -158,7 +159,7 @@ func (c *Crawler) Run() {
}

func (c *Crawler) Get(symbol, currency string) (*pb.Value, error) {
uri := "https://www.commodities-api.com/api/latest"
uri := c.Url + "/latest"
vals := url.Values{}
vals.Set("access_key", c.Key)
vals.Set("base", currency)
Expand All @@ -180,7 +181,7 @@ func (c *Crawler) Get(symbol, currency string) (*pb.Value, error) {
Symbol: symbol,
Currency: rsp.Data.Base,
Timestamp: time.Unix(rsp.Data.Timestamp, 0).Format(time.RFC3339Nano),
Source: "www.commodities-api.com",
Source: c.Url,
Author: "Micro",
}

Expand Down Expand Up @@ -221,7 +222,7 @@ func (c *Crawler) Get(symbol, currency string) (*pb.Value, error) {
}

func (c *Crawler) List(currency string) ([]*pb.Value, error) {
uri := "https://www.commodities-api.com/api/latest"
uri := c.Url + "/latest"
vals := url.Values{}
vals.Set("access_key", c.Key)
vals.Set("base", currency)
Expand All @@ -245,7 +246,7 @@ func (c *Crawler) List(currency string) ([]*pb.Value, error) {
Symbol: symbol,
Currency: rsp.Data.Base,
Timestamp: time.Unix(rsp.Data.Timestamp, 0).Format(time.RFC3339Nano),
Source: "www.commodities-api.com",
Source: c.Url,
Author: "Micro",
}

Expand Down Expand Up @@ -288,6 +289,6 @@ func (c *Crawler) List(currency string) ([]*pb.Value, error) {
return values, nil
}

func New(key string) *Crawler {
return &Crawler{key}
func New(url, key string) *Crawler {
return &Crawler{Url: url, Key: key}
}
10 changes: 8 additions & 2 deletions price/handler/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ var (

func New() *Price {
// TODO: look for "crypto.provider" to determine the handler
v, err := config.Get("commodities.api_key")
v, err := config.Get("commodities.api_url")
if err != nil {
logger.Fatalf("commodities.api_url config not found: %v", err)
}
url := v.String("")
// TODO: look for "crypto.provider" to determine the handler
v, err = config.Get("commodities.api_key")
if err != nil {
logger.Fatalf("commodities.api_key config not found: %v", err)
}
Expand All @@ -36,7 +42,7 @@ func New() *Price {
logger.Fatal("commodities.api config not found")
}

c := crawler.New(key)
c := crawler.New(url, key)
go c.Run()

return &Price{c}
Expand Down

0 comments on commit c13ffdd

Please sign in to comment.