Skip to main content

reinhardt_rest/browsable_api/
response.rs

1//! Browsable response types
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Response for browsable API
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct BrowsableResponse {
9	/// The JSON response body.
10	pub data: serde_json::Value,
11	/// Metadata about the response (status, method, path, headers).
12	pub metadata: ResponseMetadata,
13}
14
15/// Metadata associated with a browsable API response.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ResponseMetadata {
18	/// The HTTP status code.
19	pub status: u16,
20	/// The HTTP method used for the request.
21	pub method: String,
22	/// The request path.
23	pub path: String,
24	/// Response headers as key-value pairs.
25	pub headers: HashMap<String, String>,
26}
27
28impl BrowsableResponse {
29	/// Create a new BrowsableResponse
30	///
31	/// # Examples
32	///
33	/// ```
34	/// use reinhardt_rest::browsable_api::response::{BrowsableResponse, ResponseMetadata};
35	/// use std::collections::HashMap;
36	///
37	/// let metadata = ResponseMetadata {
38	///     status: 200,
39	///     method: "GET".to_string(),
40	///     path: "/api/test".to_string(),
41	///     headers: HashMap::new(),
42	/// };
43	/// let response = BrowsableResponse::new(serde_json::json!({}), metadata);
44	/// ```
45	pub fn new(data: serde_json::Value, metadata: ResponseMetadata) -> Self {
46		Self { data, metadata }
47	}
48	/// Create a successful response (200 OK)
49	///
50	/// # Examples
51	///
52	/// ```
53	/// use reinhardt_rest::browsable_api::response::BrowsableResponse;
54	///
55	/// let response = BrowsableResponse::success(
56	///     serde_json::json!({"message": "ok"}),
57	///     "GET".to_string(),
58	///     "/api/test".to_string()
59	/// );
60	/// ```
61	pub fn success(data: serde_json::Value, method: String, path: String) -> Self {
62		Self {
63			data,
64			metadata: ResponseMetadata {
65				status: 200,
66				method,
67				path,
68				headers: HashMap::new(),
69			},
70		}
71	}
72}