63 lines
1.1 KiB
Go
63 lines
1.1 KiB
Go
package aislegopher
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type DatePrice struct {
|
|
Date string
|
|
Price float64
|
|
}
|
|
|
|
type PriceHistory struct {
|
|
RegularPrices []DatePrice `json:"regularPrices,omitempty"`
|
|
ThirdPartyPrices []DatePrice `json:"3rdPartyPrices,omitempty"`
|
|
}
|
|
|
|
type Config struct {
|
|
Client *http.Client
|
|
}
|
|
|
|
var DefaultConfig = Config{
|
|
Client: http.DefaultClient,
|
|
}
|
|
|
|
func GetPriceHistory(productId int) (PriceHistory, error) {
|
|
return DefaultConfig.GetPriceHistory(productId)
|
|
}
|
|
|
|
func (c Config) GetPriceHistory(productId int) (PriceHistory, error) {
|
|
cl := c.Client
|
|
|
|
if cl == nil {
|
|
cl = http.DefaultClient
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "https://aislegopher.com/Home/GetPriceHistoryData?productId="+strconv.Itoa(productId), nil)
|
|
|
|
if err != nil {
|
|
return PriceHistory{}, err
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
res, err := cl.Do(req)
|
|
|
|
if err != nil {
|
|
return PriceHistory{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
var priceHistory PriceHistory
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&priceHistory)
|
|
|
|
if err != nil {
|
|
return PriceHistory{}, err
|
|
}
|
|
|
|
return priceHistory, nil
|
|
}
|