77package me .pagar .api ;
88
99import io .apimatic .core .GlobalConfiguration ;
10+ import io .apimatic .coreinterfaces .authentication .Authentication ;
1011import io .apimatic .coreinterfaces .compatibility .CompatibilityFactory ;
1112import io .apimatic .coreinterfaces .http .HttpClient ;
1213import io .apimatic .okhttpclient .adapter .OkClient ;
1314import java .util .HashMap ;
1415import java .util .Map ;
1516import java .util .function .Consumer ;
17+ import me .pagar .api .authentication .BasicAuthManager ;
18+ import me .pagar .api .authentication .BasicAuthModel ;
1619import me .pagar .api .controllers .BalanceOperationsController ;
1720import me .pagar .api .controllers .ChargesController ;
1821import me .pagar .api .controllers .CustomersController ;
@@ -65,7 +68,7 @@ public final class PagarmeApiSDKClient implements PagarmeApiSDKClientInterface {
6568
6669 private static final CompatibilityFactory compatibilityFactory = new CompatibilityFactoryImpl ();
6770
68- private static String userAgent = "PagarmeApiSDK - Java 6.8.5 " ;
71+ private static String userAgent = "PagarmeApiSDK - Java 6.8.6 " ;
6972
7073 /**
7174 * Current API environment.
@@ -87,15 +90,47 @@ public final class PagarmeApiSDKClient implements PagarmeApiSDKClientInterface {
8790 */
8891 private final ReadonlyHttpClientConfiguration httpClientConfig ;
8992
93+ /**
94+ * BasicAuthManager.
95+ */
96+ private BasicAuthManager basicAuthManager ;
97+
98+ /**
99+ * The instance of BasicAuthModel.
100+ */
101+ private BasicAuthModel basicAuthModel ;
102+
103+ /**
104+ * Map of authentication Managers.
105+ */
106+ private Map <String , Authentication > authentications ;
107+
90108 private PagarmeApiSDKClient (Environment environment , String serviceRefererName ,
91- HttpClient httpClient , ReadonlyHttpClientConfiguration httpClientConfig ) {
109+ HttpClient httpClient , ReadonlyHttpClientConfiguration httpClientConfig ,
110+ BasicAuthModel basicAuthModel , Map <String , Authentication > authentications ) {
92111 this .environment = environment ;
93112 this .serviceRefererName = serviceRefererName ;
94113 this .httpClient = httpClient ;
95114 this .httpClientConfig = httpClientConfig ;
115+ this .authentications =
116+ (authentications == null ) ? new HashMap <>() : new HashMap <>(authentications );
117+ this .basicAuthModel = basicAuthModel ;
118+
119+ if (this .authentications .containsKey ("httpBasic" )) {
120+ this .basicAuthManager = (BasicAuthManager ) this .authentications .get ("httpBasic" );
121+ }
122+
123+ if (!this .authentications .containsKey ("httpBasic" )
124+ || !getBasicAuthCredentials ().equals (basicAuthModel .getUsername (),
125+ basicAuthModel .getPassword ())) {
126+ this .basicAuthManager = new BasicAuthManager (basicAuthModel );
127+ this .authentications .put ("httpBasic" , basicAuthManager );
128+ }
129+
96130 GlobalConfiguration globalConfig = new GlobalConfiguration .Builder ()
97131 .httpClient (httpClient ).baseUri (server -> getBaseUri (server ))
98132 .compatibilityFactory (compatibilityFactory )
133+ .authentication (this .authentications )
99134 .userAgent (userAgent )
100135 .globalHeader ("ServiceRefererName" , serviceRefererName )
101136 .build ();
@@ -248,6 +283,21 @@ public ReadonlyHttpClientConfiguration getHttpClientConfig() {
248283 return httpClientConfig ;
249284 }
250285
286+ /**
287+ * The credentials to use with BasicAuth.
288+ * @return basicAuthCredentials
289+ */
290+ public BasicAuthCredentials getBasicAuthCredentials () {
291+ return basicAuthManager ;
292+ }
293+
294+ /**
295+ * The auth credential model for BasicAuth.
296+ * @return the instance of BasicAuthModel
297+ */
298+ public BasicAuthModel getBasicAuthModel () {
299+ return basicAuthModel ;
300+ }
251301 /**
252302 * The timeout to use for making HTTP requests.
253303 * @deprecated This method will be removed in a future version. Use
@@ -311,7 +361,8 @@ private static String environmentMapper(Environment environment, Server server)
311361 @ Override
312362 public String toString () {
313363 return "PagarmeApiSDKClient [" + "environment=" + environment + ", serviceRefererName="
314- + serviceRefererName + ", httpClientConfig=" + httpClientConfig + "]" ;
364+ + serviceRefererName + ", httpClientConfig=" + httpClientConfig
365+ + ", authentications=" + authentications + "]" ;
315366 }
316367
317368 /**
@@ -324,6 +375,9 @@ public Builder newBuilder() {
324375 builder .environment = getEnvironment ();
325376 builder .serviceRefererName = getServiceRefererName ();
326377 builder .httpClient = getHttpClient ();
378+ builder .basicAuthCredentials (getBasicAuthModel ()
379+ .toBuilder ().build ());
380+ builder .authentications = authentications ;
327381 builder .httpClientConfig (configBldr -> configBldr =
328382 ((HttpClientConfiguration ) httpClientConfig ).newBuilder ());
329383 return builder ;
@@ -337,10 +391,39 @@ public static class Builder {
337391 private Environment environment = Environment .PRODUCTION ;
338392 private String serviceRefererName = "" ;
339393 private HttpClient httpClient ;
394+ private BasicAuthModel basicAuthModel = new BasicAuthModel .Builder ("" , "" ).build ();
395+ private Map <String , Authentication > authentications = null ;
340396 private HttpClientConfiguration .Builder httpClientConfigBuilder =
341397 new HttpClientConfiguration .Builder ();
342398
343399
400+ /**
401+ * Credentials setter for BasicAuth.
402+ * @param basicAuthUserName String value for basicAuthUserName.
403+ * @param basicAuthPassword String value for basicAuthPassword.
404+ * @deprecated This builder method is deprecated.
405+ * Use {@link #basicAuthCredentials(BasicAuthModel) basicAuthCredentials} instead.
406+ * @return The current instance of builder.
407+ */
408+ @ Deprecated
409+ public Builder basicAuthCredentials (String basicAuthUserName , String basicAuthPassword ) {
410+ basicAuthModel = basicAuthModel .toBuilder ()
411+ .username (basicAuthUserName )
412+ .password (basicAuthPassword )
413+ .build ();
414+ return this ;
415+ }
416+
417+ /**
418+ * Credentials setter for BasicAuthCredentials.
419+ * @param basicAuthModel The instance of BasicAuthModel.
420+ * @return The current instance of builder.
421+ */
422+ public Builder basicAuthCredentials (BasicAuthModel basicAuthModel ) {
423+ this .basicAuthModel = basicAuthModel ;
424+ return this ;
425+ }
426+
344427 /**
345428 * Current API environment.
346429 * @param environment The environment for client.
@@ -398,7 +481,7 @@ public PagarmeApiSDKClient build() {
398481 httpClient = new OkClient (httpClientConfig .getConfiguration (), compatibilityFactory );
399482
400483 return new PagarmeApiSDKClient (environment , serviceRefererName , httpClient ,
401- httpClientConfig );
484+ httpClientConfig , basicAuthModel , authentications );
402485 }
403486 }
404487}
0 commit comments