Basic working client

This commit is contained in:
Ryan McGuire 2024-11-19 17:00:05 -05:00
parent e72e167a3e
commit 3655f7eae8
6 changed files with 91 additions and 1133 deletions

15
TODO.md Normal file
View File

@ -0,0 +1,15 @@
# EIA API GO TODO
## Makefile
- Add sed substitutions for manual fixes and add curl back to retrieve up-to-date yaml
- Ref swaps
- Int to string swaps
- *[]string to *string swaps
## EIA Client Tool
- Add reflection and autocomplete funcs
- Add reflection for listing API Routes under GetV2<stuff here>
- Add reflection for listing route parameters once selected
- Add reflection to retrieve Facets for selected route+params from autocomplete funcs

File diff suppressed because it is too large Load Diff

View File

@ -2,9 +2,11 @@ package list
import (
"github.com/spf13/cobra"
"k8s.io/utils/ptr"
eiaapi "gitea.libretechconsulting.com/50W/eia-api-go/api"
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
"gitea.libretechconsulting.com/50W/eia-api-go/pkg/eia"
)
var ListCmd = &cobra.Command{
@ -23,13 +25,56 @@ func runListCmd(cmd *cobra.Command, _ []string) {
logger.Fatal().Err(err).Send()
}
// Facets
ctx, cncl := util.RequestCtx(cmd)
defer cncl()
resp, err := client.GetV2AeoRoute1DataWithResponse(ctx, "2023", &eiaapi.GetV2AeoRoute1DataParams{})
facets, err := client.GetV2AeoRoute1FacetWithResponse(ctx, "2023")
if err != nil {
logger.Fatal().Err(err).Send()
}
logger.Debug().Any("facets", facets.JSON200).Send()
// Facet
for _, option := range *facets.JSON200.Response.FacetOptions {
ctx, cncl = util.RequestCtx(cmd)
defer cncl()
facet, err := client.GetV2AeoRoute1FacetFacetIdWithResponse(ctx, "2023", option)
if err != nil {
logger.Err(err).Send()
return
}
logger.Debug().Str("facet", option).Any("options", facet.JSON200.Response.Facets).Send()
}
ctx, cncl = util.RequestCtx(cmd)
defer cncl()
resp, err := client.GetV2AeoRoute1DataWithResponse(ctx, "2023", &eiaapi.GetV2AeoRoute1DataParams{
Start: ptr.To("2023"),
End: ptr.To("2023"),
Facets: eia.NewFacets(
&eia.Facet{
Name: "regionId",
Data: "5-4",
},
&eia.Facet{
Name: "seriesId",
Data: "gen_NA_elep_NA_nuc_NA_mcc_blnkwh",
},
&eia.Facet{
Name: "seriesId",
Data: "cap_NA_elep_NA_nup_NA_mcc_gw",
},
&eia.Facet{
Name: "scenario",
Data: "highmacro",
},
),
})
if err != nil {
logger.Error().Bytes("body", resp.Body)
logger.Fatal().Err(err).Send()
}
logger.Info().Str("resp", string(resp.Body)).Send()
// body, err := io.ReadAll(resp.Body)
// defer resp.Body.Close()
logger.Debug().Any("body", resp.JSON200).Send()
}

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/oapi-codegen/runtime v1.1.1
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
)
require (

2
go.sum
View File

@ -205,3 +205,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

View File

@ -2,6 +2,7 @@ package eia
import (
"context"
"fmt"
"net/url"
"slices"
"time"
@ -29,6 +30,7 @@ type Client struct {
ctx context.Context
apiKey string
healthCheckTimeout time.Duration
*eiaapi.Client
*eiaapi.ClientWithResponses
}
@ -41,6 +43,19 @@ type ClientOpts struct {
HealthCheckTimeout *time.Duration // Timeout for Ping() function, default is 10s
}
type Facet struct {
Name string
Data interface{}
}
func NewFacets(facets ...*Facet) *eiaapi.Facets {
newFacets := make(map[string]interface{}, len(facets))
for _, f := range facets {
newFacets[fmt.Sprintf("facets[%s][]", f.Name)] = f.Data
}
return &newFacets
}
func NewClient(opts *ClientOpts) (*Client, error) {
baseURL := defaultBaseURL
if opts.BaseURL != nil {
@ -80,7 +95,12 @@ func NewClient(opts *ClientOpts) (*Client, error) {
eiaapi.WithRequestEditorFn(newLoggingMiddleware(opts.Logger, logLevel)))
}
client, err := eiaapi.NewClientWithResponses(baseURL, slices.Clip(middlewares)...)
client, err := eiaapi.NewClient(baseURL, slices.Clip(middlewares)...)
if err != nil {
return nil, err
}
clientWithResponses, err := eiaapi.NewClientWithResponses(baseURL, slices.Clip(middlewares)...)
if err != nil {
return nil, err
}
@ -89,6 +109,7 @@ func NewClient(opts *ClientOpts) (*Client, error) {
apiKey: opts.APIKey,
ctx: opts.Context,
healthCheckTimeout: hcTimeout,
ClientWithResponses: client,
Client: client,
ClientWithResponses: clientWithResponses,
}, nil
}