pub struct ApiClient { /* private fields */ }Implementations§
Source§impl ApiClient
impl ApiClient
pub async fn collected_paths(&mut self) -> Paths
Sourcepub async fn collected_openapi(&mut self) -> OpenApi
pub async fn collected_openapi(&mut self) -> OpenApi
Generates a complete OpenAPI specification from collected request/response data.
This method aggregates all the information collected during API calls and produces a comprehensive OpenAPI 3.1 specification including paths, components, schemas, operation metadata, and server information.
§Features
- Automatic Path Collection: All endpoint calls are automatically documented
- Schema Generation: Request/response schemas are extracted from Rust types
- Operation Metadata: Includes operation IDs, descriptions, and tags
- Server Information: Configurable server URLs and descriptions
- Tag Collection: Automatically computed from all operations
- Component Schemas: Reusable schema definitions with proper references
§Example
use clawspec_core::{ApiClient, ToSchema};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, ToSchema)]
struct UserData { name: String }
let mut client = ApiClient::builder()
.with_host("api.example.com")
.with_info_simple("My API", "1.0.0")
.add_server_simple("https://api.example.com", "Production server")
.build()?;
let user_data = UserData { name: "John".to_string() };
// Make some API calls to collect data
client.get("/users")?.await?.as_json::<Vec<UserData>>().await?;
client.post("/users")?.json(&user_data)?.await?.as_json::<UserData>().await?;
// Generate complete OpenAPI specification
let openapi = client.collected_openapi().await;
// The generated spec includes:
// - API info (title, version, description)
// - Server definitions
// - All paths with operations
// - Component schemas
// - Computed tags from operations
// Export to different formats
let yaml = serde_saphyr::to_string(&openapi)?;
let json = serde_json::to_string_pretty(&openapi)?;§Generated Content
The generated OpenAPI specification includes:
- Info: API metadata (title, version, description) if configured
- Servers: Server URLs and descriptions if configured
- Paths: All documented endpoints with operations
- Components: Reusable schema definitions
- Tags: Automatically computed from operation tags
§Tag Generation
Tags are automatically computed from all operations and include:
- Explicit tags set on operations
- Auto-generated tags based on path patterns
- Deduplicated and sorted alphabetically
§Performance Notes
- This method acquires read locks on internal collections
- Schema processing is cached to avoid redundant work
- Tags are computed on-demand from operation metadata
Sourcepub async fn register_schema<T>(&mut self)where
T: ToSchema + 'static,
pub async fn register_schema<T>(&mut self)where
T: ToSchema + 'static,
Manually registers a type in the schema collection.
This method allows you to explicitly add types to the OpenAPI schema collection that might not be automatically detected. This is useful for types that are referenced indirectly, such as nested types.
§Type Parameters
T- The type to register, must implementToSchemaand'static
§Example
use clawspec_core::ApiClient;
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
struct NestedErrorType {
message: String,
}
let mut client = ApiClient::builder().build()?;
// Register the nested type that might not be automatically detected
client.register_schema::<NestedErrorType>().await;
// Now when you generate the OpenAPI spec, NestedErrorType will be included
let openapi = client.collected_openapi().await;Source§impl ApiClient
impl ApiClient
pub fn call( &self, method: Method, path: CallPath, ) -> Result<ApiCall, ApiClientError>
pub fn get(&self, path: impl Into<CallPath>) -> Result<ApiCall, ApiClientError>
pub fn post(&self, path: impl Into<CallPath>) -> Result<ApiCall, ApiClientError>
pub fn put(&self, path: impl Into<CallPath>) -> Result<ApiCall, ApiClientError>
pub fn delete( &self, path: impl Into<CallPath>, ) -> Result<ApiCall, ApiClientError>
pub fn patch( &self, path: impl Into<CallPath>, ) -> Result<ApiCall, ApiClientError>
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ApiClient
impl !RefUnwindSafe for ApiClient
impl Send for ApiClient
impl Sync for ApiClient
impl Unpin for ApiClient
impl !UnwindSafe for ApiClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more