Skip to content

Commit c8d3827

Browse files
committed
Refactor code for improved performance and readability
1 parent e9272d2 commit c8d3827

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package graph
2+
3+
import "github.com/deploymenttheory/go-api-http-client/logger"
4+
5+
// GraphAPIHandler implements the APIHandler interface for the Jamf Pro API.
6+
type GraphAPIHandler struct {
7+
OverrideBaseDomain string // OverrideBaseDomain is used to override the base domain for URL construction.
8+
InstanceName string // InstanceName is the name of the Jamf instance.
9+
Logger logger.Logger // Logger is the structured logger used for logging.
10+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graph
2+
3+
// Endpoint constants represent the URL suffixes used for Jamf API token interactions.
4+
const (
5+
APIName = "graph" // APIName: represents the name of the API.
6+
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.
11+
BearerTokenAuthenticationSupport = true // BearerTokenAuthSuppport: A boolean to indicate if the API supports bearer token authentication.
12+
OAuthAuthenticationSupport = true // OAuthAuthSuppport: A boolean to indicate if the API supports OAuth authentication.
13+
OAuthWithCertAuthenticationSupport = true // OAuthWithCertAuthSuppport: A boolean to indicate if the API supports OAuth with client certificate authentication.
14+
)
15+
16+
// GetDefaultBaseDomain returns the default base domain used for constructing API URLs to the http client.
17+
func (g *GraphAPIHandler) GetDefaultBaseDomain() string {
18+
return DefaultBaseDomain
19+
}
20+
21+
// GetOAuthTokenEndpoint returns the endpoint for obtaining an OAuth token. Used for constructing API URLs for the http client.
22+
func (g *GraphAPIHandler) GetOAuthTokenEndpoint() string {
23+
return OAuthTokenEndpoint
24+
}
25+
26+
// GetBearerTokenEndpoint returns the endpoint for obtaining a bearer token. Used for constructing API URLs for the http client.
27+
func (g *GraphAPIHandler) GetBearerTokenEndpoint() string {
28+
return BearerTokenEndpoint
29+
}
30+
31+
// GetTokenRefreshEndpoint returns the endpoint for refreshing an existing token. Used for constructing API URLs for the http client.
32+
func (g *GraphAPIHandler) GetTokenRefreshEndpoint() string {
33+
return TokenRefreshEndpoint
34+
}
35+
36+
// GetTokenInvalidateEndpoint returns the endpoint for invalidating an active token. Used for constructing API URLs for the http client.
37+
func (g *GraphAPIHandler) GetTokenInvalidateEndpoint() string {
38+
return TokenInvalidateEndpoint
39+
}
40+
41+
// GetAPIBearerTokenAuthenticationSupportStatus returns a boolean indicating if bearer token authentication is supported in the api handler.
42+
func (g *GraphAPIHandler) GetAPIBearerTokenAuthenticationSupportStatus() bool {
43+
return BearerTokenAuthenticationSupport
44+
}
45+
46+
// GetAPIOAuthAuthenticationSupportStatus returns a boolean indicating if OAuth authentication is supported in the api handler.
47+
func (g *GraphAPIHandler) GetAPIOAuthAuthenticationSupportStatus() bool {
48+
return OAuthAuthenticationSupport
49+
}
50+
51+
// GetAPIOAuthWithCertAuthenticationSupportStatus returns a boolean indicating if OAuth with client certificate authentication is supported in the api handler.
52+
func (g *GraphAPIHandler) GetAPIOAuthWithCertAuthenticationSupportStatus() bool {
53+
return OAuthWithCertAuthenticationSupport
54+
}

apihandlers/graph/graph_api_url.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// graph_api_url.go
2+
package graph
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/deploymenttheory/go-api-http-client/logger"
8+
"go.uber.org/zap"
9+
)
10+
11+
// SetBaseDomain returns the appropriate base domain for URL construction.
12+
// It uses j.OverrideBaseDomain if set, otherwise falls back to DefaultBaseDomain.
13+
func (g *GraphAPIHandler) SetBaseDomain() string {
14+
if g.OverrideBaseDomain != "" {
15+
return g.OverrideBaseDomain
16+
}
17+
return DefaultBaseDomain
18+
}
19+
20+
// ConstructAPIResourceEndpoint constructs the full URL for a Jamf API resource endpoint path and logs the URL.
21+
func (g *GraphAPIHandler) ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
22+
urlBaseDomain := g.SetBaseDomain()
23+
url := fmt.Sprintf("https://%s%s%s", instanceName, urlBaseDomain, endpointPath)
24+
g.Logger.Debug(fmt.Sprintf("Constructed %s API resource endpoint URL", APIName), zap.String("URL", url))
25+
return url
26+
}
27+
28+
// ConstructAPIAuthEndpoint constructs the full URL for a Jamf API auth endpoint path and logs the URL.
29+
func (g *GraphAPIHandler) ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
30+
urlBaseDomain := g.SetBaseDomain()
31+
url := fmt.Sprintf("https://%s%s%s", instanceName, urlBaseDomain, endpointPath)
32+
g.Logger.Debug(fmt.Sprintf("Constructed %s API authentication URL", APIName), zap.String("URL", url))
33+
return url
34+
}

0 commit comments

Comments
 (0)