-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tariff_charges.go
127 lines (103 loc) Β· 4 KB
/
tariff_charges.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:generate stringer -linecomment -type=Rate
package octopusenergy
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// TariffChargeService handles communication with the tariff related Octopus API.
type TariffChargeService service
// Rate is the type of charge, example standing-charges, day-unit-rates, standard-unit-rates
type Rate int
const (
RateStandingCharge Rate = iota // standing-charges
RateStandardUnit // standard-unit-rates
RateDayUnit // day-unit-rates
RateNightUnit // night-unit-rates
)
// TariffChargesGetOptions is the options for GetTariffCharges.
type TariffChargesGetOptions struct {
// The code of the product to be retrieved.
ProductCode string `url:"-"`
// The code of the tariff to be retrieved.
TariffCode string `url:"-"`
// Fueltype: electricity or gas
FuelType FuelType `url:"-"`
// The type of charge
Rate Rate `url:"-"`
// Show charges active from the given datetime (inclusive). This parameter can be provided on its own.
PeriodFrom *time.Time `url:"period_from,omitempty" layout:"2006-01-02T15:04:05Z" optional:"true"`
// Show charges active up to the given datetime (exclusive).
// You must also provide the period_from parameter in order to create a range.
PeriodTo *time.Time `url:"period_to,omitempty" layout:"2006-01-02T15:04:05Z" optional:"true"`
// Page size of returned results.
// Default is 100, maximum is 1,500 to give up to a month of half-hourly prices.
PageSize *int `url:"page_size,omitempty" optional:"true"`
// Pagination page to be returned on this request
Page *int `url:"page,omitempty" optional:"true"`
}
// TariffChargesGetOutput is the returned struct from GetTariffCharges.
type TariffChargesGetOutput struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ValueExcVat float64 `json:"value_exc_vat"`
ValueIncVat float64 `json:"value_inc_vat"`
ValidFrom time.Time `json:"valid_from"`
ValidTo time.Time `json:"valid_to"`
} `json:"results"`
}
// Get retrieves the details of a tariffs changes. This endpoint is paginated, it will return
// next and previous links if returned data is larger than the set page size, you are responsible
// to request the next page if required.
func (s *TariffChargeService) Get(options *TariffChargesGetOptions) (*TariffChargesGetOutput, error) {
return s.GetWithContext(context.Background(), options)
}
// GetWithContext same as Get except it takes a Context
func (s *TariffChargeService) GetWithContext(ctx context.Context, options *TariffChargesGetOptions) (*TariffChargesGetOutput, error) {
path := fmt.Sprintf("/v1/products/%s/%s-tariffs/%s/%s/", options.ProductCode, options.FuelType.String(), options.TariffCode, options.Rate.String())
rel := &url.URL{Path: path}
u := s.client.BaseURL.ResolveReference(rel)
url, err := addParameters(u, options)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil)
if err != nil {
return nil, err
}
res := TariffChargesGetOutput{}
if err := s.client.sendRequest(req, false, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetPages same as Get except it returns all pages in one request
func (s *TariffChargeService) GetPages(options *TariffChargesGetOptions) (*TariffChargesGetOutput, error) {
return s.GetPagesWithContext(context.Background(), options)
}
// GetPagesWithContext same as GetPages except it takes a Context
func (s *TariffChargeService) GetPagesWithContext(ctx context.Context, options *TariffChargesGetOptions) (*TariffChargesGetOutput, error) {
options.PageSize = Int(1500)
options.Page = nil
fullResp := TariffChargesGetOutput{}
var lastPage bool
var i int
for !lastPage {
page, err := s.GetWithContext(ctx, options)
if err != nil {
return nil, err
}
fullResp.Count = page.Count
fullResp.Results = append(fullResp.Results, page.Results...)
if page.Next == "" {
lastPage = true
}
i++
options.Page = Int(i)
}
return &fullResp, nil
}