|
| 1 | +// apiintegrations/apihandler/apihandler.go |
| 2 | +package apihandler |
| 3 | + |
| 4 | +import ( |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/deploymenttheory/go-api-http-client/apiintegrations/jamfpro" |
| 8 | + "github.com/deploymenttheory/go-api-http-client/apiintegrations/msgraph" |
| 9 | + "github.com/deploymenttheory/go-api-http-client/logger" |
| 10 | + "go.uber.org/zap" |
| 11 | +) |
| 12 | + |
| 13 | +// APIHandler is an interface for encoding, decoding, and implenting contexual api functions for different API implementations. |
| 14 | +// It encapsulates behavior for encoding and decoding requests and responses. |
| 15 | +type APIHandler interface { |
| 16 | + ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string |
| 17 | + ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string |
| 18 | + MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error) |
| 19 | + MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error) |
| 20 | + HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.Logger) error |
| 21 | + HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error |
| 22 | + GetContentTypeHeader(method string, log logger.Logger) string |
| 23 | + GetAcceptHeader() string |
| 24 | + GetDefaultBaseDomain() string |
| 25 | + GetOAuthTokenEndpoint() string |
| 26 | + GetBearerTokenEndpoint() string |
| 27 | + GetTokenRefreshEndpoint() string |
| 28 | + GetTokenInvalidateEndpoint() string |
| 29 | + GetAPIBearerTokenAuthenticationSupportStatus() bool |
| 30 | + GetAPIOAuthAuthenticationSupportStatus() bool |
| 31 | + GetAPIOAuthWithCertAuthenticationSupportStatus() bool |
| 32 | + GetAPIRequestHeaders(endpoint string) map[string]string // Provides standard headers required for making API requests. |
| 33 | +} |
| 34 | + |
| 35 | +// LoadAPIHandler returns an APIHandler based on the provided API type. |
| 36 | +// 'apiType' parameter could be "jamf" or "graph" to specify which API handler to load. |
| 37 | +func LoadAPIHandler(apiType string, log logger.Logger) (APIHandler, error) { |
| 38 | + var apiHandler APIHandler |
| 39 | + switch apiType { |
| 40 | + case "jamfpro": |
| 41 | + apiHandler = &jamfpro.JamfAPIHandler{ |
| 42 | + Logger: log, |
| 43 | + // Initialize with necessary parameters |
| 44 | + } |
| 45 | + log.Info("API handler loaded successfully", zap.String("APIType", apiType)) |
| 46 | + |
| 47 | + case "msgraph": |
| 48 | + apiHandler = &msgraph.GraphAPIHandler{ |
| 49 | + // Initialize with necessary parameters |
| 50 | + } |
| 51 | + log.Info("API handler loaded successfully", zap.String("APIType", apiType)) |
| 52 | + |
| 53 | + default: |
| 54 | + return nil, log.Error("Unsupported API type", zap.String("APIType", apiType)) |
| 55 | + } |
| 56 | + |
| 57 | + return apiHandler, nil |
| 58 | +} |
0 commit comments