Skip to content

Commit cb7db7e

Browse files
needed components for kiota based modules
1 parent 3414eed commit cb7db7e

27 files changed

+1198
-9
lines changed

.vscode/settings.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@
99
"go.formatFlags": ["-s", "-w"],
1010
"go.lintOnSave": "package",
1111
"go.lintTool": "golangci-lint",
12-
"go.lintFlags": []
12+
"go.lintFlags": [],
13+
"cSpell.words": [
14+
"formserialization",
15+
"jsonserialization",
16+
"kiota",
17+
"michaeldcanady",
18+
"multipartserialization",
19+
"nethttplibrary",
20+
"nolint",
21+
"servicenowsdkgo",
22+
"stylecheck",
23+
"textserialization"
24+
]
1325
}

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ require (
1111
)
1212

1313
require (
14+
github.com/cjlapao/common-go v0.0.39 // indirect
1415
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/go-logr/logr v1.4.1 // indirect
17+
github.com/go-logr/stdr v1.2.2 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
1519
github.com/kr/pretty v0.3.1 // indirect
1620
github.com/kr/text v0.2.0 // indirect
21+
github.com/microsoft/kiota-abstractions-go v1.8.1 // indirect
22+
github.com/microsoft/kiota-http-go v1.4.7 // indirect
23+
github.com/microsoft/kiota-serialization-form-go v1.0.0 // indirect
24+
github.com/microsoft/kiota-serialization-json-go v1.0.9 // indirect
25+
github.com/microsoft/kiota-serialization-multipart-go v1.0.0 // indirect
26+
github.com/microsoft/kiota-serialization-text-go v1.0.0 // indirect
1727
github.com/pmezard/go-difflib v1.0.0 // indirect
1828
github.com/rogpeppe/go-internal v1.9.0 // indirect
29+
github.com/std-uritemplate/std-uritemplate/go/v2 v2.0.1 // indirect
30+
github.com/stretchr/objx v0.5.2 // indirect
31+
go.opentelemetry.io/otel v1.24.0 // indirect
32+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
33+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
1934
golang.org/x/net v0.35.0 // indirect
2035
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2136
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
github.com/cjlapao/common-go v0.0.39 h1:bAAUrj2B9v0kMzbAOhzjSmiyDy+rd56r2sy7oEiQLlA=
2+
github.com/cjlapao/common-go v0.0.39/go.mod h1:M3dzazLjTjEtZJbbxoA5ZDiGCiHmpwqW9l4UWaddwOA=
13
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
57
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
8+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
9+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
10+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
11+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
12+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
613
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
714
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
815
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
916
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
17+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
18+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1019
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1120
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1221
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@@ -17,19 +26,41 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1726
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1827
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1928
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
29+
github.com/microsoft/kiota-abstractions-go v1.8.1 h1:0gtK3KERmbKYm5AxJLZ8WPlNR9eACUGWuofFIa01PnA=
30+
github.com/microsoft/kiota-abstractions-go v1.8.1/go.mod h1:YO2QCJyNM9wzvlgGLepw6s9XrPgNHODOYGVDCqQWdLI=
31+
github.com/microsoft/kiota-http-go v1.4.7 h1:yFmwYLlCCkeYGDZyvBgfoHWAadOwmg4XQyx35CBkwU8=
32+
github.com/microsoft/kiota-http-go v1.4.7/go.mod h1:Kup5nMDD3a9sjdgRKHCqZWqtrv3FbprjcPaGjLR6FzM=
33+
github.com/microsoft/kiota-serialization-form-go v1.0.0 h1:UNdrkMnLFqUCccQZerKjblsyVgifS11b3WCx+eFEsAI=
34+
github.com/microsoft/kiota-serialization-form-go v1.0.0/go.mod h1:h4mQOO6KVTNciMF6azi1J9QB19ujSw3ULKcSNyXXOMA=
35+
github.com/microsoft/kiota-serialization-json-go v1.0.9 h1:lJivec0G0tI6T8McBTnucyyYXczXytwcu1pt0UjWSBY=
36+
github.com/microsoft/kiota-serialization-json-go v1.0.9/go.mod h1:AxrS/Gbmr8y/hIp2pJcpTup/2wCE8ED+VEXkf/9xKb4=
37+
github.com/microsoft/kiota-serialization-multipart-go v1.0.0 h1:3O5sb5Zj+moLBiJympbXNaeV07K0d46IfuEd5v9+pBs=
38+
github.com/microsoft/kiota-serialization-multipart-go v1.0.0/go.mod h1:yauLeBTpANk4L03XD985akNysG24SnRJGaveZf+p4so=
39+
github.com/microsoft/kiota-serialization-text-go v1.0.0 h1:XOaRhAXy+g8ZVpcq7x7a0jlETWnWrEum0RhmbYrTFnA=
40+
github.com/microsoft/kiota-serialization-text-go v1.0.0/go.mod h1:sM1/C6ecnQ7IquQOGUrUldaO5wj+9+v7G2W3sQ3fy6M=
2041
github.com/mozillazg/go-httpheader v0.4.0 h1:aBn6aRXtFzyDLZ4VIRLsZbbJloagQfMnCiYgOq6hK4w=
2142
github.com/mozillazg/go-httpheader v0.4.0/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA=
2243
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
2344
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2445
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2546
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
2647
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
48+
github.com/std-uritemplate/std-uritemplate/go/v2 v2.0.1 h1:/m2cTZHpqgofDsrwPqsASI6fSNMNhb+9EmUYtHEV2Uk=
49+
github.com/std-uritemplate/std-uritemplate/go/v2 v2.0.1/go.mod h1:Z5KcoM0YLC7INlNhEezeIZ0TZNYf7WSNO0Lvah4DSeQ=
50+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
51+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
2752
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2853
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2954
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3055
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3156
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
3257
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
58+
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
59+
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
60+
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
61+
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
62+
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
63+
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
3364
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
3465
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
3566
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=

internal/mocking/Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Mocking
2+
3+
This is where mocking structs live, it allows for a centralized repository for all mocking implements.
4+
5+
if a mocking struct can't exist here, due to specific constraints, they should be within a `mocking.go` file within the sub-module
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mocking
2+
3+
import (
4+
"context"
5+
6+
abstractions "github.com/microsoft/kiota-abstractions-go"
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
type MockAuthenticationProvider struct {
11+
mock.Mock
12+
}
13+
14+
func NewMockAuthenticationProvider() *MockAuthenticationProvider {
15+
return &MockAuthenticationProvider{
16+
mock.Mock{},
17+
}
18+
}
19+
20+
func (aP *MockAuthenticationProvider) AuthenticateRequest(context context.Context, request *abstractions.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
21+
args := aP.Called(context, request, additionalAuthenticationContext)
22+
23+
return args.Error(0)
24+
}

internal/mocking/mock_middleware.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package mocking
2+
3+
import (
4+
nethttp "net/http"
5+
6+
nethttplibrary "github.com/microsoft/kiota-http-go"
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
var _ nethttplibrary.Middleware = (*MockMiddleware)(nil)
11+
12+
type MockMiddleware struct {
13+
mock.Mock
14+
}
15+
16+
func NewMockMiddleware() *MockMiddleware {
17+
return &MockMiddleware{
18+
mock.Mock{},
19+
}
20+
}
21+
22+
func (m *MockMiddleware) Intercept(pipeline nethttplibrary.Pipeline, middlewareIndex int, request *nethttp.Request) (*nethttp.Response, error) {
23+
args := m.Mock.Called(pipeline, middlewareIndex, request)
24+
return args.Get(0).(*nethttp.Response), args.Error(1)
25+
}

internal/new/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# New
2+
3+
This will be the "new" internal when 2.0 is released.

internal/new/base_request_builder.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package internal
2+
3+
import abstractions "github.com/microsoft/kiota-abstractions-go"
4+
5+
type BaseRequestBuilder interface {
6+
GetPathParameters() map[string]string
7+
SetPathParameters(map[string]string)
8+
GetRequestAdapter() abstractions.RequestAdapter
9+
SetRequestAdapter(abstractions.RequestAdapter)
10+
GetURLTemplate() string
11+
SetURLTemplate(string)
12+
}
13+
14+
type RequestBuilder struct {
15+
abstractions.BaseRequestBuilder
16+
}
17+
18+
func NewRequestBuilder(requestAdapter abstractions.RequestAdapter, urlTemplate string, pathParameters map[string]string) *RequestBuilder {
19+
return &RequestBuilder{
20+
*abstractions.NewBaseRequestBuilder(requestAdapter, urlTemplate, pathParameters),
21+
}
22+
}
23+
24+
func (rB *RequestBuilder) GetPathParameters() map[string]string {
25+
return rB.PathParameters
26+
}
27+
28+
func (rB *RequestBuilder) SetPathParameters(pathParameters map[string]string) {
29+
rB.PathParameters = pathParameters
30+
}
31+
32+
func (rB *RequestBuilder) GetRequestAdapter() abstractions.RequestAdapter {
33+
return rB.RequestAdapter
34+
}
35+
36+
func (rB *RequestBuilder) SetRequestAdapter(requestAdapter abstractions.RequestAdapter) {
37+
rB.RequestAdapter = requestAdapter
38+
}
39+
40+
func (rB *RequestBuilder) GetURLTemplate() string {
41+
return rB.UrlTemplate
42+
}
43+
44+
func (rB *RequestBuilder) SetURLTemplate(urlTemplate string) {
45+
rB.UrlTemplate = urlTemplate
46+
}

internal/new/helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package internal
2+
3+
import "reflect"
4+
5+
// IsNil checks if a value is nil or a nil interface
6+
func IsNil(a interface{}) bool {
7+
defer func() { _ = recover() }()
8+
return a == nil || reflect.ValueOf(a).IsNil()
9+
}

internal/new/mocking.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package internal
2+
3+
import "github.com/stretchr/testify/mock"
4+
5+
type mockServiceNowClientOption struct {
6+
mock.Mock
7+
}
8+
9+
func newMockServiceNowClientOption() *mockServiceNowClientOption {
10+
return &mockServiceNowClientOption{
11+
mock.Mock{},
12+
}
13+
}
14+
15+
func (opt *mockServiceNowClientOption) ServiceNowClientOption(config *serviceNowClientConfig) error {
16+
args := opt.Called(config)
17+
return args.Error(0)
18+
}
19+
20+
type mockServiceNowRequestAdapterOption struct {
21+
mock.Mock
22+
}
23+
24+
func newMockServiceNowRequestAdapterOption() *mockServiceNowRequestAdapterOption {
25+
return &mockServiceNowRequestAdapterOption{
26+
mock.Mock{},
27+
}
28+
}
29+
30+
func (opt *mockServiceNowRequestAdapterOption) ServiceNowClientOption(config *serviceNowRequestAdapterConfig) error {
31+
args := opt.Called(config)
32+
return args.Error(0)
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package internal
2+
3+
import (
4+
"net/http"
5+
6+
nethttplibrary "github.com/microsoft/kiota-http-go"
7+
)
8+
9+
// serviceNowClientConfig represents configurations for a ServiceNowClient
10+
type serviceNowClientConfig struct {
11+
// middleware the middleware for the client
12+
middleware []nethttplibrary.Middleware
13+
}
14+
15+
// buildServiceNowClientConfig constructs new serviceNowClientConfig from provided options
16+
func buildServiceNowClientConfig(opts ...serviceNowClientOption) (*serviceNowClientConfig, error) {
17+
opts = append(opts, serviceNowClientDefaultOptions())
18+
19+
config := new(serviceNowClientConfig)
20+
21+
for _, opt := range opts {
22+
if err := opt(config); err != nil {
23+
return nil, err
24+
}
25+
}
26+
27+
return config, nil
28+
}
29+
30+
// getDefaultMiddleware returns the default middleware for a ServiceNowClient
31+
func getDefaultMiddleware() []nethttplibrary.Middleware {
32+
kiotaMiddlewares := nethttplibrary.GetDefaultMiddlewares()
33+
34+
serviceNowMiddlewares := []nethttplibrary.Middleware{
35+
//NewGraphTelemetryHandler(options),
36+
nethttplibrary.NewHeadersInspectionHandler(),
37+
}
38+
graphMiddlewaresLen := len(serviceNowMiddlewares)
39+
resultMiddlewares := make([]nethttplibrary.Middleware, len(kiotaMiddlewares)+graphMiddlewaresLen)
40+
copy(resultMiddlewares, serviceNowMiddlewares)
41+
copy(resultMiddlewares[graphMiddlewaresLen:], kiotaMiddlewares)
42+
return resultMiddlewares
43+
}
44+
45+
// GetDefaultClient constructs default client using provided options
46+
func GetDefaultClient(opts ...serviceNowClientOption) (*http.Client, error) {
47+
config, err := buildServiceNowClientConfig(opts...)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
return nethttplibrary.GetDefaultClient(config.middleware...), nil
53+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package internal
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
func TestGetDefaultClient(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
test func(*testing.T)
15+
}{
16+
{
17+
name: "opt error",
18+
test: func(t *testing.T) {
19+
strct := newMockServiceNowClientOption()
20+
strct.On("ServiceNowClientOption", mock.IsType(&serviceNowClientConfig{})).Return(errors.New("opt error"))
21+
opt := strct.ServiceNowClientOption
22+
23+
_, err := GetDefaultClient(opt)
24+
assert.Equal(t, errors.New("opt error"), err)
25+
},
26+
},
27+
}
28+
29+
for _, test := range tests {
30+
t.Run(test.name, test.test)
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package internal
2+
3+
import (
4+
"errors"
5+
6+
nethttplibrary "github.com/microsoft/kiota-http-go"
7+
)
8+
9+
// serviceNowClientOption represents options for the ServiceNowClient
10+
type serviceNowClientOption func(*serviceNowClientConfig) error
11+
12+
// WithMiddleware adds supplied middleware to the ServiceNowClientConfig
13+
func WithMiddleware(middleware ...nethttplibrary.Middleware) serviceNowClientOption {
14+
return func(config *serviceNowClientConfig) error {
15+
if len(middleware) == 0 {
16+
return errors.New("middleware is empty")
17+
}
18+
if IsNil(config) {
19+
return errors.New("config is nil")
20+
}
21+
if IsNil(config.middleware) {
22+
config.middleware = []nethttplibrary.Middleware{}
23+
}
24+
config.middleware = append(config.middleware, middleware...)
25+
return nil
26+
}
27+
}
28+
29+
func serviceNowClientDefaultOptions() serviceNowClientOption {
30+
return func(config *serviceNowClientConfig) error {
31+
if len(config.middleware) == 0 {
32+
config.middleware = getDefaultMiddleware()
33+
}
34+
35+
return nil
36+
}
37+
}

0 commit comments

Comments
 (0)