Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
108 lines (90 loc) · 2.85 KB
/
Copy pathclient.go
File metadata and controls
108 lines (90 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package bnm
import (
"context"
"fmt"
)
// Option configures a Client.
type Option func(*Client)
// GetRequestFunc defines a function signature for performing HTTP GET requests.
// It takes a context and URL and returns the response body or an error.
type GetRequestFunc func(ctx context.Context, url string) ([]byte, error)
// UnmarshalerFunc defines a function signature for unmarshaling a byte slice
// into a Response object.
type UnmarshalerFunc func([]byte) (Response, error)
// WarnFunc defines a function signature for logging non-critical errors.
type WarnFunc func(error)
// Client is used to fetch exchange rates from the National Bank of Moldova (BNM) API.
type Client struct {
cache Cache
getRequest GetRequestFunc
unmarshaler UnmarshalerFunc
warnError WarnFunc
}
// NewClient creates a new Client instance with optional configuration.
// By default, it uses the standard HTTP client and the default unmarshaler.
//
// Example:
//
// client := NewClient(
// WithCache(myCache),
// WithWarnError(logError),
// )
func NewClient(opts ...Option) *Client {
c := &Client{
getRequest: getRequestWithDefaultClient,
unmarshaler: unmarshalResponse,
}
for _, opt := range opts {
opt(c)
}
return c
}
// WithCache sets a Cache implementation on the Client.
func WithCache(cache Cache) Option {
return func(c *Client) { c.cache = cache }
}
// WithGetRequest sets a custom GetRequestFunc on the Client.
func WithGetRequest(fn GetRequestFunc) Option {
return func(c *Client) { c.getRequest = fn }
}
// WithUnmarshaler sets a custom UnmarshalerFunc on the Client.
func WithUnmarshaler(u UnmarshalerFunc) Option {
return func(c *Client) { c.unmarshaler = u }
}
// WithWarnError sets a WarnFunc on the Client to log non-critical errors.
func WithWarnError(fn WarnFunc) Option {
return func(c *Client) { c.warnError = fn }
}
// Fetch retrieves exchange rates for a given query.
// It first checks the cache (if configured), then fetches from the BNM API,
// and finally stores the result in the cache.
//
// Returns an error if the request fails, if unmarshaling fails, or
// if there is an unexpected cache error.
//
// Example:
//
// resp, err := client.Fetch(ctx, NewQuery(time.Now(), bnm.LANG_EN)
func (c *Client) Fetch(ctx context.Context, query Query) (Response, error) {
if c.cache != nil {
if cache, err := c.cache.Get(ctx, query.ID()); err == nil {
return cache, nil
} else if err != ErrNotFound {
return Response{}, fmt.Errorf("get cache: %w", err)
}
}
data, err := c.getRequest(ctx, query.RequestURL())
if err != nil {
return Response{}, fmt.Errorf("get request: %w", err)
}
res, err := c.unmarshaler(data)
if err != nil {
return Response{}, fmt.Errorf("parse body: %w", err)
}
if c.cache != nil {
if err := c.cache.Set(ctx, query.ID(), res); err != nil {
c.warnError(fmt.Errorf("set cache: %w", err))
}
}
return res, nil
}