-
Notifications
You must be signed in to change notification settings - Fork 0
/
secured_http_client.go
59 lines (50 loc) · 2.11 KB
/
secured_http_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
package client
import (
"context"
"github.com/golibs-starter/golib/web/client"
"net/http"
)
type SecuredHttpClient struct {
client client.ContextualHttpClient
properties *SecurityProperties
}
func NewSecuredHttpClient(
client client.ContextualHttpClient,
properties *SecurityProperties,
) *SecuredHttpClient {
return &SecuredHttpClient{
client: client,
properties: properties,
}
}
func (s *SecuredHttpClient) Get(ctx context.Context, url string, result interface{},
options ...client.RequestOption) (*client.HttpResponse, error) {
return s.Request(ctx, http.MethodGet, url, nil, result, options...)
}
func (s *SecuredHttpClient) Post(ctx context.Context, url string, body interface{}, result interface{},
options ...client.RequestOption) (*client.HttpResponse, error) {
return s.Request(ctx, http.MethodPost, url, body, result, options...)
}
func (s *SecuredHttpClient) Put(ctx context.Context, url string, body interface{}, result interface{},
options ...client.RequestOption) (*client.HttpResponse, error) {
return s.Request(ctx, http.MethodPut, url, body, result, options...)
}
func (s *SecuredHttpClient) Patch(ctx context.Context, url string, body interface{}, result interface{},
options ...client.RequestOption) (*client.HttpResponse, error) {
return s.Request(ctx, http.MethodPatch, url, body, result, options...)
}
func (s *SecuredHttpClient) Delete(ctx context.Context, url string, body interface{}, result interface{},
options ...client.RequestOption) (*client.HttpResponse, error) {
return s.Request(ctx, http.MethodDelete, url, body, result, options...)
}
func (s *SecuredHttpClient) Request(ctx context.Context, method string, url string, body interface{},
result interface{}, options ...client.RequestOption) (*client.HttpResponse, error) {
httpOpts := make([]client.RequestOption, 0)
for _, auth := range s.properties.BasicAuth {
if auth.UrlRegexp() != nil && auth.UrlRegexp().MatchString(url) {
httpOpts = append(httpOpts, client.WithBasicAuth(auth.Username, auth.Password))
}
}
httpOpts = append(httpOpts, options...)
return s.client.Request(ctx, method, url, body, result, httpOpts...)
}