View Javadoc
1   package com.guinetik.rr.api;
2   
3   import com.guinetik.rr.RocketRestConfig;
4   import com.guinetik.rr.http.RocketClient;
5   import com.guinetik.rr.http.RocketClientFactory;
6   
7   /**
8    * Default synchronous API client implementation.
9    *
10   * <p>This client executes HTTP requests synchronously, blocking the calling thread
11   * until the response is received. It's the simplest way to interact with REST APIs
12   * when you don't need asynchronous processing.
13   *
14   * <h2>Basic Usage</h2>
15   * <pre class="language-java"><code>
16   * RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
17   *     .authStrategy(AuthStrategyFactory.createBearerToken("token"))
18   *     .build();
19   *
20   * DefaultApiClient client = new DefaultApiClient("https://api.example.com", config);
21   *
22   * // Execute a GET request (blocks until response)
23   * RequestSpec&lt;Void, User&gt; request = RequestBuilder.get("/users/1")
24   *     .responseType(User.class)
25   *     .build();
26   *
27   * User user = client.execute(request);
28   * </code></pre>
29   *
30   * <h2>With Custom HTTP Client</h2>
31   * <pre class="language-java"><code>
32   * // Create client with circuit breaker
33   * RocketClient httpClient = RocketClientFactory.fromConfig(config)
34   *     .withCircuitBreaker(5, 30000)
35   *     .build();
36   *
37   * DefaultApiClient client = new DefaultApiClient(baseUrl, config, httpClient);
38   * </code></pre>
39   *
40   * @author guinetik &lt;guinetik@gmail.com&gt;
41   * @see AbstractApiClient
42   * @see AsyncApiClient
43   * @see FluentApiClient
44   * @since 1.0.0
45   */
46  public class DefaultApiClient extends AbstractApiClient {
47  
48      /**
49       * Creates a new DefaultApiClient with the specified base URL and configuration.
50       *
51       * @param baseUrl The base URL for API requests
52       * @param config  The RocketRest configuration
53       */
54      public DefaultApiClient(String baseUrl, RocketRestConfig config) {
55          super(baseUrl, config, createHttpClient(baseUrl, config));
56      }
57      
58      /**
59       * Creates a new DefaultApiClient with the specified base URL, configuration and a custom client.
60       *
61       * @param baseUrl The base URL for API requests
62       * @param config  The RocketRest configuration
63       * @param client  A custom RocketClient implementation
64       */
65      public DefaultApiClient(String baseUrl, RocketRestConfig config, RocketClient client) {
66          super(baseUrl, config, client);
67      }
68  
69      /**
70       * Creates an HTTP client with the appropriate options.
71       *
72       * @param baseUrl The base URL for API requests
73       * @param config  The RocketRest configuration
74       * @return A new RocketClient
75       */
76      private static RocketClient createHttpClient(String baseUrl, RocketRestConfig config) {
77          return RocketClientFactory.fromConfig(config)
78                  .build();
79      }
80  }