|
| 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