-
Notifications
You must be signed in to change notification settings - Fork 111
/
nav.go
61 lines (52 loc) · 1.28 KB
/
nav.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
package mutualfund
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
"github.com/ananthakumaran/paisa/internal/config"
"github.com/ananthakumaran/paisa/internal/model/price"
)
func GetNav(schemeCode string, commodityName string) ([]*price.Price, error) {
log.Info("Fetching Mutual Fund nav from mfapi.in")
url := fmt.Sprintf("https://api.mfapi.in/mf/%s", schemeCode)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
type Data struct {
Date string
Nav string
}
type Result struct {
Data []Data
}
var result Result
err = json.Unmarshal(respBytes, &result)
if err != nil {
return nil, err
}
var prices []*price.Price
for _, data := range result.Data {
date, err := time.ParseInLocation("02-01-2006", data.Date, time.Local)
if err != nil {
return nil, err
}
value, err := strconv.ParseFloat(data.Nav, 64)
if err != nil {
return nil, err
}
price := price.Price{Date: date, CommodityType: config.MutualFund, CommodityID: schemeCode, CommodityName: commodityName, Value: decimal.NewFromFloat(value)}
prices = append(prices, &price)
}
return prices, nil
}