• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TrueLayer / truelayer-java / #97

15 Apr 2024 02:02PM UTC coverage: 90.566% (-0.6%) from 91.176%
#97

push

github

web-flow
feat(EWT-535): Support for merchant account transactions pagination (#279)

4 of 8 new or added lines in 2 files covered. (50.0%)

13 existing lines in 2 files now uncovered.

432 of 477 relevant lines covered (90.57%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.63
/src/main/java/com/truelayer/java/TrueLayerClientBuilder.java
1
package com.truelayer.java;
2

3
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
4
import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;
5

6
import com.truelayer.java.auth.AuthenticationHandler;
7
import com.truelayer.java.auth.IAuthenticationHandler;
8
import com.truelayer.java.commonapi.CommonHandler;
9
import com.truelayer.java.commonapi.ICommonApi;
10
import com.truelayer.java.commonapi.ICommonHandler;
11
import com.truelayer.java.entities.RequestScopes;
12
import com.truelayer.java.hpp.HostedPaymentPageLinkBuilder;
13
import com.truelayer.java.hpp.IHostedPaymentPageLinkBuilder;
14
import com.truelayer.java.http.OkHttpClientFactory;
15
import com.truelayer.java.http.RetrofitFactory;
16
import com.truelayer.java.http.auth.cache.ICredentialsCache;
17
import com.truelayer.java.http.auth.cache.SimpleCredentialsCache;
18
import com.truelayer.java.http.interceptors.logging.DefaultLogConsumer;
19
import com.truelayer.java.mandates.IMandatesApi;
20
import com.truelayer.java.mandates.IMandatesHandler;
21
import com.truelayer.java.mandates.MandatesHandler;
22
import com.truelayer.java.merchantaccounts.IMerchantAccountsApi;
23
import com.truelayer.java.merchantaccounts.IMerchantAccountsHandler;
24
import com.truelayer.java.merchantaccounts.MerchantAccountsHandler;
25
import com.truelayer.java.payments.IPaymentsApi;
26
import com.truelayer.java.payments.IPaymentsHandler;
27
import com.truelayer.java.payments.PaymentsHandler;
28
import com.truelayer.java.paymentsproviders.IPaymentsProvidersApi;
29
import com.truelayer.java.paymentsproviders.IPaymentsProvidersHandler;
30
import com.truelayer.java.paymentsproviders.PaymentsProvidersHandler;
31
import com.truelayer.java.payouts.IPayoutsApi;
32
import com.truelayer.java.payouts.IPayoutsHandler;
33
import com.truelayer.java.payouts.PayoutsHandler;
34
import com.truelayer.java.versioninfo.LibraryInfoLoader;
35
import java.time.Clock;
36
import java.time.Duration;
37
import java.util.concurrent.ExecutorService;
38
import java.util.function.Consumer;
39
import okhttp3.OkHttpClient;
40

41
/**
42
 * Builder class for TrueLayerClient instances.
43
 */
44
public class TrueLayerClientBuilder {
45
    private ClientCredentials clientCredentials;
46

47
    private RequestScopes globalScopes;
48

49
    private SigningOptions signingOptions;
50

51
    /**
52
     * Optional timeout configuration that defines a time limit for a complete HTTP call.
53
     * This includes resolving DNS, connecting, writing the request body, server processing, as well as
54
     * reading the response body. If not set, the internal HTTP client configuration are used.
55
     */
56
    private Duration timeout;
57

58
    /**
59
     * Optional configuration for internal connection pool.
60
     */
61
    private ConnectionPoolOptions connectionPoolOptions;
62

63
    /**
64
     * Optional execution service to be used by the internal HTTP client.
65
     */
66
    private ExecutorService requestExecutor;
67

68
    // By default, production is used
69
    private Environment environment = Environment.live();
1✔
70

71
    private Consumer<String> logMessageConsumer;
72

73
    private ICredentialsCache credentialsCache;
74

75
    private ProxyConfiguration proxyConfiguration;
76

77
    TrueLayerClientBuilder() {}
1✔
78

79
    /**
80
     * Utility to set the client credentials required for Oauth2 protected endpoints.
81
     * @param credentials the credentials object that holds client id and secret.
82
     * @return the instance of the client builder used.
83
     * @see ClientCredentials
84
     */
85
    public TrueLayerClientBuilder clientCredentials(ClientCredentials credentials) {
86
        this.clientCredentials = credentials;
1✔
87
        return this;
1✔
88
    }
89

90
    /**
91
     * Utility to set the signing options required for payments.
92
     * @param signingOptions the signing options object that holds signature related information.
93
     * @return the instance of the client builder used.
94
     * @see SigningOptions
95
     */
96
    public TrueLayerClientBuilder signingOptions(SigningOptions signingOptions) {
97
        this.signingOptions = signingOptions;
1✔
98
        return this;
1✔
99
    }
100

101
    /**
102
     * Utility to set custom global scopes used by the library. If used, the specified scopes will override the
103
     * default scopes used by the library. If using this option, make sure to set valid scopes for all the API interactions
104
     * that your integration will have.
105
     * @param globalScopes custom global scopes to be used by the library for all authenticated endpoints.
106
     * @return the instance of the client builder used.
107
     */
108
    public TrueLayerClientBuilder withGlobalScopes(RequestScopes globalScopes) {
109
        this.globalScopes = globalScopes;
×
UNCOV
110
        return this;
×
111
    }
112

113
    /**
114
     * Utility to set a call timeout for the client.
115
     * @param timeout Optional timeout configuration that defines a time limit for a complete HTTP call.
116
     * This includes resolving DNS, connecting, writing the request body, server processing, as well as
117
     * reading the response body. If not set, the internal HTTP client configuration are used.
118
     * @return the instance of the client builder used.
119
     */
120
    public TrueLayerClientBuilder withTimeout(Duration timeout) {
121
        this.timeout = timeout;
×
UNCOV
122
        return this;
×
123
    }
124

125
    /**
126
     * Sets a connection pool for the internal HTTP client
127
     * @param connectionPoolOptions optional connection pool to be used
128
     * @return the instance of the client builder used.
129
     */
130
    public TrueLayerClientBuilder withConnectionPool(ConnectionPoolOptions connectionPoolOptions) {
131
        this.connectionPoolOptions = connectionPoolOptions;
×
UNCOV
132
        return this;
×
133
    }
134

135
    /**
136
     * Sets a custom HTTP request dispatcher for the internal HTTP client
137
     * @param requestExecutor an executor service responsible for handling the HTTP requests
138
     * @return the instance of the client builder used.
139
     */
140
    public TrueLayerClientBuilder withRequestExecutor(ExecutorService requestExecutor) {
141
        this.requestExecutor = requestExecutor;
×
UNCOV
142
        return this;
×
143
    }
144

145
    /**
146
     * Utility to configure the library to interact a specific <i>TrueLayer</i> environment.
147
     * By default, <i>TrueLayer</i> production environment is used.
148
     * @param environment the environment to use
149
     * @return the instance of the client builder used.
150
     * @see Environment
151
     */
152
    public TrueLayerClientBuilder environment(Environment environment) {
153
        this.environment = environment;
×
UNCOV
154
        return this;
×
155
    }
156

157
    /**
158
     * Utility to enable default logs for HTTP traces.
159
     * @return the instance of the client builder used
160
     */
161
    public TrueLayerClientBuilder withHttpLogs() {
162
        this.logMessageConsumer = new DefaultLogConsumer();
1✔
163
        return this;
1✔
164
    }
165

166
    /**
167
     * Utility to enable custom logging for HTTP traces. Please notice that blocking
168
     * in the context of this consumer invocation will affect performance. An asynchronous implementation is
169
     * strongly advised.
170
     * @param logConsumer a custom log consumer
171
     * @return the instance of the client builder used
172
     */
173
    public TrueLayerClientBuilder withHttpLogs(Consumer<String> logConsumer) {
174
        this.logMessageConsumer = logConsumer;
1✔
175
        return this;
1✔
176
    }
177

178
    /**
179
     * Utility to enable default in memory caching for Oauth credentials.
180
     * @return the instance of the client builder used
181
     */
182
    public TrueLayerClientBuilder withCredentialsCaching() {
183
        this.credentialsCache = new SimpleCredentialsCache(Clock.systemUTC());
1✔
184
        return this;
1✔
185
    }
186

187
    /**
188
     * Utility to enable a custom cache for Oauth credentials.
189
     * @return the instance of the client builder used
190
     */
191
    public TrueLayerClientBuilder withCredentialsCaching(ICredentialsCache credentialsCache) {
192
        this.credentialsCache = credentialsCache;
1✔
193
        return this;
1✔
194
    }
195

196
    /**
197
     * Utility to configure a custom proxy, optionally including an authentication.
198
     * @param proxyConfiguration the configuration describing the custom proxy
199
     * @return the instance of the client builder used
200
     */
201
    public TrueLayerClientBuilder withProxyConfiguration(ProxyConfiguration proxyConfiguration) {
202
        this.proxyConfiguration = proxyConfiguration;
×
UNCOV
203
        return this;
×
204
    }
205

206
    /**
207
     * Builds the Java library main class to interact with TrueLayer APIs.
208
     * @return a client instance
209
     * @see TrueLayerClient
210
     */
211
    public TrueLayerClient build() {
212
        if (isEmpty(clientCredentials)) {
1✔
213
            throw new TrueLayerException("client credentials must be set");
1✔
214
        }
215

216
        OkHttpClientFactory httpClientFactory = new OkHttpClientFactory(new LibraryInfoLoader());
1✔
217

218
        OkHttpClient baseHttpClient = httpClientFactory.buildBaseApiClient(
1✔
219
                timeout, connectionPoolOptions, requestExecutor, logMessageConsumer, proxyConfiguration);
220

221
        OkHttpClient authHttpClient = httpClientFactory.buildAuthApiClient(baseHttpClient, clientCredentials);
1✔
222

223
        IAuthenticationHandler authenticationHandler = AuthenticationHandler.New()
1✔
224
                .clientCredentials(clientCredentials)
1✔
225
                .httpClient(RetrofitFactory.build(authHttpClient, environment.getAuthApiUri()))
1✔
226
                .build();
1✔
227

228
        IHostedPaymentPageLinkBuilder hppLinkBuilder =
229
                HostedPaymentPageLinkBuilder.New().uri(environment.getHppUri()).build();
1✔
230

231
        // We're reusing a client with only User agent and Idempotency key interceptors and give it our base payment
232
        // endpoint
233
        ICommonApi commonApi = RetrofitFactory.build(authHttpClient, environment.getPaymentsApiUri())
1✔
234
                .create(ICommonApi.class);
1✔
235
        ICommonHandler commonHandler = new CommonHandler(commonApi);
1✔
236

237
        // As per our RFC, if signing options is not configured we create a client which is able to interact
238
        // with the Authentication API only
239
        if (isEmpty(signingOptions)) {
1✔
240
            return new TrueLayerClient(authenticationHandler, hppLinkBuilder, commonHandler);
1✔
241
        }
242

243
        OkHttpClient paymentsHttpClient = httpClientFactory.buildPaymentsApiClient(
1✔
244
                authHttpClient, authenticationHandler, signingOptions, credentialsCache);
245

246
        IPaymentsApi paymentsApi = RetrofitFactory.build(paymentsHttpClient, environment.getPaymentsApiUri())
1✔
247
                .create(IPaymentsApi.class);
1✔
248

249
        PaymentsHandler.PaymentsHandlerBuilder paymentsHandlerBuilder =
250
                PaymentsHandler.builder().paymentsApi(paymentsApi);
1✔
251
        if (customScopesPresent()) {
1✔
UNCOV
252
            paymentsHandlerBuilder.scopes(globalScopes);
×
253
        }
254
        IPaymentsHandler paymentsHandler = paymentsHandlerBuilder.build();
1✔
255

256
        IPaymentsProvidersApi paymentsProvidersApi = RetrofitFactory.build(
1✔
257
                        paymentsHttpClient, environment.getPaymentsApiUri())
1✔
258
                .create(IPaymentsProvidersApi.class);
1✔
259

260
        PaymentsProvidersHandler.PaymentsProvidersHandlerBuilder paymentsProvidersHandlerBuilder =
261
                PaymentsProvidersHandler.builder().paymentsProvidersApi(paymentsProvidersApi);
1✔
262
        if (customScopesPresent()) {
1✔
UNCOV
263
            paymentsProvidersHandlerBuilder.scopes(globalScopes);
×
264
        }
265
        IPaymentsProvidersHandler paymentsProvidersHandler = paymentsProvidersHandlerBuilder.build();
1✔
266

267
        IMerchantAccountsApi merchantAccountsApi = RetrofitFactory.build(
1✔
268
                        paymentsHttpClient, environment.getPaymentsApiUri())
1✔
269
                .create(IMerchantAccountsApi.class);
1✔
270
        MerchantAccountsHandler.MerchantAccountsHandlerBuilder merchantAccountsHandlerBuilder =
271
                MerchantAccountsHandler.builder().merchantAccountsApi(merchantAccountsApi);
1✔
272
        if (customScopesPresent()) {
1✔
UNCOV
273
            merchantAccountsHandlerBuilder.scopes(globalScopes);
×
274
        }
275
        IMerchantAccountsHandler merchantAccountsHandler = merchantAccountsHandlerBuilder.build();
1✔
276

277
        IMandatesApi mandatesApi = RetrofitFactory.build(paymentsHttpClient, environment.getPaymentsApiUri())
1✔
278
                .create(IMandatesApi.class);
1✔
279
        MandatesHandler.MandatesHandlerBuilder mandatesHandlerBuilder =
280
                MandatesHandler.builder().mandatesApi(mandatesApi);
1✔
281
        if (customScopesPresent()) {
1✔
UNCOV
282
            mandatesHandlerBuilder.scopes(globalScopes);
×
283
        }
284
        IMandatesHandler mandatesHandler = mandatesHandlerBuilder.build();
1✔
285

286
        IPayoutsApi payoutsApi = RetrofitFactory.build(paymentsHttpClient, environment.getPaymentsApiUri())
1✔
287
                .create(IPayoutsApi.class);
1✔
288
        PayoutsHandler.PayoutsHandlerBuilder payoutsHandlerBuilder =
289
                PayoutsHandler.builder().payoutsApi(payoutsApi);
1✔
290
        if (customScopesPresent()) {
1✔
UNCOV
291
            merchantAccountsHandlerBuilder.scopes(globalScopes);
×
292
        }
293
        IPayoutsHandler payoutsHandler = payoutsHandlerBuilder.build();
1✔
294

295
        return new TrueLayerClient(
1✔
296
                authenticationHandler,
297
                paymentsHandler,
298
                paymentsProvidersHandler,
299
                merchantAccountsHandler,
300
                mandatesHandler,
301
                payoutsHandler,
302
                commonHandler,
303
                hppLinkBuilder);
304
    }
305

306
    private boolean customScopesPresent() {
307
        return isNotEmpty(globalScopes) && isNotEmpty(globalScopes.getScopes());
1✔
308
    }
309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc