mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Refactored how clients work to reduce hard-coded dependencies
This commit is contained in:
parent
8e59885f55
commit
c5a348562b
3 changed files with 100 additions and 57 deletions
|
|
@ -13,38 +13,36 @@ import (
|
|||
"github.com/OpenCHAMI/magellan/internal/util"
|
||||
)
|
||||
|
||||
type Option func(*Client)
|
||||
type Option[T Client] func(client T)
|
||||
|
||||
// The 'Client' struct is a wrapper around the default http.Client
|
||||
// that provides an extended API to work with functional options.
|
||||
// It also provides functions that work with `collect` data.
|
||||
type Client struct {
|
||||
*http.Client
|
||||
type Client interface {
|
||||
Name() string
|
||||
GetClient() *http.Client
|
||||
RootEndpoint(endpoint string) string
|
||||
|
||||
// functions needed to make request
|
||||
Add(data util.HTTPBody, headers util.HTTPHeader) error
|
||||
Update(data util.HTTPBody, headers util.HTTPHeader) error
|
||||
}
|
||||
|
||||
// NewClient() creates a new client
|
||||
func NewClient(opts ...Option) *Client {
|
||||
client := &Client{
|
||||
Client: http.DefaultClient,
|
||||
}
|
||||
func NewClient[T Client](opts ...func(T)) T {
|
||||
client := new(T)
|
||||
for _, opt := range opts {
|
||||
opt(client)
|
||||
opt(*client)
|
||||
}
|
||||
return client
|
||||
return *client
|
||||
}
|
||||
|
||||
func WithHttpClient(httpClient *http.Client) Option {
|
||||
return func(c *Client) {
|
||||
c.Client = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
func WithCertPool(certPool *x509.CertPool) Option {
|
||||
func WithCertPool[T Client](certPool *x509.CertPool) func(T) {
|
||||
if certPool == nil {
|
||||
return func(c *Client) {}
|
||||
return func(client T) {}
|
||||
}
|
||||
return func(c *Client) {
|
||||
c.Client.Transport = &http.Transport{
|
||||
return func(client T) {
|
||||
client.GetClient().Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: certPool,
|
||||
InsecureSkipVerify: true,
|
||||
|
|
@ -60,20 +58,20 @@ func WithCertPool(certPool *x509.CertPool) Option {
|
|||
}
|
||||
}
|
||||
|
||||
func WithSecureTLS(certPath string) Option {
|
||||
func WithSecureTLS[T Client](certPath string) func(T) {
|
||||
cacert, err := os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
return func(c *Client) {}
|
||||
return func(client T) {}
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
return WithCertPool(certPool)
|
||||
return WithCertPool[T](certPool)
|
||||
}
|
||||
|
||||
// Post() is a simplified wrapper function that packages all of the
|
||||
// that marshals a mapper into a JSON-formatted byte array, and then performs
|
||||
// a request to the specified URL.
|
||||
func (c *Client) Post(url string, data map[string]any, header util.HTTPHeader) (*http.Response, util.HTTPBody, error) {
|
||||
func (c *MagellanClient) Post(url string, data map[string]any, header util.HTTPHeader) (*http.Response, util.HTTPBody, error) {
|
||||
// serialize data into byte array
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
|
|
@ -81,7 +79,3 @@ func (c *Client) Post(url string, data map[string]any, header util.HTTPHeader) (
|
|||
}
|
||||
return util.MakeRequest(c.Client, url, http.MethodPost, body, header)
|
||||
}
|
||||
|
||||
func (c *Client) MakeRequest(url string, method string, body util.HTTPBody, header util.HTTPHeader) (*http.Response, util.HTTPBody, error) {
|
||||
return util.MakeRequest(c.Client, url, method, body, header)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue