1 package com.guinetik.examples;
2
3 import com.guinetik.rr.RocketRest;
4 import com.guinetik.rr.RocketRestConfig;
5 import com.guinetik.rr.result.ApiError;
6 import com.guinetik.rr.result.Result;
7
8
9
10
11
12 public class RocketRestFluentExample implements Example {
13
14 private static final String API_BASE_URL = "https://jsonplaceholder.typicode.com";
15
16 @Override
17 public String getName() {
18 return "RocketRest Fluent API with Result Pattern";
19 }
20
21 @Override
22 public void run() {
23 System.out.println("Demonstrating the RocketRest Fluent API with Result pattern...");
24
25
26 RocketRestConfig config = RocketRestConfig.builder(API_BASE_URL)
27 .build();
28
29
30 RocketRest client = new RocketRest(API_BASE_URL, config);
31
32 try {
33
34 System.out.println("\n=== Example 1: GET Todo (Pattern Matching Style) ===");
35 Result<Todo, ApiError> todoResult = client.fluent().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
47 System.out.println("\n=== Example 2: GET Todo (Functional Style) ===");
48 client.fluent().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
53 System.out.println("\n=== Example 3: GET Non-existent Resource (Error Handling) ===");
54 Result<Todo, ApiError> errorResult = client.fluent().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
60 System.out.println("\n=== Example 4: GET with Result Mapping ===");
61 String title = client.fluent().get("/todos/3", Todo.class)
62 .map(t -> "TITLE: " + t.getTitle())
63 .getOrElse("No title available");
64
65 System.out.println(title);
66
67
68 System.out.println("\n=== Example 5: Comparing direct access vs. fluent API ===");
69 try {
70
71 Todo directTodo = client.get("/todos/4", Todo.class);
72 System.out.println("Direct access successful: " + directTodo.getTitle());
73
74
75 Result<Todo, ApiError> fluentResult = client.fluent().get("/todos/4", Todo.class);
76 fluentResult.ifSuccess(t -> System.out.println("Fluent API successful: " + t.getTitle()));
77 } catch (Exception e) {
78 System.out.println("Direct access threw exception: " + e.getMessage());
79 }
80
81
82 System.out.println("\n=== Example 6: Handling a 404 Error ===");
83 client.fluent().get("/non-existent", String.class)
84 .ifSuccess(response -> System.out.println("This shouldn't happen!"))
85 .ifFailure(error -> {
86 System.out.println("Expected error occurred: " + error);
87 if (error.getStatusCode() == 404) {
88 System.out.println("Confirmed 404 Not Found error");
89 }
90 });
91
92
93 System.out.println("\n=== Example 7: POST Request with Body ===");
94 Todo newTodo = new Todo(0, 1, "My new todo item", false);
95 client.fluent().post("/todos", newTodo, Todo.class)
96 .ifSuccess(created -> System.out.println("Successfully created Todo with ID: " + created.getId()))
97 .ifFailure(error -> System.out.println("Failed to create Todo: " + error));
98
99 } finally {
100 client.shutdown();
101 }
102 }
103 }