mirror of
https://github.com/ananthakumaran/paisa.git
synced 2024-09-12 16:39:28 -04:00
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
|
package nps
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"time"
|
||
|
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/ananthakumaran/paisa/internal/model/price"
|
||
|
)
|
||
|
|
||
|
func GetNav(schemeCode string, commodityName string) ([]*price.Price, error) {
|
||
|
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
|
||
|
}
|