diff --git a/internal/app/api.go b/internal/app/api.go index e348186..c1ea157 100644 --- a/internal/app/api.go +++ b/internal/app/api.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "os" "strings" "time" ) @@ -35,9 +36,23 @@ func Auth(config SmartHubConfig) (string, error) { if err != nil { return "", err } - decoder := json.NewDecoder(resp.Body) + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Println("Error: failed to close response body") + } + }(resp.Body) + reader := resp.Body.(io.Reader) + if debug { + _, _ = fmt.Fprintln(os.Stderr, "\nDEBUG: Response from auth endpoint:") + reader = io.TeeReader(reader, os.Stderr) + } + decoder := json.NewDecoder(reader) oauth := &OAuth{} err = decoder.Decode(oauth) + if debug { + _, _ = fmt.Fprintf(os.Stderr, "\n\n") + } if err != nil { return "", err } diff --git a/internal/app/main.go b/internal/app/main.go index 8ac6ed4..6f094e3 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -37,13 +37,17 @@ type Config struct { InfluxDB InfluxConfig `yaml:"influxdb"` } +var debug bool + // Main runs the program. func Main() error { configFlag := flag.String("config", "config.yaml", "Config file") startFlag := flag.String("start", "", "Start date of period to extract from electric co.") endFlag := flag.String("end", "", "End date of period to extract from electric co.") + debugFlag := flag.Bool("debug", false, "Enable to print out verbose debugging logs.") flag.Parse() + debug = *debugFlag // read config file, err := os.ReadFile(*configFlag) if err != nil { diff --git a/internal/app/parser.go b/internal/app/parser.go index 750af9b..6b1076d 100644 --- a/internal/app/parser.go +++ b/internal/app/parser.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "os" "time" ) @@ -64,22 +65,31 @@ func (t *RetryableError) Error() string { // ParseReader parses the json response received in FetchData from the SmartHub poll api. // It can return a normal error, a RetryableError, or parsed ElectricUsage. -func ParseReader(reader io.ReadCloser) ([]ElectricUsage, error) { +func ParseReader(readCloser io.ReadCloser) ([]ElectricUsage, error) { defer func() { - if err := reader.Close(); err != nil { - panic(err) + if err := readCloser.Close(); err != nil { + fmt.Println("Error: failed to close response body") } }() + reader := readCloser.(io.Reader) + if debug { + _, _ = fmt.Fprintln(os.Stderr, "\nDEBUG: Response from poll endpoint:") + reader = io.TeeReader(readCloser, os.Stderr) + } resp := &Response{} err := json.NewDecoder(reader).Decode(resp) if err != nil { return nil, err } + if debug { + _, _ = fmt.Fprintln(os.Stderr, "\n\nDEBUG: Parsed data from poll endpoint:") + _, _ = fmt.Fprintf(os.Stderr, "%+v\n\n", resp) + } if resp.Status != "COMPLETE" { fmt.Println("Data not ready, retrying...") return nil, NewRetryableError("data processing not complete") } - fmt.Println("Data received, parsing...") + fmt.Println("Data received, transforming...") datas, ok := resp.Data["ELECTRIC"] if !ok { return nil, errors.New("no ELECTRIC key")