3939import com .amplifyframework .core .Amplify ;
4040import com .amplifyframework .core .Consumer ;
4141import com .amplifyframework .hub .HubChannel ;
42+ import com .amplifyframework .util .Immutable ;
4243import com .amplifyframework .util .UserAgent ;
4344
4445import org .json .JSONObject ;
6970@ SuppressWarnings ("TypeParameterHidesVisibleType" ) // <R> shadows >com.amplifyframework.api.aws.R
7071public final class AWSApiPlugin extends ApiPlugin <Map <String , OkHttpClient >> {
7172 private final Map <String , ClientDetails > apiDetails ;
73+ private final Map <String , OkHttpConfigurator > apiConfigurators ;
7274 private final GraphQLResponse .Factory gqlResponseFactory ;
7375 private final ApiAuthProviders authProvider ;
7476 private final ExecutorService executorService ;
@@ -78,30 +80,39 @@ public final class AWSApiPlugin extends ApiPlugin<Map<String, OkHttpClient>> {
7880 private final Set <String > gqlApis ;
7981
8082 /**
81- * Default constructor for this plugin without any override .
83+ * Default constructor for this plugin without any overrides .
8284 */
8385 public AWSApiPlugin () {
84- this (ApiAuthProviders . noProviderOverrides ());
86+ this (builder ());
8587 }
8688
8789 /**
88- * Constructs an instance of AWSApiPlugin with
89- * configured auth providers to override default modes
90- * of authorization.
91- * If no Auth provider implementation is provided, then
92- * the plugin will assume default behavior for that specific
93- * mode of authorization.
94- *
95- * @param apiAuthProvider configured instance of {@link ApiAuthProviders}
90+ * Deprecated. Use {@link #builder()} instead.
91+ * @param apiAuthProvider Don't use this
92+ * @deprecated Use the fluent {@link #builder()}, instead.
9693 */
94+ @ Deprecated
9795 public AWSApiPlugin (@ NonNull ApiAuthProviders apiAuthProvider ) {
96+ this (builder ().apiAuthProviders (apiAuthProvider ));
97+ }
98+
99+ private AWSApiPlugin (@ NonNull Builder builder ) {
98100 this .apiDetails = new HashMap <>();
99101 this .gqlResponseFactory = new GsonGraphQLResponseFactory ();
100- this .authProvider = Objects . requireNonNull ( apiAuthProvider ) ;
102+ this .authProvider = builder . apiAuthProviders ;
101103 this .restApis = new HashSet <>();
102104 this .gqlApis = new HashSet <>();
103105 this .executorService = Executors .newCachedThreadPool ();
104106 this .requestDecorator = new AuthRuleRequestDecorator (authProvider );
107+ this .apiConfigurators = Immutable .of (builder .apiConfigurators );
108+ }
109+
110+ /**
111+ * Begins construction of a new AWSApiPlugin instance by using a fluent builder.
112+ * @return A builder to help construct an AWSApiPlugin
113+ */
114+ public static Builder builder () {
115+ return new Builder ();
105116 }
106117
107118 @ NonNull
@@ -132,7 +143,13 @@ public void configure(
132143 if (apiConfiguration .getAuthorizationType () != AuthorizationType .NONE ) {
133144 builder .addInterceptor (interceptorFactory .create (apiConfiguration ));
134145 }
146+
147+ OkHttpConfigurator configurator = apiConfigurators .get (apiName );
148+ if (configurator != null ) {
149+ configurator .applyConfiguration (builder );
150+ }
135151 final OkHttpClient okHttpClient = builder .build ();
152+
136153 final SubscriptionAuthorizer subscriptionAuthorizer =
137154 new SubscriptionAuthorizer (apiConfiguration , authProvider );
138155 final SubscriptionEndpoint subscriptionEndpoint =
@@ -736,4 +753,54 @@ private void transitionTo(ApiEndpointStatus newStatus) {
736753 }
737754 }
738755 }
756+
757+ /**
758+ * Builds an {@link AWSApiPlugin}.
759+ */
760+ public static final class Builder {
761+ private ApiAuthProviders apiAuthProviders ;
762+ private final Map <String , OkHttpConfigurator > apiConfigurators ;
763+
764+ private Builder () {
765+ this .apiAuthProviders = ApiAuthProviders .noProviderOverrides ();
766+ this .apiConfigurators = new HashMap <>();
767+ }
768+
769+ /**
770+ * Specify authentication providers.
771+ * @param apiAuthProviders A set of authentication providers to use for API calls
772+ * @return Current builder instance, for fluent construction of plugin
773+ */
774+ @ NonNull
775+ public Builder apiAuthProviders (@ NonNull ApiAuthProviders apiAuthProviders ) {
776+ Objects .requireNonNull (apiAuthProviders );
777+ Builder .this .apiAuthProviders = apiAuthProviders ;
778+ return Builder .this ;
779+ }
780+
781+ /**
782+ * Apply customizations to an underlying OkHttpClient that will be used
783+ * for a particular API.
784+ * @param forApiName The name of the API for which these customizations should apply.
785+ This can be found in your `amplifyconfiguration.json` file.
786+ * @param byConfigurator A lambda that hands the user an OkHttpClient.Builder,
787+ * and enables the user to set come configurations on it.
788+ * @return A builder instance, to continue chaining configurations
789+ */
790+ @ NonNull
791+ public Builder configureClient (
792+ @ NonNull String forApiName , @ NonNull OkHttpConfigurator byConfigurator ) {
793+ this .apiConfigurators .put (forApiName , byConfigurator );
794+ return this ;
795+ }
796+
797+ /**
798+ * Builds an {@link AWSApiPlugin}.
799+ * @return An AWSApiPlugin
800+ */
801+ @ NonNull
802+ public AWSApiPlugin build () {
803+ return new AWSApiPlugin (Builder .this );
804+ }
805+ }
739806}
0 commit comments