mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
feat: initial implementation of command split
This commit is contained in:
parent
522ddb985d
commit
835b678e75
8 changed files with 181 additions and 63 deletions
|
|
@ -9,8 +9,6 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Option[T Client] func(client *T)
|
||||
|
|
@ -38,47 +36,47 @@ func NewClient[T Client](opts ...func(T)) T {
|
|||
return *client
|
||||
}
|
||||
|
||||
func WithCertPool[T Client](certPool *x509.CertPool) func(T) {
|
||||
func WithCertPool(client Client, certPool *x509.CertPool) error {
|
||||
// make sure we have a valid cert pool
|
||||
if certPool == nil {
|
||||
return func(client T) {}
|
||||
return fmt.Errorf("invalid cert pool")
|
||||
}
|
||||
return func(client T) {
|
||||
// make sure that we can access the internal client
|
||||
if client.GetInternalClient() == nil {
|
||||
log.Warn().Any("client", client.GetInternalClient()).Msg("invalid internal HTTP client ()")
|
||||
return
|
||||
}
|
||||
client.GetInternalClient().Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: certPool,
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
DisableKeepAlives: true,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 120 * time.Second,
|
||||
KeepAlive: 120 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 120 * time.Second,
|
||||
ResponseHeaderTimeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
// make sure that we can access the internal client
|
||||
internalClient := client.GetInternalClient()
|
||||
if internalClient == nil {
|
||||
return fmt.Errorf("invalid HTTP client")
|
||||
}
|
||||
internalClient.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: certPool,
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
DisableKeepAlives: true,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 120 * time.Second,
|
||||
KeepAlive: 120 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 120 * time.Second,
|
||||
ResponseHeaderTimeout: 120 * time.Second,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WithSecureTLS[T Client](certPath string) func(T) {
|
||||
func WithSecureTLS(client Client, certPath string) error {
|
||||
cacert, err := os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
return func(client T) {}
|
||||
return fmt.Errorf("failed to read certificate from path '%s': %v", certPath, err)
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
return WithCertPool[T](certPool)
|
||||
return WithCertPool(client, 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 *MagellanClient) Post(url string, data map[string]any, header HTTPHeader) (*http.Response, HTTPBody, error) {
|
||||
func (c *DefaultClient) Post(url string, data map[string]any, header HTTPHeader) (*http.Response, HTTPBody, error) {
|
||||
// serialize data into byte array
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
type MagellanClient struct {
|
||||
type DefaultClient struct {
|
||||
*http.Client
|
||||
}
|
||||
|
||||
func (c *MagellanClient) Name() string {
|
||||
func (c *DefaultClient) Name() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
|
|
@ -18,7 +18,7 @@ func (c *MagellanClient) Name() string {
|
|||
// the first argument with no data processing or manipulation. The function sends
|
||||
// the data to a set callback URL (which may be changed to use a configurable value
|
||||
// instead).
|
||||
func (c *MagellanClient) Add(data HTTPBody, headers HTTPHeader) error {
|
||||
func (c *DefaultClient) Add(data HTTPBody, headers HTTPHeader) error {
|
||||
if data == nil {
|
||||
return fmt.Errorf("no data found")
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ func (c *MagellanClient) Add(data HTTPBody, headers HTTPHeader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (c *MagellanClient) Update(data HTTPBody, headers HTTPHeader) error {
|
||||
func (c *DefaultClient) Update(data HTTPBody, headers HTTPHeader) error {
|
||||
if data == nil {
|
||||
return fmt.Errorf("no data found")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue