Skip to content

Commit 0714e9e

Browse files
committed
Refactor APIHandler interface and LoadAPIHandler function to accept additional parameters
1 parent 96bd7ee commit 0714e9e

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

apiintegrations/apihandler/apihandler.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
// APIHandler is an interface for encoding, decoding, and implenting contexual api functions for different API implementations.
1212
// It encapsulates behavior for encoding and decoding requests and responses.
1313
type APIHandler interface {
14-
ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string
15-
ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string
14+
ConstructAPIResourceEndpoint(APIResourceEndpointIdentifier string, endpointPath string, log logger.Logger) string
15+
ConstructAPIAuthEndpoint(APIAuthEndpointIdentifier string, endpointPath string, log logger.Logger) string
1616
MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error)
1717
MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error)
1818
GetContentTypeHeader(method string, log logger.Logger) string
@@ -29,23 +29,24 @@ type APIHandler interface {
2929
GetAPIRequestHeaders(endpoint string) map[string]string // Provides standard headers required for making API requests.
3030
}
3131

32-
// LoadAPIHandler returns an APIHandler based on the provided API type.
33-
// 'apiType' parameter could be "jamf" or "graph" to specify which API handler to load.
34-
func LoadAPIHandler(apiType string, log logger.Logger) (APIHandler, error) {
32+
// Modify the function signature to accept instanceName, tenantID, and tenantName.
33+
func LoadAPIHandler(apiType, instanceName, tenantID, tenantName string, log logger.Logger) (APIHandler, error) {
3534
var apiHandler APIHandler
3635
switch apiType {
3736
case "jamfpro":
3837
apiHandler = &jamfpro.JamfAPIHandler{
39-
Logger: log,
40-
// Initialize with necessary parameters
38+
Logger: log,
39+
InstanceName: instanceName, // Assuming you add InstanceName field to JamfAPIHandler
4140
}
42-
log.Info("API handler loaded successfully", zap.String("APIType", apiType))
41+
log.Info("Jamf Pro API handler loaded successfully", zap.String("APIType", apiType), zap.String("InstanceName", instanceName))
4342

4443
case "msgraph":
4544
apiHandler = &msgraph.GraphAPIHandler{
46-
// Initialize with necessary parameters
45+
Logger: log,
46+
TenantID: tenantID, // Assuming you add TenantID field to GraphAPIHandler
47+
TenantName: tenantName, // Assuming you add TenantName field to GraphAPIHandler
4748
}
48-
log.Info("API handler loaded successfully", zap.String("APIType", apiType))
49+
log.Info("Microsoft Graph API handler loaded successfully", zap.String("APIType", apiType), zap.String("TenantID", tenantID), zap.String("TenantName", tenantName))
4950

5051
default:
5152
return nil, log.Error("Unsupported API type", zap.String("APIType", apiType))

apiintegrations/msgraph/msgraph_api_url.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (g *GraphAPIHandler) SetBaseDomain() string {
2020
// ConstructAPIResourceEndpoint constructs the full URL for a graph API resource endpoint path and logs the URL.
2121
func (g *GraphAPIHandler) ConstructAPIResourceEndpoint(tenantName string, endpointPath string, log logger.Logger) string {
2222
urlBaseDomain := g.SetBaseDomain()
23-
url := fmt.Sprintf("https://%s%s%s", tenantName, urlBaseDomain, endpointPath)
23+
url := fmt.Sprintf("https://%s%s%s", g.TenantName, urlBaseDomain, endpointPath)
2424
g.Logger.Debug(fmt.Sprintf("Constructed %s API resource endpoint URL", APIName), zap.String("URL", url))
2525
return url
2626
}
@@ -32,7 +32,7 @@ func (g *GraphAPIHandler) ConstructAPIAuthEndpoint(tenantID string, endpointPath
3232
const baseURL = "https://login.microsoftonline.com"
3333

3434
// Construct the full URL by combining the base URL, tenant ID, and endpoint path.
35-
url := fmt.Sprintf("%s/%s%s", baseURL, tenantID, endpointPath)
35+
url := fmt.Sprintf("%s/%s%s", baseURL, g.TenantID, endpointPath)
3636

3737
// Log the constructed URL for debugging purposes.
3838
log.Debug("constructed Microsoft Graph API authentication URL", zap.String("URL", url))

httpclient/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
8888
log.SetLevel(parsedLogLevel)
8989

9090
// Use the APIType from the config to determine which API handler to load
91-
apiHandler, err := apihandler.LoadAPIHandler(config.Environment.APIType, log)
91+
apiHandler, err := apihandler.LoadAPIHandler(config.Environment.APIType, config.Environment.InstanceName, config.Environment.TenantID, config.Environment.TenantName, log)
9292
if err != nil {
9393
log.Error("Failed to load API handler", zap.String("APIType", config.Environment.APIType), zap.Error(err))
9494
return nil, err

0 commit comments

Comments
 (0)