View Javadoc
1   package com.guinetik.examples;
2   
3   import com.guinetik.rr.RocketRestConfig;
4   import com.guinetik.rr.api.FluentApiClient;
5   import com.guinetik.rr.result.ApiError;
6   import com.guinetik.rr.result.Result;
7   
8   /**
9    * Example demonstrating the Result-based API for developers who prefer 
10   * a more declarative approach without exceptions.
11   */
12  public class FluentApiExample implements Example {
13      
14      private static final String API_BASE_URL = "https://jsonplaceholder.typicode.com";
15      
16      @Override
17      public String getName() {
18          return "Fluent API with Result Pattern";
19      }
20      
21      @Override
22      public void run() {
23          System.out.println("Demonstrating the Result-based API (no exceptions)...");
24          
25          // Create configuration for the API
26          RocketRestConfig config = RocketRestConfig.builder(API_BASE_URL)
27                  .build();
28          
29          // Create FluentApiClient that uses a Result pattern
30          FluentApiClient client = new FluentApiClient(API_BASE_URL, config);
31          
32          try {
33              // Example 1: Successful request with pattern matching style
34              System.out.println("\n=== Example 1: GET Todo (Pattern Matching Style) ===");
35              Result<Todo, ApiError> todoResult = client.get("/todos/1", Todo.class);
36              
37              if (todoResult.isSuccess()) {
38                  Todo todo = todoResult.getValue();
39                  System.out.println("Successfully fetched Todo: " + todo);
40              } else {
41                  ApiError error = todoResult.getError();
42                  System.out.println("Failed to fetch Todo: " + error);
43                  System.out.println("Error type: " + error.getErrorType());
44              }
45              
46              // Example 2: Using functional methods
47              System.out.println("\n=== Example 2: GET Todo (Functional Style) ===");
48              client.get("/todos/2", Todo.class)
49                  .ifSuccess(todo -> System.out.println("Todo title: " + todo.getTitle()))
50                  .ifFailure(error -> System.out.println("Error: " + error.getMessage()));
51              
52              // Example 3: Handling an error case
53              System.out.println("\n=== Example 3: GET Non-existent Resource (Error Handling) ===");
54              Result<Todo, ApiError> errorResult = client.get("/todos/999", Todo.class);
55              
56              Todo todo = errorResult.getOrElse(new Todo(0, 0, "Default todo (when error occurs)", false));
57              System.out.println("Got todo (with fallback): " + todo);
58              
59              // Example 4: Mapping result
60              System.out.println("\n=== Example 4: GET with Result Mapping ===");
61              String title = client.get("/todos/3", Todo.class)
62                  .map(t -> "TITLE: " + t.getTitle())
63                  .getOrElse("No title available");
64              
65              System.out.println(title);
66              
67              // Example 5: Using String.class directly
68              System.out.println("\n=== Example 5: GET Raw JSON ===");
69              Result<String, ApiError> jsonResult = client.get("/users/1", String.class);
70              
71              jsonResult.ifSuccess(json -> {
72                  System.out.println("Raw JSON response:");
73                  System.out.println(json.substring(0, Math.min(json.length(), 200)) + "...");
74              });
75              
76              // Example 6: Non-existent endpoint
77              System.out.println("\n=== Example 6: Handling a 404 Error ===");
78              client.get("/non-existent", String.class)
79                  .ifSuccess(response -> System.out.println("This shouldn't happen!"))
80                  .ifFailure(error -> {
81                      System.out.println("Expected error occurred: " + error);
82                      if (error.getStatusCode() == 404) {
83                          System.out.println("Confirmed 404 Not Found error");
84                      }
85                  });
86                  
87          } finally {
88              client.shutdown();
89          }
90      }
91  }