api

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package api provides functionality for interacting with the EdgeCom Energy API.

The package implements:

  • Robust HTTP client with timeouts and context support
  • Automatic data conversion and storage
  • Historical data bootstrapping
  • Structured logging
  • Error handling with custom error types

Example:

fetcher := api.NewSeriesFetcher(
    "https://api.example.com/timeseries",
    dbService,
    logger,
)

if err := fetcher.FetchData(ctx, start, end); err != nil {
    log.Printf("Failed to fetch data: %v", err)
    return err
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAPIRequest is returned when there's an error making an API request
	ErrAPIRequest = errors.New("error making API request")
	// ErrAPIStatus is returned when the API returns a non-200 status code
	ErrAPIStatus = errors.New("error status from API")
)

Error types for API-related errors

Functions

This section is empty.

Types

type SeriesFetcher

type SeriesFetcher struct {
	// contains filtered or unexported fields
}

SeriesFetcher is a struct that fetches data from the EdgeCom Energy API and stores it in a database.

func NewSeriesFetcher

func NewSeriesFetcher(apiURL string, dbService database.TimeSeriesRepository, logger *logrus.Logger) *SeriesFetcher

NewSeriesFetcher creates a new SeriesFetcher instance. Parameters:

  • apiURL: The base URL for the EdgeCom API
  • dbService: Repository for storing time series data
  • logger: Structured logger for operation tracking

Returns:

  • A configured SeriesFetcher instance ready for use

func (*SeriesFetcher) BootstrapHistoricalData

func (f *SeriesFetcher) BootstrapHistoricalData(ctx context.Context) error

BootstrapHistoricalData initializes the database with historical data. It attempts to fetch the last 2 years of data, with a fallback to the last 24 hours if the full historical fetch fails.

The method implements a graceful degradation strategy:

  1. Attempts to fetch 2 years of historical data
  2. On failure, falls back to last 24 hours
  3. Logs all operations and failures

func (*SeriesFetcher) FetchData

func (f *SeriesFetcher) FetchData(ctx context.Context, start, end time.Time) error

FetchData fetches data from the EdgeCom Energy API for a given time range and stores it in the database. The method:

  1. Constructs the API request with proper formatting
  2. Executes the request with timeout
  3. Processes the response
  4. Stores the data in the database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL