Item price support
This commit is contained in:
@@ -96,31 +96,40 @@ type VolumeEntry struct {
|
||||
}
|
||||
|
||||
// TimeseriesResponse wraps the 5m/1h API responses.
|
||||
// The API returns data as a map keyed by item ID string.
|
||||
type TimeseriesResponse struct {
|
||||
Data []VolumeEntry `json:"data"`
|
||||
Data map[string]VolumeEntry `json:"data"`
|
||||
}
|
||||
|
||||
// Get5Min fetches 5-minute average data for an item.
|
||||
func (c *Client) Get5Min(itemID int) (*TimeseriesResponse, error) {
|
||||
func (c *Client) Get5Min(itemID int) (*VolumeEntry, error) {
|
||||
url := fmt.Sprintf("%s/5m?id=%d", c.baseURL, itemID)
|
||||
var resp TimeseriesResponse
|
||||
if err := c.getJSON(url, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
idStr := fmt.Sprintf("%d", itemID)
|
||||
if entry, ok := resp.Data[idStr]; ok {
|
||||
return &entry, nil
|
||||
}
|
||||
return nil, fmt.Errorf("no 5m data for item %d", itemID)
|
||||
}
|
||||
|
||||
// Get1Hour fetches 1-hour average data for an item.
|
||||
func (c *Client) Get1Hour(itemID int) (*TimeseriesResponse, error) {
|
||||
func (c *Client) Get1Hour(itemID int) (*VolumeEntry, error) {
|
||||
url := fmt.Sprintf("%s/1h?id=%d", c.baseURL, itemID)
|
||||
var resp TimeseriesResponse
|
||||
if err := c.getJSON(url, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
idStr := fmt.Sprintf("%d", itemID)
|
||||
if entry, ok := resp.Data[idStr]; ok {
|
||||
return &entry, nil
|
||||
}
|
||||
return nil, fmt.Errorf("no 1h data for item %d", itemID)
|
||||
}
|
||||
|
||||
func (c *Client) getJSON(url string, dest interface{}) error {
|
||||
func (c *Client) getJSON(url string, dest any) error {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating request: %w", err)
|
||||
@@ -131,7 +140,7 @@ func (c *Client) getJSON(url string, dest interface{}) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
Reference in New Issue
Block a user