Skip to content

Commit 2fba1e2

Browse files
committed
Update Microsoft Graph API constants
1 parent 00fcb03 commit 2fba1e2

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// graph_api_exceptions.go
2+
package graph
3+
4+
import (
5+
_ "embed"
6+
7+
"encoding/json"
8+
"log"
9+
)
10+
11+
// EndpointConfig is a struct that holds configuration details for a specific API endpoint.
12+
// It includes what type of content it can accept and what content type it should send.
13+
type EndpointConfig struct {
14+
Accept string `json:"accept"` // Accept specifies the MIME type the endpoint can handle in responses.
15+
ContentType *string `json:"content_type"` // ContentType, if not nil, specifies the MIME type to set for requests sent to the endpoint. A pointer is used to distinguish between a missing field and an empty string.
16+
}
17+
18+
// ConfigMap is a map that associates endpoint URL patterns with their corresponding configurations.
19+
// The map's keys are strings that identify the endpoint, and the values are EndpointConfig structs
20+
// that hold the configuration for that endpoint.
21+
type ConfigMap map[string]EndpointConfig
22+
23+
// Variables
24+
var configMap ConfigMap
25+
26+
// Embedded Resources
27+
//
28+
//go:embed graph_api_exceptions_configuration.json
29+
var graph_api_exceptions_configuration []byte
30+
31+
// init is invoked automatically on package initialization and is responsible for
32+
// setting up the default state of the package by loading the api exceptions configuration.
33+
func init() {
34+
// Load the default configuration from an embedded resource.
35+
err := loadAPIExceptionsConfiguration()
36+
if err != nil {
37+
log.Fatalf("Error loading Jamf Pro API exceptions configuration: %s", err)
38+
}
39+
}
40+
41+
// loadAPIExceptionsConfiguration reads and unmarshals the jamfpro_api_exceptions_configuration JSON data from an embedded file
42+
// into the configMap variable, which holds the exceptions configuration for endpoint-specific headers.
43+
func loadAPIExceptionsConfiguration() error {
44+
// Unmarshal the embedded default configuration into the global configMap.
45+
return json.Unmarshal(graph_api_exceptions_configuration, &configMap)
46+
}

apihandlers/graph/graph_api_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// graph_api_handler.go
12
package graph
23

34
import "github.com/deploymenttheory/go-api-http-client/logger"

apihandlers/graph/graph_api_handler_constants.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
// graph_api_handler_constants.go
12
package graph
23

34
// Endpoint constants represent the URL suffixes used for Jamf API token interactions.
45
const (
5-
APIName = "graph" // APIName: represents the name of the API.
6+
APIName = "microsoft graph" // APIName: represents the name of the API.
67
DefaultBaseDomain = "graph.microsoft.com" // DefaultBaseDomain: represents the base domain for the jamf instance.
7-
OAuthTokenEndpoint = "" // OAuthTokenEndpoint: The endpoint to obtain an OAuth token.
8-
BearerTokenEndpoint = "" // BearerTokenEndpoint: The endpoint to obtain a bearer token.
9-
TokenRefreshEndpoint = "" // TokenRefreshEndpoint: The endpoint to refresh an existing token.
10-
TokenInvalidateEndpoint = "" // TokenInvalidateEndpoint: The endpoint to invalidate an active token.
8+
OAuthTokenEndpoint = "graph.microsoft.com" // OAuthTokenEndpoint: The endpoint to obtain an OAuth token.
9+
BearerTokenEndpoint = "graph.microsoft.com" // BearerTokenEndpoint: The endpoint to obtain a bearer token.
10+
TokenRefreshEndpoint = "graph.microsoft.com" // TokenRefreshEndpoint: The endpoint to refresh an existing token.
11+
TokenInvalidateEndpoint = "graph.microsoft.com" // TokenInvalidateEndpoint: The endpoint to invalidate an active token.
1112
BearerTokenAuthenticationSupport = true // BearerTokenAuthSuppport: A boolean to indicate if the API supports bearer token authentication.
1213
OAuthAuthenticationSupport = true // OAuthAuthSuppport: A boolean to indicate if the API supports OAuth authentication.
1314
OAuthWithCertAuthenticationSupport = true // OAuthWithCertAuthSuppport: A boolean to indicate if the API supports OAuth with client certificate authentication.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// graph_api_headers.go
2+
package graph
3+
4+
import (
5+
"strings"
6+
7+
"github.com/deploymenttheory/go-api-http-client/logger"
8+
"go.uber.org/zap"
9+
)
10+
11+
// GetContentTypeHeader determines the appropriate Content-Type header for a given API endpoint.
12+
// It attempts to find a content type that matches the endpoint prefix in the global configMap.
13+
// If a match is found and the content type is defined (not nil), it returns the specified content type.
14+
// If the endpoint does not match any of the predefined patterns, "application/json" is used as a fallback.
15+
// This method logs the decision process at various stages for debugging purposes.
16+
func (g *GraphAPIHandler) GetContentTypeHeader(endpoint string, log logger.Logger) string {
17+
// Dynamic lookup from configuration should be the first priority
18+
for key, config := range configMap {
19+
if strings.HasPrefix(endpoint, key) {
20+
if config.ContentType != nil {
21+
g.Logger.Debug("Content-Type for endpoint found in configMap", zap.String("endpoint", endpoint), zap.String("content_type", *config.ContentType))
22+
return *config.ContentType
23+
}
24+
g.Logger.Debug("Content-Type for endpoint is nil in configMap, handling as special case", zap.String("endpoint", endpoint))
25+
// If a nil ContentType is an expected case, do not set Content-Type header.
26+
return "" // Return empty to indicate no Content-Type should be set.
27+
}
28+
}
29+
30+
// Fallback to JSON if no other match is found.
31+
g.Logger.Debug("Content-Type for endpoint not found in configMap or standard patterns, using default JSON", zap.String("endpoint", endpoint))
32+
return "application/json"
33+
}
34+
35+
// GetAcceptHeader constructs and returns a weighted Accept header string for HTTP requests.
36+
// The Accept header indicates the MIME types that the client can process and prioritizes them
37+
// based on the quality factor (q) parameter. Higher q-values signal greater preference.
38+
// This function specifies a range of MIME types with their respective weights, ensuring that
39+
// the server is informed of the client's versatile content handling capabilities while
40+
// indicating a preference for XML. The specified MIME types cover common content formats like
41+
// images, JSON, XML, HTML, plain text, and certificates, with a fallback option for all other types.
42+
func (g *GraphAPIHandler) GetAcceptHeader() string {
43+
weightedAcceptHeader := "application/x-x509-ca-cert;q=0.95," +
44+
"application/pkix-cert;q=0.94," +
45+
"application/pem-certificate-chain;q=0.93," +
46+
"application/octet-stream;q=0.8," + // For general binary files
47+
"image/png;q=0.75," +
48+
"image/jpeg;q=0.74," +
49+
"image/*;q=0.7," +
50+
"application/xml;q=0.65," +
51+
"text/xml;q=0.64," +
52+
"text/xml;charset=UTF-8;q=0.63," +
53+
"application/json;q=0.5," +
54+
"text/html;q=0.5," +
55+
"text/plain;q=0.4," +
56+
"*/*;q=0.05" // Fallback for any other types
57+
58+
return weightedAcceptHeader
59+
}
60+
61+
// GetAPIRequestHeaders returns a map of standard headers required for making API requests.
62+
func (g *GraphAPIHandler) GetAPIRequestHeaders(endpoint string) map[string]string {
63+
headers := map[string]string{
64+
"Accept": g.GetAcceptHeader(), // Dynamically set based on API requirements.
65+
"Content-Type": g.GetContentTypeHeader(endpoint, g.Logger), // Dynamically set based on the endpoint.
66+
"Authorization": "", // To be set by the client with the actual token.
67+
"User-Agent": "go-api-http-client-graph-handler", // To be set by the client, usually with application info.
68+
}
69+
return headers
70+
}

0 commit comments

Comments
 (0)