Skip to content

Commit

Permalink
Fixed bug with ListProducts (#7)
Browse files Browse the repository at this point in the history
* Added Go 1.16 github actions

* Added Go 1.16 to tests

* Fixed a bug with listing products
  • Loading branch information
FileGo authored May 10, 2021
1 parent dea7fd0 commit 2e0d66f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 15 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ jobs:
- uses: actions/checkout@v2

- name: test
run: go test ./...
- name: race
run: go test -race ./...
run: go test -race -v ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/FileGo/octopusenergyapi

go 1.15
go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
9 changes: 3 additions & 6 deletions octopusapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ func (c *Client) listProductsPage(URL string) ([]Product, string, error) {
return nil, "", errors.Errorf("error retrieving: %v", err)
}

return data.Results, data.Next, nil
return data.Results, strings.TrimPrefix(data.Next, baseURL), nil
}

// ListProducts returns a list of energy products
// https://developer.octopus.energy/docs/api/#list-products
func (c *Client) ListProducts() ([]Product, error) {
var products []Product

URL := fmt.Sprintf("%s/products/", c.URL)
URL := "/products/"

for {
pageProducts, url, err := c.listProductsPage(URL)
Expand All @@ -196,10 +196,7 @@ func (c *Client) ListProducts() ([]Product, error) {
return nil, errors.Errorf("error retrieving products page: %v", err)
}

for _, product := range pageProducts {
products = append(products, product)
}

products = append(products, pageProducts...)
if URL == "" {
break
}
Expand Down
6 changes: 2 additions & 4 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package octopusenergyapi

import (
"net/http"
"regexp"
"time"
)

Expand Down Expand Up @@ -58,9 +57,8 @@ type GridSupplyPoint struct {

// Client represents a Client to be used with the API
type Client struct {
httpClient *http.Client
URL string
postcodeRegex *regexp.Regexp
httpClient *http.Client
URL string
}

// MeterPoint represents a meter point
Expand Down

0 comments on commit 2e0d66f

Please sign in to comment.