2023-05-21 11:46:23 +02:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
2023-05-12 12:44:27 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package meteologix
|
|
|
|
|
2023-05-15 16:12:28 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
)
|
2023-05-15 16:02:04 +02:00
|
|
|
|
2023-05-12 12:44:27 +02:00
|
|
|
const (
|
2023-05-15 16:12:28 +02:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2023-05-15 16:12:28 +02:00
|
|
|
// DefaultUserAgent is the default User-Agent presented by the HTTPClient
|
2023-05-24 22:00:47 +02:00
|
|
|
var DefaultUserAgent = fmt.Sprintf("go-meteologix/fv%s (%s; %s; "+
|
2024-02-11 15:24:46 +01:00
|
|
|
"+https://src.neessen.cloud/wneessen/go-meteologix)", VERSION, runtime.GOOS,
|
2023-05-15 16:12:28 +02:00
|
|
|
runtime.Version())
|
|
|
|
|
2023-05-12 12:44:27 +02:00
|
|
|
// Client represents the Meteologix API Client
|
|
|
|
type Client struct {
|
2023-05-13 13:18:47 +02:00
|
|
|
// 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 {
|
2023-05-13 13:18:47 +02:00
|
|
|
// 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
|
2023-05-13 13:18:47 +02:00
|
|
|
// 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
|
2023-08-02 16:08:38 +02:00
|
|
|
// bearerToken holds the (optional) bearer token for the API authentication
|
|
|
|
bearerToken string
|
2023-05-13 13:18:47 +02:00
|
|
|
// 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
|
2023-05-13 13:18:47 +02:00
|
|
|
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{
|
2023-05-13 13:18:47 +02:00
|
|
|
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) {
|
2023-05-13 13:18:47 +02:00
|
|
|
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) {
|
2023-05-13 13:18:47 +02:00
|
|
|
co.apiKey = k
|
2023-05-12 12:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:08:38 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 13:18:47 +02:00
|
|
|
// 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) {
|
2023-05-13 13:18:47 +02:00
|
|
|
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) {
|
2023-05-13 13:18:47 +02:00
|
|
|
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) {
|
2023-05-13 13:18:47 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|