go-meteologix/meteologix.go

149 lines
3.6 KiB
Go
Raw Permalink Normal View History

// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
2023-05-12 12:44:27 +02:00
//
// SPDX-License-Identifier: MIT
package meteologix
import (
"fmt"
"runtime"
)
2023-05-15 16:02:04 +02:00
2023-05-12 12:44:27 +02:00
const (
// APIBaseURL represents the base URL for the Meteologix API.
//
// We currently support v02 of the API.
APIBaseURL = "https://api.kachelmannwetter.com/v02"
// APIMockURL represents the mocked API URL for testing purposes
APIMockURL = "https://go-meteologix-mock.neessen.dev/v02"
2023-05-12 12:44:27 +02:00
// DefaultAcceptLang is the default language set for API requests
DefaultAcceptLang = "en"
)
// DefaultUserAgent is the default User-Agent presented by the HTTPClient
var DefaultUserAgent = fmt.Sprintf("go-meteologix/fv%s (%s; %s; "+
"+https://src.neessen.cloud/wneessen/go-meteologix)", VERSION, runtime.GOOS,
runtime.Version())
2023-05-12 12:44:27 +02:00
// Client represents the Meteologix API Client
type Client struct {
// config represents the Config for the Client
config *Config
// httpClient references the HTTPClient of the Server
httpClient *HTTPClient
2023-05-12 12:44:27 +02:00
}
// Config represents the Client configuration settings
type Config struct {
// apiKey holds the (optional) API key for the API user authentication
apiKey string
2023-05-15 16:02:04 +02:00
// apiURL holds the base URL for the API. This is configurable so we
// can test against our mock API.
apiURL string
// acceptLang hold the (optional) accept-language tag
acceptLang string
// authPass holds the (optional) passowrd for the API user authentication
authPass string
// authUser holds the (optional) username for the API user authentication
authUser string
// bearerToken holds the (optional) bearer token for the API authentication
bearerToken string
// userAgent represents an alternative User-Agent HTTP header string
userAgent string
2023-05-12 12:44:27 +02:00
}
// Option represents a function that is used for setting/overriding Config options
type Option func(*Config)
// New returns a new Meteologix API Client
func New(o ...Option) *Client {
co := &Config{}
2023-05-15 16:02:04 +02:00
co.apiURL = APIBaseURL
co.acceptLang = DefaultAcceptLang
co.userAgent = DefaultUserAgent
2023-05-12 12:44:27 +02:00
// Set/override Config options
for _, opt := range o {
if opt == nil {
continue
}
opt(co)
}
return &Client{
config: co,
httpClient: NewHTTPClient(co),
2023-05-12 12:44:27 +02:00
}
}
// WithAcceptLanguage sets the HTTP Accept-Lanauge header of the HTTP client
//
// The provided string needs to conform the HTTP Accept-Language header format
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
func WithAcceptLanguage(l string) Option {
if l == "" {
return nil
}
return func(co *Config) {
co.acceptLang = l
2023-05-12 12:44:27 +02:00
}
}
// WithAPIKey sets the API Key for user authentication of the HTTP client
func WithAPIKey(k string) Option {
if k == "" {
return nil
}
return func(co *Config) {
co.apiKey = k
2023-05-12 12:44:27 +02:00
}
}
// WithBearerToken uses a bearer token for the client authentication of the
// HTTP client
func WithBearerToken(t string) Option {
if t == "" {
return nil
}
return func(co *Config) {
co.bearerToken = t
}
}
// WithPassword sets the HTTP Basic auth authPass for the HTTP client
2023-05-12 12:44:27 +02:00
func WithPassword(p string) Option {
if p == "" {
return nil
}
return func(co *Config) {
co.authPass = p
2023-05-12 12:44:27 +02:00
}
}
// WithUserAgent sets a custom user agent string for the HTTP client
func WithUserAgent(a string) Option {
if a == "" {
return nil
}
return func(co *Config) {
co.userAgent = a
2023-05-12 12:44:27 +02:00
}
}
// WithUsername sets the HTTP Basic auth username for the HTTP client
func WithUsername(u string) Option {
if u == "" {
return nil
}
return func(co *Config) {
co.authUser = u
2023-05-12 12:44:27 +02:00
}
}
2023-05-15 16:02:04 +02:00
// withMockAPI sets the API URL to our mock API for testing
func withMockAPI() Option {
return func(co *Config) {
co.apiURL = APIMockURL
}
}