-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
53 lines (42 loc) · 948 Bytes
/
client.go
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
package hapifhirgo
import (
"errors"
"net/http"
"os"
"time"
)
const (
defaultTimeout = 10 * time.Second
)
type Client struct {
baseURL string
HTTP *http.Client
}
// ClientOption allows customization of the client.
type ClientOption func(c *Client)
func WithTimeout(t time.Duration) func(c *Client) {
return func(c *Client) {
c.HTTP.Timeout = t
}
}
// NewClientFromEnvVars creates a new client where the needed fields are
// retrieved from the environment variables.
func NewClientFromEnvVars() (*Client, error) {
return NewClient(os.Getenv("HAPI_FHIR_BASE_URL"))
}
// NewClient creates a new hapi fhir api client.
func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
if baseURL == "" {
return nil, errors.New("baseURL is empty")
}
client := &Client{
HTTP: &http.Client{
Timeout: defaultTimeout,
},
baseURL: baseURL,
}
for _, opt := range options {
opt(client)
}
return client, nil
}