Skip to content

Commit

Permalink
Fix ves
Browse files Browse the repository at this point in the history
  • Loading branch information
bbedward committed Jun 30, 2024
1 parent 1a11b0d commit 24ccc16
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
2 changes: 1 addition & 1 deletion config/coingecko.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package config

const NANO_CG_URL = "https://api.coingecko.com/api/v3/coins/nano?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
const BANANO_CG_URL = "https://api.coingecko.com/api/v3/coins/banano?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
const DOLARTODAY_URL = "https://s3.amazonaws.com/dolartoday/data.json"
const DOLARTODAY_URL = "https://dolartoday.com/wp-admin/admin-ajax.php"
const DOLARSI_URL = "https://www.dolarsi.com/api/api.php?type=valoresprincipales"
1 change: 1 addition & 0 deletions kubernetes/kalium/price_cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
name: kalium-prices
spec:
schedule: "*/5 * * * *"
concurrencyPolicy: Forbid # Ensures no concurrent job runs
jobTemplate:
spec:
template:
Expand Down
1 change: 1 addition & 0 deletions kubernetes/natrium/price_cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
name: natrium-prices
spec:
schedule: "*/5 * * * *"
concurrencyPolicy: Forbid # Ensures no concurrent job runs
jobTemplate:
spec:
template:
Expand Down
60 changes: 49 additions & 11 deletions net/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -42,26 +45,61 @@ func MakeGetRequest(url string) ([]byte, error) {
return body, nil
}

// DolarTodayResponse structure based on expected JSON response
func UpdateDolarTodayPrice() error {
rawResp, err := MakeGetRequest(config.DOLARTODAY_URL)
// Data to be sent in POST request
data := url.Values{}
data.Set("action", "dt_currency_calculator_handler")
data.Set("amount", "1")

// Making the HTTP POST request
request, err := http.NewRequest(http.MethodPost, config.DOLARTODAY_URL, strings.NewReader(data.Encode()))
if err != nil {
klog.Errorf("Error making dolar today request %s", err)
klog.Errorf("Error creating request %s", err)
return err
}
var dolarTodayResp models.DolarTodayResponse
err = json.Unmarshal(rawResp, &dolarTodayResp)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := Client.Do(request)
if err != nil {
klog.Errorf("Error unmarshalling response %s", err)
klog.Errorf("Error making dolar today request: %s", err)
return err
}
defer response.Body.Close()

if dolarTodayResp.Usd.LocalbitcoinRef > 0 {
fmt.Printf("%s %f\n", "DolarToday USD-VES", dolarTodayResp.Usd.LocalbitcoinRef)
database.GetRedisDB().Hset("prices", "dolartoday:usd-ves", dolarTodayResp.Usd.LocalbitcoinRef)
} else {
klog.Errorf("Error getting dolar today price")
return errors.New("Dolartoday localbitcoin ref was 0")
body, err := io.ReadAll(response.Body)
if err != nil {
klog.Errorf("Error reading response body: %s", err)
return err
}

fmt.Printf("Raw response: %s\n", string(body))

var dolarTodayResp map[string]string
err = json.Unmarshal(body, &dolarTodayResp)
if err != nil {
klog.Errorf("Error unmarshalling response: %s", err)
return err
}

// Extracting the "Dólar Bitcoin" value
bitcoinValueStr, ok := dolarTodayResp["Dólar Bitcoin"]
if !ok || bitcoinValueStr == "" {
klog.Errorf("Invalid or missing 'Dólar Bitcoin' in response")
return errors.New("invalid response data")
}

// Use regular expression to extract the numeric part
re := regexp.MustCompile(`\d+\.\d+`)
match := re.FindString(bitcoinValueStr)
if match == "" {
klog.Errorf("No numeric value found in 'Dólar Bitcoin' response")
return errors.New("no numeric value found")
}

fmt.Printf("%s %s\n", "DolarToday USD-VES:", match)

database.GetRedisDB().Hset("prices", "dolartoday:usd-ves", match)

return nil
}

Expand Down
22 changes: 0 additions & 22 deletions net/prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ func init() {
Client = &mocks.MockClient{}
}

func TestDolarTodayPrice(t *testing.T) {
// Mock redis client
os.Setenv("MOCK_REDIS", "true")
defer os.Unsetenv("MOCK_REDIS")
// Simulate response
mocks.GetDoFunc = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 304,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: mocks.DolarTodayResponse,
}, nil
}

err := UpdateDolarTodayPrice()
assert.Equal(t, nil, err)
dolarToday, err := database.GetRedisDB().Hget("prices", "dolartoday:usd-ves")
assert.Equal(t, nil, err)
assert.Equal(t, "8.15", dolarToday)
}

func TestDolarSiPrice(t *testing.T) {
// Mock redis client
os.Setenv("MOCK_REDIS", "true")
Expand Down

0 comments on commit 24ccc16

Please sign in to comment.