Skip to content

Commit d555fb4

Browse files
committed
Refactor ConstructAPIResourceEndpoint and ConstructAPIAuthEndpoint functions in APIHandler implementations to remove the instanceName parameter
1 parent 0714e9e commit d555fb4

File tree

7 files changed

+18
-22
lines changed

7 files changed

+18
-22
lines changed

apiintegrations/apihandler/apihandler.go

+6-7
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(APIResourceEndpointIdentifier string, endpointPath string, log logger.Logger) string
15-
ConstructAPIAuthEndpoint(APIAuthEndpointIdentifier string, endpointPath string, log logger.Logger) string
14+
ConstructAPIResourceEndpoint(endpointPath string, log logger.Logger) string
15+
ConstructAPIAuthEndpoint(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,22 +29,21 @@ type APIHandler interface {
2929
GetAPIRequestHeaders(endpoint string) map[string]string // Provides standard headers required for making API requests.
3030
}
3131

32-
// Modify the function signature to accept instanceName, tenantID, and tenantName.
32+
// LoadAPIHandler loads the appropriate API handler based on the API type.
3333
func LoadAPIHandler(apiType, instanceName, tenantID, tenantName string, log logger.Logger) (APIHandler, error) {
3434
var apiHandler APIHandler
3535
switch apiType {
3636
case "jamfpro":
3737
apiHandler = &jamfpro.JamfAPIHandler{
3838
Logger: log,
39-
InstanceName: instanceName, // Assuming you add InstanceName field to JamfAPIHandler
39+
InstanceName: instanceName, // Used for constructing the resource and auth endpoints
4040
}
4141
log.Info("Jamf Pro API handler loaded successfully", zap.String("APIType", apiType), zap.String("InstanceName", instanceName))
4242

4343
case "msgraph":
4444
apiHandler = &msgraph.GraphAPIHandler{
45-
Logger: log,
46-
TenantID: tenantID, // Assuming you add TenantID field to GraphAPIHandler
47-
TenantName: tenantName, // Assuming you add TenantName field to GraphAPIHandler
45+
Logger: log,
46+
TenantID: tenantID, // Used for constructing the auth endpoint
4847
}
4948
log.Info("Microsoft Graph API handler loaded successfully", zap.String("APIType", apiType), zap.String("TenantID", tenantID), zap.String("TenantName", tenantName))
5049

apiintegrations/jamfpro/jamfpro_api_url.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ func (j *JamfAPIHandler) SetBaseDomain() string {
1818
}
1919

2020
// ConstructAPIResourceEndpoint constructs the full URL for a Jamf API resource endpoint path and logs the URL.
21-
func (j *JamfAPIHandler) ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
21+
func (j *JamfAPIHandler) ConstructAPIResourceEndpoint(endpointPath string, log logger.Logger) string {
2222
urlBaseDomain := j.SetBaseDomain()
23-
url := fmt.Sprintf("https://%s%s%s", instanceName, urlBaseDomain, endpointPath)
23+
url := fmt.Sprintf("https://%s%s%s", j.InstanceName, urlBaseDomain, endpointPath)
2424
j.Logger.Debug(fmt.Sprintf("Constructed %s API resource endpoint URL", APIName), zap.String("URL", url))
2525
return url
2626
}
2727

2828
// ConstructAPIAuthEndpoint constructs the full URL for a Jamf API auth endpoint path and logs the URL.
29-
func (j *JamfAPIHandler) ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
29+
func (j *JamfAPIHandler) ConstructAPIAuthEndpoint(endpointPath string, log logger.Logger) string {
3030
urlBaseDomain := j.SetBaseDomain()
31-
url := fmt.Sprintf("https://%s%s%s", instanceName, urlBaseDomain, endpointPath)
31+
url := fmt.Sprintf("https://%s%s%s", j.InstanceName, urlBaseDomain, endpointPath)
3232
j.Logger.Debug(fmt.Sprintf("Constructed %s API authentication URL", APIName), zap.String("URL", url))
3333
return url
3434
}

apiintegrations/msgraph/msgraph_api_url.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ import (
1111
// SetBaseDomain returns the appropriate base domain for URL construction.
1212
// It uses j.OverrideBaseDomain if set, otherwise falls back to DefaultBaseDomain.
1313
func (g *GraphAPIHandler) SetBaseDomain() string {
14-
if g.OverrideBaseDomain != "" {
15-
return g.OverrideBaseDomain
16-
}
1714
return DefaultBaseDomain
1815
}
1916

2017
// ConstructAPIResourceEndpoint constructs the full URL for a graph API resource endpoint path and logs the URL.
21-
func (g *GraphAPIHandler) ConstructAPIResourceEndpoint(tenantName string, endpointPath string, log logger.Logger) string {
18+
func (g *GraphAPIHandler) ConstructAPIResourceEndpoint(endpointPath string, log logger.Logger) string {
2219
urlBaseDomain := g.SetBaseDomain()
2320
url := fmt.Sprintf("https://%s%s%s", g.TenantName, urlBaseDomain, endpointPath)
2421
g.Logger.Debug(fmt.Sprintf("Constructed %s API resource endpoint URL", APIName), zap.String("URL", url))
@@ -27,7 +24,7 @@ func (g *GraphAPIHandler) ConstructAPIResourceEndpoint(tenantName string, endpoi
2724

2825
// ConstructAPIAuthEndpoint constructs the full URL for the Microsoft Graph API authentication endpoint.
2926
// It uses the provided tenant ID and endpoint path and logs the constructed URL.
30-
func (g *GraphAPIHandler) ConstructAPIAuthEndpoint(tenantID string, endpointPath string, log logger.Logger) string {
27+
func (g *GraphAPIHandler) ConstructAPIAuthEndpoint(endpointPath string, log logger.Logger) string {
3128
// The base URL for the Microsoft Graph API authentication endpoint.
3229
const baseURL = "https://login.microsoftonline.com"
3330

authenticationhandler/auth_bearer_token.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (h *AuthTokenHandler) ObtainToken(apiHandler apihandler.APIHandler, httpCli
2121
bearerTokenEndpoint := apiHandler.GetBearerTokenEndpoint()
2222

2323
// Construct the full authentication endpoint URL
24-
authenticationEndpoint := apiHandler.ConstructAPIAuthEndpoint(h.InstanceName, bearerTokenEndpoint, h.Logger)
24+
authenticationEndpoint := apiHandler.ConstructAPIAuthEndpoint(bearerTokenEndpoint, h.Logger)
2525

2626
h.Logger.Debug("Attempting to obtain token for user", zap.String("Username", username))
2727

@@ -69,7 +69,7 @@ func (h *AuthTokenHandler) RefreshToken(apiHandler apihandler.APIHandler, httpCl
6969
apiTokenRefreshEndpoint := apiHandler.GetTokenRefreshEndpoint()
7070

7171
// Construct the full authentication endpoint URL
72-
tokenRefreshEndpoint := apiHandler.ConstructAPIAuthEndpoint(h.InstanceName, apiTokenRefreshEndpoint, h.Logger)
72+
tokenRefreshEndpoint := apiHandler.ConstructAPIAuthEndpoint(apiTokenRefreshEndpoint, h.Logger)
7373

7474
h.Logger.Debug("Attempting to refresh token", zap.String("URL", tokenRefreshEndpoint))
7575

authenticationhandler/auth_oauth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (h *AuthTokenHandler) ObtainOAuthToken(apiHandler apihandler.APIHandler, ht
3737
oauthTokenEndpoint := apiHandler.GetOAuthTokenEndpoint()
3838

3939
// Construct the full authentication endpoint URL
40-
authenticationEndpoint := apiHandler.ConstructAPIAuthEndpoint(h.InstanceName, oauthTokenEndpoint, h.Logger)
40+
authenticationEndpoint := apiHandler.ConstructAPIAuthEndpoint(oauthTokenEndpoint, h.Logger)
4141

4242
// Get the OAuth token scope from the APIHandler
4343
oauthTokenScope := apiHandler.GetOAuthTokenScope()

httpclient/multipartrequest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
6060
}
6161

6262
// Construct URL using the ConstructAPIResourceEndpoint function
63-
url := c.APIHandler.ConstructAPIResourceEndpoint(c.clientConfig.Environment.InstanceName, endpoint, log)
63+
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
6464

6565
// Create the request
6666
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))

httpclient/request.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
146146
}
147147

148148
// Construct URL with correct structure defined in api handler
149-
url := c.APIHandler.ConstructAPIResourceEndpoint(c.clientConfig.Environment.InstanceName, endpoint, log)
149+
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
150150

151151
// Increment total request counter within ConcurrencyHandler's metrics
152152
c.ConcurrencyHandler.Metrics.Lock.Lock()
@@ -312,7 +312,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
312312
}
313313

314314
// Construct URL using the ConstructAPIResourceEndpoint function
315-
url := c.APIHandler.ConstructAPIResourceEndpoint(c.clientConfig.Environment.InstanceName, endpoint, log)
315+
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
316316

317317
// Create a new HTTP request with the provided method, URL, and body
318318
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))

0 commit comments

Comments
 (0)