File: spotmarket.go

package info (click to toggle)
golang-github-packethost-packngo 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 440 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 975 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package packngo

const spotMarketBasePath = "/market/spot/prices"

// SpotMarketService expooses Spot Market methods
type SpotMarketService interface {
	Prices() (PriceMap, *Response, error)
}

// SpotMarketServiceOp implements SpotMarketService
type SpotMarketServiceOp struct {
	client *Client
}

// PriceMap is a map of [facility][plan]-> float Price
type PriceMap map[string]map[string]float64

// Prices gets current PriceMap from the API
func (s *SpotMarketServiceOp) Prices() (PriceMap, *Response, error) {
	root := new(struct {
		SMPs map[string]map[string]struct {
			Price float64 `json:"price"`
		} `json:"spot_market_prices"`
	})

	resp, err := s.client.DoRequest("GET", spotMarketBasePath, nil, root)
	if err != nil {
		return nil, resp, err
	}

	prices := make(PriceMap)
	for facility, planMap := range root.SMPs {
		prices[facility] = map[string]float64{}
		for plan, v := range planMap {
			prices[facility][plan] = v.Price
		}
	}
	return prices, resp, err
}