Package io.mochaapi.client
Class ApiRequest
java.lang.Object
io.mochaapi.client.ApiRequest
Represents a configurable HTTP request with chainable methods.
Allows building requests step by step with headers, query parameters, and body.
Example usage:
ApiResponse response = client.get("https://api.example.com/data")
.header("Authorization", "Bearer token123")
.query("page", 1)
.query("limit", 10)
.body(requestData)
.execute();
- Since:
- 1.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionApiRequest(String url, String method) Creates a new API request with the specified URL and HTTP method.ApiRequest(String url, String method, ApiClient client) Creates a new API request with the specified URL and HTTP method. -
Method Summary
Modifier and TypeMethodDescriptionvoidasync(Consumer<ApiResponse> callback) Executes the request asynchronously with a callback.Sets the request body.Downloads the response to a file.Downloads the response to a file with the specified filename.Downloads the response to a file path.Downloads the response as a ManagedInputStream for streaming.execute()Executes the request synchronously and returns the response.Executes the request asynchronously and returns a CompletableFuture.getBody()Gets the security configuration from the associated client.getUrl()Adds a header to the request.booleanConverts this request to a multipart request for file uploads.Adds a query parameter to the request.
-
Constructor Details
-
ApiRequest
Creates a new API request with the specified URL and HTTP method. This constructor is used internally by ApiClient.- Parameters:
url- the target URLmethod- the HTTP method (GET, POST, PUT, DELETE, PATCH)client- the ApiClient instance
-
ApiRequest
Creates a new API request with the specified URL and HTTP method. This constructor is used by the static Api class for backward compatibility.- Parameters:
url- the target URLmethod- the HTTP method (GET, POST, PUT, DELETE, PATCH)
-
-
Method Details
-
getSecurityConfig
Gets the security configuration from the associated client. Uses production-safe defaults if no client is associated.- Returns:
- SecurityConfig instance
-
header
Adds a header to the request.- Parameters:
name- the header namevalue- the header value- Returns:
- this request for chaining
- Throws:
IllegalArgumentException- if name or value is null
-
query
Adds a query parameter to the request.- Parameters:
name- the parameter namevalue- the parameter value- Returns:
- this request for chaining
- Throws:
IllegalArgumentException- if name is null
-
body
Sets the request body. Can be a String, Map, or any object that will be JSON serialized.- Parameters:
body- the request body- Returns:
- this request for chaining
- Throws:
IllegalArgumentException- if body is null
-
multipart
Converts this request to a multipart request for file uploads.- Returns:
- multipart request builder
-
download
Downloads the response to a file.- Parameters:
file- the target file- Returns:
- the downloaded file
- Throws:
ApiException- if download fails
-
download
Downloads the response to a file path.- Parameters:
path- the target file path- Returns:
- the downloaded file
- Throws:
ApiException- if download fails
-
download
Downloads the response to a file with the specified filename.- Parameters:
filename- the target filename- Returns:
- the downloaded file
- Throws:
ApiException- if download fails
-
downloadStream
Downloads the response as a ManagedInputStream for streaming. The returned stream implements AutoCloseable and should be used in try-with-resources.Usage example:
try (ManagedInputStream stream = request.downloadStream()) { byte[] data = stream.readAllBytes(); } // Stream is automatically closed- Returns:
- managed input stream for reading the response
- Throws:
ApiException- if download fails
-
execute
Executes the request synchronously and returns the response.- Returns:
- ApiResponse containing the result
- Throws:
ApiException- if the request fails
-
executeAsync
Executes the request asynchronously and returns a CompletableFuture.- Returns:
- CompletableFuture containing the response
-
async
Executes the request asynchronously with a callback.- Parameters:
callback- function to handle the response
-
getUrl
-
getMethod
-
getHeaders
-
getQueryParams
-
getBody
-
isMultipart
public boolean isMultipart()
-