-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.go
109 lines (89 loc) · 4.13 KB
/
Factory.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package acquiringsdk
import (
"errors"
"net/url"
"github.com/Worldline-Acquiring/acquiring-sdk-go/authentication"
"github.com/Worldline-Acquiring/acquiring-sdk-go/authentication/oauth2"
"github.com/Worldline-Acquiring/acquiring-sdk-go/communicator"
"github.com/Worldline-Acquiring/acquiring-sdk-go/configuration"
"github.com/Worldline-Acquiring/acquiring-sdk-go/json"
)
// CreateOAuth2Configuration creates a CommunicatorConfiguration with default OAuth2 settings and the given clientID, clientSecret and tokenURI
func CreateOAuth2Configuration(clientID, clientSecret, tokenURI, integrator string) (*configuration.CommunicatorConfiguration, error) {
return configuration.DefaultOAuth2Configuration(clientID, clientSecret, tokenURI, integrator)
}
// CreateCommunicatorBuilderFromConfiguration creates a CommunicatorBuilder with the given CommunicatorConfiguration
func CreateCommunicatorBuilderFromConfiguration(config *configuration.CommunicatorConfiguration) (*CommunicatorBuilder, error) {
builder := NewCommunicatorBuilder()
connection, err := communicator.NewDefaultConnection(config.SocketTimeout,
config.ConnectTimeout,
config.KeepAliveTimeout,
config.IdleTimeout,
config.MaxConnections,
config.Proxy)
if err != nil {
return nil, err
}
metadataProviderBuilder, err := communicator.NewMetadataProviderBuilder(config.Integrator)
if err != nil {
return nil, err
}
metadataProviderBuilder.ShoppingCartExtension = config.ShoppingCartExtension
metadataProvider, err := metadataProviderBuilder.Build()
if err != nil {
return nil, err
}
authenticator, err := getAuthenticator(config)
if err != nil {
return nil, err
}
marshaller := json.DefaultMarshaller()
return builder.
WithAPIEndpoint(&config.APIEndpoint).
WithConnection(connection).
WithMetadataProvider(metadataProvider).
WithAuthenticator(authenticator).
WithMarshaller(marshaller), nil
}
func getAuthenticator(conf *configuration.CommunicatorConfiguration) (authentication.Authenticator, error) {
if conf.AuthorizationType == configuration.OAUTH2 {
return oauth2.NewAuthenticator(conf)
}
return nil, errors.New("unknown authorizationType " + string(conf.AuthorizationType))
}
// CreateCommunicatorFromConfiguration creates a Communicator with the given CommunicatorConfiguration
func CreateCommunicatorFromConfiguration(conf *configuration.CommunicatorConfiguration) (*communicator.Communicator, error) {
builder, err := CreateCommunicatorBuilderFromConfiguration(conf)
if err != nil {
return nil, err
}
return builder.Build()
}
// CreateCommunicatorWithDefaultMarshaller creates a Communicator with the given components and a default marshaller
func CreateCommunicatorWithDefaultMarshaller(apiEndpoint *url.URL, connection communicator.Connection, authenticator authentication.Authenticator, metadataProvider *communicator.MetadataProvider) (*communicator.Communicator, error) {
return communicator.NewCommunicator(apiEndpoint, connection, authenticator, metadataProvider, json.DefaultMarshaller())
}
// CreateClientFromConfiguration creates a Client with the given CommunicatorConfiguration
func CreateClientFromConfiguration(config *configuration.CommunicatorConfiguration) (*Client, error) {
comm, err := CreateCommunicatorFromConfiguration(config)
if err != nil {
return nil, err
}
return CreateClientFromCommunicator(comm)
}
// CreateClientWithDefaultMarshaller creates a Client with the given components and a default marshaller
func CreateClientWithDefaultMarshaller(apiEndpoint *url.URL, connection communicator.Connection, authenticator authentication.Authenticator, metadataProvider *communicator.MetadataProvider) (*Client, error) {
comm, err := CreateCommunicatorWithDefaultMarshaller(apiEndpoint, connection, authenticator, metadataProvider)
if err != nil {
return nil, err
}
return CreateClientFromCommunicator(comm)
}
// CreateClientFromCommunicator creates a Client with the given Communicator
func CreateClientFromCommunicator(communicator *communicator.Communicator) (*Client, error) {
return NewClient(communicator)
}
// NewCallContext creates an empty CallContext
func NewCallContext() *communicator.CallContext {
return communicator.NewCallContext()
}