View Javadoc
1   package com.guinetik.rr;
2   
3   import com.guinetik.rr.auth.AuthStrategy;
4   import com.guinetik.rr.auth.AuthStrategyFactory;
5   
6   import java.util.function.Consumer;
7   
8   /**
9    * Configuration container for {@link RocketRest} clients.
10   *
11   * <p>This class holds all configuration options needed to create a RocketRest client,
12   * including the service URL, authentication strategy, and default request options.
13   * Use the fluent {@link Builder} to construct instances.
14   *
15   * <h2>Basic Usage</h2>
16   * <pre class="language-java"><code>
17   * RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
18   *     .build();
19   *
20   * RocketRest client = new RocketRest(config);
21   * </code></pre>
22   *
23   * <h2>With Authentication</h2>
24   * <pre class="language-java"><code>
25   * RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
26   *     .authStrategy(AuthStrategyFactory.createBearerToken("my-api-token"))
27   *     .build();
28   * </code></pre>
29   *
30   * <h2>With Default Options</h2>
31   * <pre class="language-java"><code>
32   * RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
33   *     .authStrategy(AuthStrategyFactory.createBasicAuth("user", "pass"))
34   *     .defaultOptions(options -&gt; {
35   *         options.set(RocketRestOptions.RETRY_ENABLED, true);
36   *         options.set(RocketRestOptions.MAX_RETRIES, 3);
37   *         options.set(RocketRestOptions.CONNECT_TIMEOUT, 5000);
38   *         options.set(RocketRestOptions.LOG_REQUEST_BODY, true);
39   *     })
40   *     .build();
41   * </code></pre>
42   *
43   * <h2>OAuth2 Client Credentials</h2>
44   * <pre class="language-java"><code>
45   * RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
46   *     .tokenUrl("https://auth.example.com/oauth/token")
47   *     .authStrategy(AuthStrategyFactory.createOAuth2ClientCredentials(
48   *         "https://auth.example.com/oauth/token",
49   *         "client-id",
50   *         "client-secret"
51   *     ))
52   *     .build();
53   * </code></pre>
54   *
55   * @author guinetik &lt;guinetik@gmail.com&gt;
56   * @see RocketRest
57   * @see RocketRestOptions
58   * @see com.guinetik.rr.auth.AuthStrategy
59   * @since 1.0.0
60   */
61  public class RocketRestConfig {
62  
63      /**
64       * Builder for creating RocketRestConfig instances.
65       */
66      public static class Builder {
67          private final String serviceUrl;
68          private final RocketRestOptions defaultOptions = new RocketRestOptions();
69          private String tokenUrl;
70          private AuthStrategy authStrategy = AuthStrategyFactory.createNoAuth();
71  
72          public Builder(String serviceUrl) {
73              this.serviceUrl = serviceUrl;
74          }
75  
76          /**
77           * Sets the token URL for OAuth flows.
78           *
79           * @param tokenUrl the token URL
80           * @return this builder instance
81           */
82          public Builder tokenUrl(String tokenUrl) {
83              this.tokenUrl = tokenUrl;
84              return this;
85          }
86  
87          /**
88           * Sets the authentication strategy.
89           *
90           * @param authStrategy the authentication strategy
91           * @return this builder instance
92           */
93          public Builder authStrategy(AuthStrategy authStrategy) {
94              this.authStrategy = authStrategy;
95              return this;
96          }
97  
98  
99          /**
100          * Sets default client options that will be used by clients created with this config.
101          *
102          * @param optionsConfigurer a consumer that configures the default options
103          * @return this builder instance
104          */
105         public Builder defaultOptions(Consumer<RocketRestOptions> optionsConfigurer) {
106             optionsConfigurer.accept(this.defaultOptions);
107             return this;
108         }
109 
110         /**
111          * Sets a specific default client option.
112          *
113          * @param key   the option key
114          * @param value the option value
115          * @return this builder instance
116          */
117         public Builder defaultOption(String key, Object value) {
118             this.defaultOptions.set(key, value);
119             return this;
120         }
121 
122         /**
123          * Builds a new RocketRestConfig instance.
124          *
125          * @return a new RocketRestConfig instance
126          */
127         public RocketRestConfig build() {
128             return new RocketRestConfig(this);
129         }
130     }
131     private String serviceUrl;
132     private final String tokenUrl;
133     private final AuthStrategy authStrategy;
134     // Default client options
135     private final RocketRestOptions defaultOptions;
136 
137     protected RocketRestConfig(Builder builder) {
138         this.serviceUrl = builder.serviceUrl;
139         this.tokenUrl = builder.tokenUrl;
140         this.authStrategy = builder.authStrategy != null ? builder.authStrategy : AuthStrategyFactory.createNoAuth();
141         this.defaultOptions = builder.defaultOptions;
142     }
143 
144     /**
145      * Gets the service URL.
146      *
147      * @return the service URL
148      */
149     public String getServiceUrl() {
150         return serviceUrl;
151     }
152 
153     /**
154      * Gets the token URL for OAuth flows.
155      *
156      * @return the token URL
157      */
158     public String getTokenUrl() {
159         return tokenUrl;
160     }
161 
162     /**
163      * Gets the authentication strategy.
164      *
165      * @return the authentication strategy
166      */
167     public AuthStrategy getAuthStrategy() {
168         return authStrategy;
169     }
170 
171     /**
172      * Gets the default client options for clients created with this config.
173      *
174      * @return the default client options
175      */
176     public RocketRestOptions getDefaultOptions() {
177         return defaultOptions;
178     }
179 
180     public void setServiceUrl(String baseUrl) {
181         this.serviceUrl = baseUrl;
182     }
183 
184     /**
185      * Creates a new builder for RocketRestConfig.
186      *
187      * @param serviceUrl the base URL for the API service
188      * @return a new builder instance
189      */
190     public static Builder builder(String serviceUrl) {
191         return new Builder(serviceUrl);
192     }
193 }