-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservicenow_service_client_option.go
87 lines (71 loc) · 2.59 KB
/
servicenow_service_client_option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package servicenowsdkgo
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/michaeldcanady/servicenow-sdk-go/internal"
"github.com/microsoft/kiota-abstractions-go/store"
nethttplibrary "github.com/microsoft/kiota-http-go"
)
// serviceNowServiceClientOption is a function type that modifies the serviceNowServiceClientConfig.
// It returns an error if the modification is not successful.
type serviceNowServiceClientOption func(*serviceNowServiceClientConfig) error
// withURL creates an option to set the base URL for the requests.
// It returns an error if the provided configuration is nil or the URL is empty.
func withURL(uri string) serviceNowServiceClientOption {
return func(config *serviceNowServiceClientConfig) error {
if internal.IsNil(config) {
return errors.New("config is nil")
}
uri = strings.TrimSpace(uri)
if uri == "" {
return errors.New("url is empty")
}
if _, err := url.ParseRequestURI(uri); err != nil {
return fmt.Errorf("%s", err)
}
config.rawURI = uri
return nil
}
}
// withMiddleware creates an option to set the middleware used by the requests.
// It returns an error if the provided configuration is nil or the middleware slice is empty.
func withMiddleware(middleware ...nethttplibrary.Middleware) serviceNowServiceClientOption {
return func(config *serviceNowServiceClientConfig) error {
if internal.IsNil(config) {
return errors.New("config is nil")
}
if len(middleware) == 0 {
return errors.New("middleware is empty")
}
config.middleware = append(config.middleware, middleware...)
return nil
}
}
// withInstance creates an option to set the instance of the default ServiceNow URL for the requests.
// It returns an error if the provided configuration is nil or the instance string is empty.
func withInstance(instance string) serviceNowServiceClientOption {
return func(config *serviceNowServiceClientConfig) error {
if internal.IsNil(config) {
return errors.New("config is nil")
}
instance = strings.TrimSpace(instance)
if instance == "" {
return errors.New("instance is empty")
}
config.instance = instance
return nil
}
}
// withBackingStoreFactory creates an option to set the backingStoreFactory for the serviceNowServiceClient.
// It returns an error if the provided factory is nil.
func withBackingStoreFactory(backingStoreFactory store.BackingStoreFactory) serviceNowServiceClientOption {
return func(config *serviceNowServiceClientConfig) error {
if internal.IsNil(backingStoreFactory) {
return errors.New("backingStoreFactory is nil")
}
config.backingStoreFactory = backingStoreFactory
return nil
}
}