Skip to content

Commit dd69997

Browse files
authored
Merge pull request #119 from deploymenttheory/dev
large refactor to decouple componants within the httpclient into dist…
2 parents b84499d + db8d75b commit dd69997

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1017
-80
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// apiintegrations/apihandler/apihandler_test.go
2+
package apihandler
3+
4+
import (
5+
"testing"
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/mocklogger"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/mock"
12+
)
13+
14+
func TestLoadAPIHandler(t *testing.T) {
15+
// Create a mock logger for testing purposes.
16+
mockLog := mocklogger.NewMockLogger()
17+
18+
// Define your test cases.
19+
tests := []struct {
20+
name string
21+
apiType string
22+
wantType interface{}
23+
wantErr bool
24+
}{
25+
{
26+
name: "Load JamfPro Handler",
27+
apiType: "jamfpro",
28+
wantType: &jamfpro.JamfAPIHandler{},
29+
wantErr: false,
30+
},
31+
{
32+
name: "Load Graph Handler",
33+
apiType: "msgraph",
34+
wantType: &msgraph.GraphAPIHandler{},
35+
wantErr: false,
36+
},
37+
{
38+
name: "Unsupported API Type",
39+
apiType: "unknown",
40+
wantErr: true,
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
// Setup expectations for the mock logger based on whether an error is expected.
47+
if tt.wantErr {
48+
mockLog.On("Error", mock.Anything, mock.Anything, mock.Anything).Return().Once()
49+
} else {
50+
mockLog.On("Info", mock.Anything, mock.Anything, mock.Anything).Return().Once()
51+
}
52+
53+
// Attempt to load the API handler.
54+
got, err := LoadAPIHandler(tt.apiType, mockLog)
55+
56+
// Assert error handling.
57+
if tt.wantErr {
58+
assert.Error(t, err)
59+
} else {
60+
assert.NoError(t, err)
61+
assert.IsType(t, tt.wantType, got, "Got %T, want %T", got, tt.wantType)
62+
}
63+
64+
// Assert that the mock logger's expectations were met.
65+
mockLog.AssertExpectations(t)
66+
})
67+
}
68+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)