teslainventory_sdk/stock.go
Matthieu 'JP' DERASSE 05ac49fd68
Some checks failed
continuous-integration/drone Build is failing
feat(first): First commit with basic implementation. Missing test
2023-08-09 12:11:17 +00:00

39 lines
936 B
Go

package teslastocksdk
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
// GetAvailabilities return the car availabilities matching with the provided characteristics.
func (c *Client) GetAvailabilities(ctx context.Context, params AvailabilityParams) (*AvailabilitiesResponse, error) {
b, err := json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("fail to marshal availability params. Error: %w", err)
}
queryParams := url.Values{
"query": {string(b)},
}
resp, err := c.get(
ctx,
fmt.Sprintf("/inventory/api/v1/inventory-results?%s", queryParams.Encode()),
nil,
)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }() // explicitly discard error
availabilities := AvailabilitiesResponse{}
if err := json.NewDecoder(resp.Body).Decode(&availabilities); err != nil {
return nil, fmt.Errorf("fail to unmarshal response. Error: %w", err)
}
return &availabilities, nil
}