Package com.guinetik.rr.request
Class RequestBuilder<Req,Res>
- java.lang.Object
-
- com.guinetik.rr.request.RequestBuilder<Req,Res>
-
- Type Parameters:
Req- the type of the request bodyRes- the type of the response
public class RequestBuilder<Req,Res> extends Object
Fluent builder for constructingRequestSpecinstances.This builder provides a clean, type-safe API for creating HTTP request specifications with all necessary parameters like endpoint, method, headers, body, and response type.
Static Factory Methods
// GET request RequestSpec<Void, User> getUser = RequestBuilder.<Void, User>get("/users/1") .responseType(User.class) .build(); // POST request with body RequestSpec<CreateUser, User> createUser = RequestBuilder.<CreateUser, User>post("/users") .body(new CreateUser("John", "john@example.com")) .responseType(User.class) .build(); // PUT request RequestSpec<UpdateUser, User> updateUser = RequestBuilder.<UpdateUser, User>put("/users/1") .body(new UpdateUser("John Doe")) .responseType(User.class) .build(); // DELETE request RequestSpec<Void, Void> deleteUser = RequestBuilder.<Void, Void>delete("/users/1") .responseType(Void.class) .build();With Headers and Query Params
Map<String, String> params = new HashMap<>(); params.put("page", "1"); params.put("limit", "10"); RequestSpec<Void, UserList> request = RequestBuilder.<Void, UserList>get("/users") .queryParams(params) .headers(RocketHeaders.defaultJson().set("X-Custom", "value")) .responseType(UserList.class) .build();- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RequestSpec
-
-
Constructor Summary
Constructors Constructor Description RequestBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestBuilder<Req,Res>body(Req body)Sets the body of the request.RequestSpec<Req,Res>build()Builds and returns aRequestSpecinstance.static <Req,Res>
RequestBuilder<Req,Res>delete(String endpoint)Creates a DELETE request builder for the specified endpoint.RequestBuilder<Req,Res>endpoint(String endpoint)Sets the API endpoint for the request.static <Req,Res>
RequestBuilder<Req,Res>get(String endpoint)Creates a GET request builder for the specified endpoint.RequestBuilder<Req,Res>headers(RocketHeaders headers)Sets the headers for the request.RequestBuilder<Req,Res>method(String method)Sets the HTTP method for the request.static <Req,Res>
RequestBuilder<Req,Res>post(String endpoint)Creates a POST request builder for the specified endpoint.static <Req,Res>
RequestBuilder<Req,Res>put(String endpoint)Creates a PUT request builder for the specified endpoint.RequestBuilder<Req,Res>queryParam(String name, String value)Adds a single query parameter to the request.RequestBuilder<Req,Res>queryParams(Map<String,String> queryParams)Sets the query parameters for the request.RequestBuilder<Req,Res>responseType(Class<Res> responseType)Sets the expected response type of the request.
-
-
-
Method Detail
-
get
public static <Req,Res> RequestBuilder<Req,Res> get(String endpoint)
Creates a GET request builder for the specified endpoint.- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
endpoint- the API endpoint.- Returns:
- a new builder instance.
-
post
public static <Req,Res> RequestBuilder<Req,Res> post(String endpoint)
Creates a POST request builder for the specified endpoint.- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
endpoint- the API endpoint.- Returns:
- a new builder instance.
-
put
public static <Req,Res> RequestBuilder<Req,Res> put(String endpoint)
Creates a PUT request builder for the specified endpoint.- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
endpoint- the API endpoint.- Returns:
- a new builder instance.
-
delete
public static <Req,Res> RequestBuilder<Req,Res> delete(String endpoint)
Creates a DELETE request builder for the specified endpoint.- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
endpoint- the API endpoint.- Returns:
- a new builder instance.
-
endpoint
public RequestBuilder<Req,Res> endpoint(String endpoint)
Sets the API endpoint for the request.- Parameters:
endpoint- the API endpoint.- Returns:
- the builder instance.
-
method
public RequestBuilder<Req,Res> method(String method)
Sets the HTTP method for the request.- Parameters:
method- the HTTP method (e.g., GET, POST, PUT, DELETE).- Returns:
- the builder instance.
-
queryParams
public RequestBuilder<Req,Res> queryParams(Map<String,String> queryParams)
Sets the query parameters for the request.- Parameters:
queryParams- a map of query parameters.- Returns:
- the builder instance.
-
queryParam
public RequestBuilder<Req,Res> queryParam(String name, String value)
Adds a single query parameter to the request.- Parameters:
name- the parameter name.value- the parameter value.- Returns:
- the builder instance.
-
headers
public RequestBuilder<Req,Res> headers(RocketHeaders headers)
Sets the headers for the request.- Parameters:
headers- a map of headers.- Returns:
- the builder instance.
-
body
public RequestBuilder<Req,Res> body(Req body)
Sets the body of the request.- Parameters:
body- the request body.- Returns:
- the builder instance.
-
responseType
public RequestBuilder<Req,Res> responseType(Class<Res> responseType)
Sets the expected response type of the request.- Parameters:
responseType- the response type.- Returns:
- the builder instance.
-
build
public RequestSpec<Req,Res> build()
Builds and returns aRequestSpecinstance.- Returns:
- the constructed
RequestSpec.
-
-