FetchPricing calls POST /pricing with the given filters.
(products, regions []string, attrs map[string]string, prices []string)
| 126 | |
| 127 | // FetchPricing calls POST /pricing with the given filters. |
| 128 | func (c *Client) FetchPricing(products, regions []string, attrs map[string]string, prices []string) (*PricingAPIResponse, error) { |
| 129 | params := url.Values{} |
| 130 | for _, p := range products { |
| 131 | parts := strings.SplitN(p, " ", 2) |
| 132 | if len(parts) >= 1 && parts[0] != "" { |
| 133 | params.Add("provider", parts[0]) |
| 134 | } |
| 135 | if len(parts) == 2 && parts[1] != "" { |
| 136 | params.Add("products", parts[1]) |
| 137 | } |
| 138 | } |
| 139 | for _, r := range regions { |
| 140 | params.Add("region", r) |
| 141 | } |
| 142 | |
| 143 | endpoint := APIBaseURL + "/pricing?" + params.Encode() |
| 144 | status, respBytes, err := c.post(endpoint, PricingRequest{Attrs: attrs, Prices: prices}, true) |
| 145 | if err != nil { |
| 146 | return nil, fmt.Errorf("failed to connect to pricing API: %w", err) |
| 147 | } |
| 148 | |
| 149 | if status == http.StatusNotFound { |
| 150 | return &PricingAPIResponse{Data: []PricingItem{}, Total: 0}, nil |
| 151 | } |
| 152 | if status >= 300 { |
| 153 | return nil, fmt.Errorf("API request failed (%d): %s", status, string(respBytes)) |
| 154 | } |
| 155 | |
| 156 | var result PricingAPIResponse |
| 157 | if err := json.Unmarshal(respBytes, &result); err != nil { |
| 158 | preview := string(respBytes) |
| 159 | if len(preview) > 500 { |
| 160 | preview = preview[:500] |
| 161 | } |
| 162 | return nil, fmt.Errorf("failed to parse pricing response: %w (first 500 chars: %s)", err, preview) |
| 163 | } |
| 164 | return &result, nil |
| 165 | } |
| 166 | |
| 167 | func (c *Client) FetchPricingBatch(requests BatchPricingRequest) (*BatchPricingApiResponse, error) { |
| 168 | endpoint := APIBaseURL + "/pricing/batch" |