Skip to content

Commit

Permalink
Merge pull request #240 from deploymenttheory/dev-jl-version2
Browse files Browse the repository at this point in the history
Dev jl version2
  • Loading branch information
thejoeker12 authored Jun 17, 2024
2 parents 7a57529 + 2335731 commit a0db942
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 73 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/deploymenttheory/go-api-http-client

go 1.22.2
go 1.22.4

require (
github.com/antchfx/xmlquery v1.4.0
Expand All @@ -14,7 +14,9 @@ require (
github.com/antchfx/xpath v1.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50 // indirect

github.com/deploymenttheory/go-api-sdk-jamfpro v1.7.2-0.20240610152250-5b91816c5437 // indirect

github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50 h1:wGwI8cpfDAnnMYAX5cJE/TJaIb/oQN4Nyzrfrtcj1Og=
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50/go.mod h1:57h5+i4v167pAEg6fLwJ0r0obHguEh15v2fNasqUl7Y=
github.com/deploymenttheory/go-api-sdk-jamfpro v1.7.2-0.20240610152250-5b91816c5437 h1:ib64f8eoNEyh2xoTLDZniTqY0bFKshtDuc9e+WiDLuU=
github.com/deploymenttheory/go-api-sdk-jamfpro v1.7.2-0.20240610152250-5b91816c5437/go.mod h1:BI17RMj/tRij7Z+XPoruF0l3bDmDRG3JcVglkh7Ul5k=


github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
19 changes: 6 additions & 13 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ type Client struct {

// Options/Variables for Client
type ClientConfig struct {
Integration APIIntegration

LogLevel string
LogOutputFormat string // Output format of the logs. Use "pretty" for JSON format, "console" for human-readable format
LogConsoleSeparator string
ExportLogs bool
LogExportPath string
HideSensitiveData bool
Integration APIIntegration
HideSensitiveData bool

// CookieJarEnabled bool
CustomCookies []*http.Cookie
Expand All @@ -54,7 +48,7 @@ type ClientConfig struct {
EnableDynamicRateLimiting bool
CustomTimeout time.Duration
TokenRefreshBufferPeriod time.Duration
TotalRetryDuration time.Duration
TotalRetryDuration time.Duration // TODO do we need this now it's in the integration?
FollowRedirects bool
MaxRedirects int
}
Expand Down Expand Up @@ -97,13 +91,12 @@ func BuildClient(config ClientConfig, populateDefaultValues bool, log logger.Log
Concurrency: concurrencyHandler,
}

client.parseCustomCookies(config.CustomCookies)
if len(client.config.CustomCookies) > 0 {
client.parseCustomCookies(config.CustomCookies)
}

log.Debug("New API client initialized",
zap.String("Authentication Method", (*client.Integration).GetAuthMethodDescriptor()),
zap.String("Logging Level", config.LogLevel),
zap.String("Log Encoding Format", config.LogOutputFormat),
zap.String("Log Separator", config.LogConsoleSeparator),
zap.Bool("Hide Sensitive Data In Logs", config.HideSensitiveData),
zap.Int("Max Retry Attempts", config.MaxRetryAttempts),
zap.Bool("Enable Dynamic Rate Limiting", config.EnableDynamicRateLimiting),
Expand Down
55 changes: 1 addition & 54 deletions httpclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package httpclient

import (
"errors"
"fmt"
"slices"
"time"
)

Expand Down Expand Up @@ -40,7 +38,6 @@ func LoadConfigFromEnv() (*ClientConfig, error) {

// TODO Review validateClientConfig
func validateClientConfig(config ClientConfig, populateDefaults bool) error {
var err error

if populateDefaults {
SetDefaultValuesClientConfig(&config)
Expand All @@ -51,38 +48,8 @@ func validateClientConfig(config ClientConfig, populateDefaults bool) error {
return errors.New("no api integration supplied, please see documentation")
}

// Level
validLogLevels := []string{
"LogLevelDebug",
"LogLevelInfo",
"LogLevelWarn",
"LogLevelError",
"LogLevelPanic",
"LogLevelFatal",
}
if !slices.Contains(validLogLevels, config.LogLevel) {
return fmt.Errorf("invalid log level: %s", config.LogLevel)
}

validLogFormats := []string{
"json",
"pretty",
}

if !slices.Contains(validLogFormats, config.LogOutputFormat) {
return fmt.Errorf("invalid log output format: %s", config.LogOutputFormat)
}

// Log Export Path
if config.ExportLogs {
_, err = validateFilePath(config.LogExportPath)
if err != nil {
return err
}
}

if config.MaxRetryAttempts < 0 {
return errors.New("Max retry cannot be less than 0")
return errors.New("max retry cannot be less than 0")
}

if config.MaxConcurrentRequests < 1 {
Expand Down Expand Up @@ -112,26 +79,6 @@ func validateClientConfig(config ClientConfig, populateDefaults bool) error {

func SetDefaultValuesClientConfig(config *ClientConfig) {

if config.LogLevel == "" {
config.LogLevel = DefaultLogLevelString
}

if config.LogOutputFormat == "" {
config.LogOutputFormat = DefaultLogOutputFormatString
}

if config.LogConsoleSeparator == "" {
config.LogConsoleSeparator = DefaultLogConsoleSeparator
}

if !config.ExportLogs {
config.ExportLogs = DefaultExportLogs
}

if config.LogExportPath == "" {
config.LogExportPath = DefaultLogExportPath
}

if !config.HideSensitiveData {
config.HideSensitiveData = DefaultHideSensitiveData
}
Expand Down
1 change: 1 addition & 0 deletions httpclient/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ type APIIntegration interface {
PrepRequestParamsAndAuth(req *http.Request) error
PrepRequestBody(body interface{}, method string, endpoint string) ([]byte, error)
MarshalMultipartRequest(fields map[string]string, files map[string]string) ([]byte, string, error)
GetSessionCookies() (*[]http.Cookie, error)
}
6 changes: 3 additions & 3 deletions httpclient/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func validateClientSecret(clientSecret string) error {
}

if matched, _ := regexp.MatchString(`[a-z]`, clientSecret); !matched {
return errors.New("client secret must contain at least one lowercase letter.")
return errors.New("client secret must contain at least one lowercase letter")
}

if matched, _ := regexp.MatchString(`[A-Z]`, clientSecret); !matched {
return errors.New("client secret must contain at least one uppercase letter.")
return errors.New("client secret must contain at least one uppercase letter")
}

if matched, _ := regexp.MatchString(`\d`, clientSecret); !matched {
return errors.New("client secret must contain at least one digit.")
return errors.New("client secret must contain at least one digit")
}

return nil
Expand Down

0 comments on commit a0db942

Please sign in to comment.