package mantra.rest
ApiResponse
CLASS
public class
Standardized REST API response envelope encapsulating status, data, error details, and pagination metadata.
🏗️ Constructor Summary
Constructor Signature
def __init__(self, status: Int, success: Bool, data: String, error: ApiError, pagination: Pagination)
⚙️ Method Summary
| Modifier & Return Type | Method Signature | Description |
|---|---|---|
ApiResponse |
static def ok(data: String) |
Constructs a 200 OK success envelope with data payload. |
ApiResponse |
static def created(data: String) |
Constructs a 201 Created success envelope with newly created data. |
ApiResponse |
static def list(data: String, pagination: Pagination) |
Constructs a paginated 200 OK response envelope. |
ApiResponse |
static def not_found(msg: String) |
Constructs a 404 Not Found error envelope. |
ApiResponse |
static def bad_request(msg: String) |
Constructs a 400 Bad Request error envelope. |
String |
pub def to_json() |
Serializes the response envelope into standard JSON format. |
Response |
pub def to_response() |
Converts envelope into a mantra.web Response object ready for transmission. |
🔍 Detailed Method Specifications
static def ok(data: String) -> ApiResponse
Constructs a 200 OK success envelope with data payload.
static def created(data: String) -> ApiResponse
Constructs a 201 Created success envelope with newly created data.
static def list(data: String, pagination: Pagination) -> ApiResponse
Constructs a paginated 200 OK response envelope.
static def not_found(msg: String) -> ApiResponse
Constructs a 404 Not Found error envelope.
static def bad_request(msg: String) -> ApiResponse
Constructs a 400 Bad Request error envelope.
pub def to_json() -> String
Serializes the response envelope into standard JSON format.
pub def to_response() -> Response
Converts envelope into a mantra.web Response object ready for transmission.
💡 Example Usage
api_response_example.mantra
let resp = ApiResponse.ok("{\"id\": 1, \"username\": \"alice\"}")
print(resp.to_json())
# {"success":true,"data":{"id": 1, "username": "alice"},"error":null,"pagination":null}