Skip to content

Commit 5886ab7

Browse files
authored
Merge pull request #80 from deploymenttheory/dev
Dev
2 parents decad06 + df3983d commit 5886ab7

File tree

4 files changed

+51
-666
lines changed

4 files changed

+51
-666
lines changed

apihandlers/jamfpro/README.MD

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
------------------------------Summary----------------------------------------
2-
This module is a api handler module for the go-api-http-client to accommodate specifics of
3-
jamf's api(s). It handles the encoding (marshalling) and decoding (unmarshalling)
4-
of data. It also sets the correct content headers for the various http methods.
1+
# Jamf Pro API Handler
52

6-
This module integrates with the go-api-http-client logger for wrapped error handling
7-
for human readable return codes. It also supports the go-api-http-client tiered logging
8-
functionality for logging support.
3+
The Jamf Pro API Handler is an integral component of the Go API HTTP Client, designed specifically for seamless integration with the Jamf Pro API. This handler facilitates the encoding and decoding of requests and responses, manages API-specific headers, and constructs endpoints for efficient API communication.
94

10-
The logic of this module is defined as follows:
5+
## Features
6+
7+
- **Endpoint Construction**: Dynamically constructs API resource and authentication endpoints based on the instance name and predefined URL patterns.
8+
- **Content-Type Handling**: Determines the appropriate `Content-Type` header for requests, with specialized handling for both the Classic API (XML) and the JamfPro API (JSON).
9+
- **Accept Header Management**: Generates a weighted `Accept` header to indicate the client's capability to process various MIME types, prioritizing XML for compatibility with the Classic API.
10+
- **Standard Headers**: Provides a set of standard headers required for API requests, including `Accept`, `Content-Type`, and `Authorization`.
11+
- **Request Marshaling**: Encodes request bodies into the appropriate format (XML or JSON) based on the target API endpoint, with support for multipart/form-data encoding for file uploads.
12+
- **Response Handling**: Processes API responses, decoding them into the desired data structures and handling binary data responses where applicable.
13+
14+
The logic of this api handler is defined as follows:
1115
Classic API:
1216

1317
For requests (GET, POST, PUT, DELETE):
@@ -33,3 +37,42 @@ For responses (DELETE):
3337
Headers
3438
- Sets accept headers based on weighting. Jamf Pro API doesn't support XML, so MIME type is skipped and returns JSON
3539
- Set content header as application/json with edge case exceptions based on need.
40+
41+
42+
## Usage
43+
44+
To utilize the Jamf Pro API Handler within the Go API HTTP Client, instantiate the client with the Jamf Pro-specific configuration:
45+
46+
```go
47+
package main
48+
49+
import (
50+
"log"
51+
52+
"github.com/deploymenttheory/go-api-http-client/httpclient"
53+
"github.com/deploymenttheory/go-api-http-client/apihandlers/jamfpro"
54+
)
55+
56+
func main() {
57+
// Configuration for the HTTP client specific to Jamf Pro
58+
config := httpclient.ClientConfig{
59+
Environment: httpclient.EnvironmentConfig{
60+
InstanceName: "your-instance-name",
61+
APIType: "jamfpro", // Specify the API type as "jamfpro"
62+
},
63+
// Other configuration settings...
64+
}
65+
66+
// Initialize the Jamf Pro API handler with the configuration
67+
jamfHandler, err := jamfpro.LoadAPIHandler(config.Environment.APIType, config.Logger)
68+
if err != nil {
69+
log.Fatalf("Failed to initialize Jamf Pro API handler: %v", err)
70+
}
71+
72+
// Use the handler for API interactions
73+
// Example: Constructing an API resource endpoint
74+
resourceURL := jamfHandler.ConstructAPIResourceEndpoint(config.Environment.InstanceName, "/path/to/resource", config.Logger)
75+
log.Printf("Constructed Resource URL: %s", resourceURL)
76+
77+
// Proceed with making API calls using the constructed URLs and the configured HTTP client...
78+
}

apihandlers/jamfpro/jamfpro_api_response.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -72,55 +72,6 @@ func (j *JamfAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interfa
7272
return fmt.Errorf("received non-success status code: %d, raw response: %s", resp.StatusCode, rawResponse)
7373
}
7474

75-
/*
76-
// HandleResponse processes an HTTP response for a Jamf API request. It handles different response types and errors accordingly.
77-
func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, log logger.Logger) error {
78-
// Special handling for DELETE requests
79-
if resp.Request.Method == "DELETE" {
80-
return j.handleDeleteRequest(resp)
81-
}
82-
83-
// Read the response body
84-
bodyBytes, err := j.readResponseBody(resp)
85-
if err != nil {
86-
return err
87-
}
88-
89-
// Convert bodyBytes to a string to represent the raw response body
90-
rawResponse := string(bodyBytes)
91-
92-
// Log the raw response details for debugging
93-
j.logResponseDetails(resp, bodyBytes)
94-
95-
// Get the content type from the response headers
96-
contentType := resp.Header.Get("Content-Type")
97-
98-
// Check for binary data handling
99-
contentDisposition := resp.Header.Get("Content-Disposition")
100-
if err := j.handleBinaryData(contentType, contentDisposition, bodyBytes, out); err != nil {
101-
return err
102-
}
103-
104-
// Process based on the content type and status code
105-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
106-
// Handle known error content types
107-
if strings.Contains(contentType, "text/html") {
108-
return j.handleErrorHTMLResponse(bodyBytes, resp.StatusCode)
109-
} else if strings.Contains(contentType, "application/json") {
110-
return j.handleErrorJSONResponse(bodyBytes, resp.StatusCode, rawResponse)
111-
}
112-
// Log generic error for unknown content types
113-
j.Logger.Error("Received non-success status code without detailed error response",
114-
zap.Int("status_code", resp.StatusCode),
115-
zap.String("raw_response", rawResponse),
116-
)
117-
return fmt.Errorf("received non-success status code: %d", resp.StatusCode)
118-
}
119-
120-
// Unmarshal the response based on content type
121-
return j.unmarshalResponse(contentType, bodyBytes, out)
122-
}
123-
*/
12475
// handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body.
12576
func (j *JamfAPIHandler) handleDeleteRequest(resp *http.Response) error {
12677
if resp.StatusCode >= 200 && resp.StatusCode < 300 {

0 commit comments

Comments
 (0)