-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
100 lines (84 loc) · 2.98 KB
/
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
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
// Package infobip provides a client library
// for interacting with the Infobip API.
// https://www.infobip.com/docs/api
package infobip
import (
"errors"
"net/http"
"net/url"
"os"
"github.com/infobip-community/infobip-api-go-sdk/v3/internal"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/email"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/mms"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/rcs"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/sms"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/webrtc"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/whatsapp"
)
// Client is the entrypoint to all Infobip channels.
type Client struct {
apiKey string
baseURL string
httpClient http.Client
WhatsApp whatsapp.WhatsApp
MMS mms.MMS
Email email.Email
SMS sms.SMS
WebRTC webrtc.WebRTC
RCS rcs.RCS
}
// NewClientFromEnv returns a client object using the credentials from the environment.
// If a client is not provided using options, a default one is created.
func NewClientFromEnv(options ...func(*Client)) (Client, error) {
if os.Getenv("IB_BASE_URL") == "" {
return Client{}, errors.New("IB_BASE_URL environment variable is not set")
}
if os.Getenv("IB_API_KEY") == "" {
return Client{}, errors.New("IB_API_KEY environment variable is not set")
}
return NewClient(os.Getenv("IB_BASE_URL"), os.Getenv("IB_API_KEY"), options...)
}
// NewClient returns a client object using the provided baseURL and apiKey.
// If a client is not provided using options, a default one is created.
func NewClient(baseURL string, apiKey string, options ...func(*Client)) (Client, error) {
baseURL, err := validateURL(baseURL)
if err != nil {
return Client{}, err
}
c := Client{baseURL: baseURL, apiKey: apiKey, httpClient: http.Client{}}
for _, opt := range options {
opt(&c)
}
c.WhatsApp = &whatsapp.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
c.MMS = &mms.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
c.Email = &email.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
c.SMS = &sms.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
c.WebRTC = &webrtc.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
c.RCS = &rcs.Channel{
ReqHandler: internal.HTTPHandler{APIKey: apiKey, BaseURL: baseURL, HTTPClient: c.httpClient},
}
return c, nil
}
func validateURL(baseURL string) (string, error) {
_, err := url.ParseRequestURI(baseURL)
if err != nil {
baseURL = "https://" + baseURL
_, err = url.ParseRequestURI(baseURL)
}
return baseURL, err
}
func WithHTTPClient(httpClient http.Client) func(*Client) {
return func(c *Client) {
c.httpClient = httpClient
}
}