Skip to content

Commit

Permalink
Merge pull request #2 from kenchan0130/support-default-content-type
Browse files Browse the repository at this point in the history
Support default content type for base client
  • Loading branch information
kenchan0130 committed May 9, 2023
2 parents 25127a8 + b4fd0d7 commit 66f9cb5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions classic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewClient(serverURL string) (*Client, error) {
u.Path = path.Join(u.Path, apiEndpointPath)

client := jamf.NewBaseClient(u)
client.DefaultContentType = "application/xml; charset=utf-8"

c := &Client{
BaseClient: client,
Expand Down
1 change: 1 addition & 0 deletions informal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewClient(serverURL string, username string, password string) (*Client, err
}

client := jamf.NewBaseClient(u)
client.DefaultContentType = "application/json; charset=utf-8"

c := &Client{
BaseClient: client,
Expand Down
41 changes: 24 additions & 17 deletions jamf/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ValidStatusFunc func(*http.Response) bool
type HttpRequestInput interface {
GetRequestMiddlewareFunc() RequestMiddlewareFunc
GetConsistencyFailureFunc() ConsistencyFailureFunc
GetContentType() string
GetContentType(defaultType string) string
GetValidStatusCodes() []int
GetValidStatusFunc() ValidStatusFunc
}
Expand All @@ -48,6 +48,7 @@ type BaseClient struct {
BaseURL *url.URL
AuthorizationToken *string
DisableRetries bool
DefaultContentType string

// HttpClient is the underlying http.Client, which by default uses a retryable client
HttpClient *http.Client
Expand Down Expand Up @@ -87,7 +88,9 @@ func (c *BaseClient) buildUri(uri Uri) string {
func (c *BaseClient) performRequest(req *http.Request, input HttpRequestInput) (*http.Response, int, error) {
var status int

req.Header.Add("Content-Type", input.GetContentType())
if contentType := input.GetContentType(c.DefaultContentType); contentType != "" {
req.Header.Add("Content-Type", input.GetContentType(c.DefaultContentType))
}
if c.AuthorizationToken != nil {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *c.AuthorizationToken))
}
Expand Down Expand Up @@ -162,6 +165,7 @@ func containsStatusCode(expected []int, actual int) bool {
type DeleteHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
RequestMiddlewareFunc RequestMiddlewareFunc
ContentType string
Uri Uri
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Expand All @@ -172,9 +176,12 @@ func (i DeleteHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFu
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, currently only application/json is supported
func (i DeleteHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
// GetContentType returns the content type for the request.
func (i DeleteHttpRequestInput) GetContentType(defaultType string) string {
if i.ContentType != "" {
return i.ContentType
}
return defaultType
}

func (i DeleteHttpRequestInput) GetRequestMiddlewareFunc() RequestMiddlewareFunc {
Expand Down Expand Up @@ -225,12 +232,12 @@ func (i GetHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, defaults to application/json
func (i GetHttpRequestInput) GetContentType() string {
// GetContentType returns the content type for the request.
func (i GetHttpRequestInput) GetContentType(defaultType string) string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
return defaultType
}

func (i GetHttpRequestInput) GetRequestMiddlewareFunc() RequestMiddlewareFunc {
Expand Down Expand Up @@ -284,12 +291,12 @@ func (i PatchHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFun
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, defaults to application/json
func (i PatchHttpRequestInput) GetContentType() string {
// GetContentType returns the content type for the request.
func (i PatchHttpRequestInput) GetContentType(defaultType string) string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
return defaultType
}

func (i PatchHttpRequestInput) GetRequestMiddlewareFunc() RequestMiddlewareFunc {
Expand Down Expand Up @@ -348,12 +355,12 @@ func (i PostHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, defaults to application/json
func (i PostHttpRequestInput) GetContentType() string {
// GetContentType returns the content type for the request.
func (i PostHttpRequestInput) GetContentType(defaultType string) string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
return defaultType
}

func (i PostHttpRequestInput) GetRequestMiddlewareFunc() RequestMiddlewareFunc {
Expand Down Expand Up @@ -412,12 +419,12 @@ func (i PutHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, defaults to application/json
func (i PutHttpRequestInput) GetContentType() string {
// GetContentType returns the content type for the request.
func (i PutHttpRequestInput) GetContentType(defaultType string) string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
return defaultType
}

func (i PutHttpRequestInput) GetRequestMiddlewareFunc() RequestMiddlewareFunc {
Expand Down
1 change: 1 addition & 0 deletions jamfproapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewClient(serverURL string) (*Client, error) {
u.Path = path.Join(u.Path, apiEndpointPath)

client := jamf.NewBaseClient(u)
client.DefaultContentType = "application/json; charset=utf-8"

c := &Client{
BaseClient: client,
Expand Down

0 comments on commit 66f9cb5

Please sign in to comment.