2022-08-07 04:52:08 -04:00
|
|
|
package nps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-09-11 07:49:23 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-07 04:52:08 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2022-09-11 07:49:23 -04:00
|
|
|
"time"
|
2022-08-07 04:52:08 -04:00
|
|
|
|
|
|
|
"github.com/ananthakumaran/paisa/internal/model/price"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetNav(schemeCode string, commodityName string) ([]*price.Price, error) {
|
2022-09-11 07:49:23 -04:00
|
|
|
log.Info("Fetching NPS Fund nav from Purified Bytes")
|
2022-08-07 04:52:08 -04:00
|
|
|
url := fmt.Sprintf("https://nps.purifiedbytes.com/api/schemes/%s/nav.json", schemeCode)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
Date string
|
|
|
|
Nav float64
|
|
|
|
}
|
|
|
|
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.Parse("2006-01-02", data.Date)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
price := price.Price{Date: date, CommodityType: price.NPS, CommodityID: schemeCode, CommodityName: commodityName, Value: data.Nav}
|
|
|
|
prices = append(prices, &price)
|
|
|
|
}
|
|
|
|
return prices, nil
|
|
|
|
}
|