Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add carbonintensity.org.uk provider to CLI #36

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions carbonintensity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"encoding/json"
"net/http"
"time"

gridintensity "github.com/thegreenwebfoundation/grid-intensity-go/api"
)

const ProviderName = "carbonintensity.org.uk"

type ApiOption func(*ApiClient) error

func New(opts ...ApiOption) (gridintensity.Provider, error) {
func New(opts ...ApiOption) (*ApiClient, error) {
a := &ApiClient{}
for _, opt := range opts {
err := opt(a)
Expand Down Expand Up @@ -41,14 +39,25 @@ type ApiClient struct {
}

func (a *ApiClient) GetCarbonIntensity(ctx context.Context, region string) (float64, error) {
latestData, err := a.getLatestCarbonIntensityData(ctx, region)
data, err := a.getCarbonIntensityData(ctx, region)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest in the name was bugging me as I think its redundant.

if err != nil {
return 0, err
}
return latestData.Actual, nil
if data.Intensity == nil {
return 0, ErrNoResponse
}
return data.Intensity.Actual, nil
}

func (a *ApiClient) GetCarbonIntensityData(ctx context.Context, region string) (*IntensityData, error) {
data, err := a.getCarbonIntensityData(ctx, region)
if err != nil {
return nil, err
}
return data, nil
}

func (a *ApiClient) getLatestCarbonIntensityData(ctx context.Context, region string) (*Intensity, error) {
func (a *ApiClient) getCarbonIntensityData(ctx context.Context, region string) (*IntensityData, error) {
if region != "UK" {
return nil, ErrOnlyUK
}
Expand All @@ -74,8 +83,8 @@ func (a *ApiClient) getLatestCarbonIntensityData(ctx context.Context, region str
return nil, err
}

if len(respObj.Data) == 0 || respObj.Data[0].Intensity == nil {
if len(respObj.Data) == 0 {
return nil, ErrNoResponse
}
return respObj.Data[0].Intensity, nil
return &respObj.Data[0], nil
}
2 changes: 2 additions & 0 deletions carbonintensity/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type CarbonIntensityResponse struct {
}

type IntensityData struct {
From string `json:"from"`
To string `json:"to"`
Intensity *Intensity `json:"intensity"`
}

Expand Down
34 changes: 34 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/thegreenwebfoundation/grid-intensity-go/carbonintensity"
"github.com/thegreenwebfoundation/grid-intensity-go/ember"
"github.com/thegreenwebfoundation/grid-intensity-go/watttime"
)
Expand Down Expand Up @@ -63,6 +64,26 @@ func init() {
viper.BindPFlag(region, rootCmd.PersistentFlags().Lookup(region))
}

func getCarbonIntensityOrgUK(ctx context.Context, region string) error {
c, err := carbonintensity.New()
if err != nil {
return fmt.Errorf("could not make provider %v", err)
}

result, err := c.GetCarbonIntensityData(ctx, region)
if err != nil {
return err
}

bytes, err := json.MarshalIndent(result, "", "\t")
if err != nil {
return err
}

fmt.Println(string(bytes))
return nil
}

func getEmberGridIntensityForCountry(countryCode string) error {
result, err := ember.GetGridIntensityForCountry(countryCode)
if err != nil {
Expand Down Expand Up @@ -149,6 +170,19 @@ func runRoot() error {
}

switch providerName {
case carbonintensity.ProviderName:
if regionCode == "" {
regionCode = "UK"
}
if regionCode != "UK" {
return fmt.Errorf("only region UK is supported")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We default the region but also check it is set to UK.

viper.Set(region, regionCode)

err = getCarbonIntensityOrgUK(ctx, regionCode)
if err != nil {
return err
}
case ember.ProviderName:
if regionCode == "" {
regionCode, err = getCountryCode()
Expand Down