1
1
package ch .postfinance .sdk ;
2
2
3
3
import ch .postfinance .sdk .service .*;
4
+
5
+ import java .net .InetSocketAddress ;
6
+ import java .net .Proxy ;
4
7
import java .util .HashMap ;
5
8
import java .util .Map ;
9
+
6
10
import com .fasterxml .jackson .annotation .JsonInclude ;
7
11
import com .fasterxml .jackson .databind .DeserializationFeature ;
8
12
import com .fasterxml .jackson .databind .ObjectMapper ;
9
13
import com .fasterxml .jackson .databind .SerializationFeature ;
10
14
import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
11
15
import com .google .api .client .http .AbstractHttpContent ;
12
- import com .google .api .client .http .HttpRequest ;
13
16
import com .google .api .client .http .HttpRequestFactory ;
14
- import com .google .api .client .http .HttpRequestInitializer ;
15
17
import com .google .api .client .http .javanet .NetHttpTransport ;
16
18
import com .google .api .client .json .Json ;
17
- import com .google .api .client .http .HttpHeaders ;
18
19
19
20
import java .io .IOException ;
20
21
import java .io .OutputStream ;
21
22
23
+ /**
24
+ * The ApiClient class is responsible for setting up and maintaining the state and configuration
25
+ * necessary to interact with a remote API service.
26
+ */
22
27
23
28
public class ApiClient {
29
+
30
+ private static final String DEFAULT_BASE_PATH = "https://checkout.postfinance.ch:443/api" ;
31
+
32
+ // Configuration fields
24
33
private int readTimeOut = 25 ;
25
- private final String basePath ;
26
- private final HttpRequestFactory httpRequestFactory ;
27
- private final ObjectMapper objectMapper ;
28
- private final long userId ;
29
- private final String applicationKey ;
30
- private final Map <String , String > defaultHeaders ;
34
+ private String basePath ;
35
+ private HttpRequestFactory httpRequestFactory ;
36
+ private ObjectMapper objectMapper ;
37
+ private long userId ;
38
+ private String applicationKey ;
39
+ private Map <String , String > defaultHeaders = new HashMap <>() ;
31
40
32
- // A reasonable default object mapper. Client can pass in a chosen ObjectMapper anyway, this is just for reasonable defaults.
41
+ // Proxy settings
42
+ private String proxyHostname ;
43
+ private int proxyPort ;
44
+
45
+ /**
46
+ * Creates a default ObjectMapper for JSON serialization/deserialization.
47
+ * This mapper will ignore unknown properties and set proper date formats among other configurations.
48
+ */
33
49
private static ObjectMapper createDefaultObjectMapper () {
34
50
ObjectMapper objectMapper = new ObjectMapper ()
35
51
.disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
@@ -40,60 +56,121 @@ private static ObjectMapper createDefaultObjectMapper() {
40
56
return objectMapper ;
41
57
}
42
58
43
-
44
- /**
45
- * Constructor for ApiClient
46
- *
47
- * @param userId
48
- * @param applicationKey
49
- */
50
- public ApiClient (long userId , String applicationKey ) {
51
- this (userId , applicationKey , "https://checkout.postfinance.ch:443/api" );
52
- }
53
-
54
59
/**
55
- * Constructor for ApiClient
56
- *
57
- * @param userId
58
- * @param applicationKey
59
- */
60
- public ApiClient (long userId , String applicationKey , String basePath ) {
60
+ * Validates the primary inputs required for establishing a connection with the API.
61
+ *
62
+ * @param userId The user ID that will be authenticated.
63
+ * @param applicationKey The application key corresponding to the user's account, used for authentication.
64
+ * @param basePath The base URL for the API against which all the requests would be made.
65
+ * @throws IllegalArgumentException if any argument does not meet the criteria.
66
+ */
67
+ private void validateInputs (long userId , String applicationKey , String basePath ) {
61
68
if (applicationKey == null || applicationKey .trim ().isEmpty ()) {
62
- throw new IllegalArgumentException ("The application key cannot be empty or null ." );
69
+ throw new IllegalArgumentException ("Application key cannot be null or empty ." );
63
70
}
64
71
if (userId < 1 ) {
65
- throw new IllegalArgumentException ("The user id is invalid ." );
72
+ throw new IllegalArgumentException ("User ID must be positive ." );
66
73
}
67
74
if (basePath == null || basePath .trim ().isEmpty ()) {
68
- throw new IllegalArgumentException ("The base path cannot be empty." );
69
- }
70
-
71
- this .basePath = basePath ;
75
+ throw new IllegalArgumentException ("Base path cannot be null or empty." );
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Initializes common properties for the API client.
81
+ */
82
+ private void initializeBaseProperties (long userId , String applicationKey , String basePath ) {
83
+ validateInputs (userId , applicationKey , basePath );
84
+ this .basePath = basePath ;
72
85
this .userId = userId ;
73
86
this .applicationKey = applicationKey ;
74
- this .defaultHeaders = new HashMap <>();
75
- this .httpRequestFactory = this .createRequestFactory ();
76
87
this .objectMapper = createDefaultObjectMapper ();
77
88
}
78
89
79
- public HttpRequestFactory getHttpRequestFactory () {
80
- return httpRequestFactory ;
90
+ /**
91
+ * Sets up the proxy properties for the API client.
92
+ */
93
+ private void initializeProxyProperties (String proxyHostname , int proxyPort ) {
94
+ this .proxyHostname = proxyHostname ;
95
+ this .proxyPort = proxyPort ;
81
96
}
82
97
83
- public HttpRequestFactory createRequestFactory () {
98
+ /**
99
+ * Constructs an ApiClient with the default base path.
100
+ */
101
+ public ApiClient (long userId , String applicationKey ) {
102
+ this (userId , applicationKey , DEFAULT_BASE_PATH );
103
+ }
104
+
105
+ /**
106
+ * Constructs an ApiClient with a custom base path.
107
+ */
108
+ public ApiClient (long userId , String applicationKey , String basePath ) {
109
+ initializeBaseProperties (userId , applicationKey , basePath );
110
+ this .httpRequestFactory = createRequestFactory ();
111
+ }
112
+
113
+ /**
114
+ * Constructor for ApiClient specifying user credentials, proxy details, and base path.
115
+ *
116
+ * @param userId user identifier for authentication.
117
+ * @param applicationKey unique application key for user authentication.
118
+ * @param basePath the base URL for API requests.
119
+ * @param proxyHostname the hostname of the proxy server.
120
+ * @param proxyPort the port of the proxy server.
121
+ */
122
+ public ApiClient (long userId , String applicationKey , String basePath , String proxyHostname , int proxyPort ) {
123
+ initializeBaseProperties (userId , applicationKey , basePath );
124
+ initializeProxyProperties (proxyHostname , proxyPort );
125
+ this .httpRequestFactory = createRequestFactory ();
126
+ }
127
+
128
+ /**
129
+ * Constructor for ApiClient specifying user credentials with the default base path and proxy details.
130
+ *
131
+ * @param userId user identifier for authentication.
132
+ * @param applicationKey unique application key for user authentication.
133
+ * @param proxyHostname the hostname of the proxy server.
134
+ * @param proxyPort the port of the proxy server.
135
+ */
136
+ public ApiClient (long userId , String applicationKey , String proxyHostname , int proxyPort ) {
137
+ this (userId , applicationKey , DEFAULT_BASE_PATH , proxyHostname , proxyPort );
138
+ }
139
+
140
+ /**
141
+ * Creates an HttpRequestFactory configured for making HTTP requests. The method initializes a transport builder
142
+ * and potentially sets a proxy for it.
143
+ *
144
+ * @return HttpRequestFactory This factory is configured with the built transport and the interceptor.
145
+ * It is ready for making HTTP requests, handling the details of connection
146
+ * and protocol, allowing for high configurability and ease of modifications.
147
+ */
148
+ private HttpRequestFactory createRequestFactory () {
84
149
final RequestInterceptor interceptor = new RequestInterceptor (this .userId , this .applicationKey , this .defaultHeaders );
85
- NetHttpTransport transport = new NetHttpTransport ();
86
- return transport .createRequestFactory (new HttpRequestInitializer () {
87
- public void initialize (HttpRequest request ) {
88
- request .setInterceptor (interceptor );
89
- }
90
- });
150
+ NetHttpTransport .Builder builder = new NetHttpTransport .Builder ();
151
+
152
+ if (proxyHostname != null && !proxyHostname .isEmpty ()) {
153
+ Proxy proxy = new Proxy (Proxy .Type .HTTP , new InetSocketAddress (proxyHostname , proxyPort ));
154
+ builder .setProxy (proxy );
155
+ }
156
+
157
+ NetHttpTransport transport = builder .build ();
158
+
159
+ return transport .createRequestFactory (request -> request .setInterceptor (interceptor ));
91
160
}
92
161
162
+ /**
163
+ * Allows the addition of default headers that will be sent with each request.
164
+ */
93
165
public void addDefaultHeader (String key , String value ) {
94
166
this .defaultHeaders .put (key , value );
95
167
}
96
168
169
+ // Standard getters and setters
170
+ public HttpRequestFactory getHttpRequestFactory () {
171
+ return httpRequestFactory ;
172
+ }
173
+
97
174
public String getBasePath () {
98
175
return basePath ;
99
176
}
0 commit comments