pub struct DocService { /* private fields */ }Expand description
Document service
Provides centralized management of HTTP client (with auto-retry), cache, and document cache.
§Fields
client: HTTP client with retry middleware (shared reference for connection pool reuse)cache: Generic cache instancedoc_cache: Document-specific cache
Implementations§
Source§impl DocService
impl DocService
Sourcepub fn new(cache: Arc<dyn Cache>) -> Result<Self>
pub fn new(cache: Arc<dyn Cache>) -> Result<Self>
Create new document service (with default TTL)
§Arguments
cache- cache instance
§Errors
Returns error if HTTP client creation fails
§Examples
use std::sync::Arc;
use crates_docs::tools::docs::DocService;
use crates_docs::cache::memory::MemoryCache;
let cache = Arc::new(MemoryCache::new(1000));
let service = DocService::new(cache).expect("Failed to create DocService");§Note
This method uses the global HTTP client singleton for connection pool reuse.
Make sure to call init_global_http_client() during server initialization
for optimal performance.
Sourcepub fn with_config(
cache: Arc<dyn Cache>,
cache_config: &CacheConfig,
) -> Result<Self>
pub fn with_config( cache: Arc<dyn Cache>, cache_config: &CacheConfig, ) -> Result<Self>
Create new document service (with custom cache config)
§Arguments
cache- cache instancecache_config- cache configuration
§Errors
Returns error if HTTP client creation fails
§Note
This method uses the global HTTP client singleton for connection pool reuse. If the global client is not initialized, it will be initialized with default config.
Sourcepub fn with_full_config(
cache: Arc<dyn Cache>,
cache_config: &CacheConfig,
_perf_config: &PerformanceConfig,
) -> Result<Self>
pub fn with_full_config( cache: Arc<dyn Cache>, cache_config: &CacheConfig, _perf_config: &PerformanceConfig, ) -> Result<Self>
Create new document service (with full config)
§Arguments
cache- cache instancecache_config- cache configurationperf_config- performance configuration(used only for initializing global HTTP client if not yet initialized)
§Errors
Returns error if HTTP client creation fails
§Note
This method uses the global HTTP client singleton for connection pool reuse.
The perf_config is used only if the global client hasn’t been initialized yet.
For consistent configuration, call init_global_http_client() during server startup.
Sourcepub fn client(&self) -> &ClientWithMiddleware
pub fn client(&self) -> &ClientWithMiddleware
Get HTTP client (with retry middleware)
Sourcepub async fn fetch_html(
&self,
url: &str,
tool_name: Option<&str>,
) -> Result<String, CallToolError>
pub async fn fetch_html( &self, url: &str, tool_name: Option<&str>, ) -> Result<String, CallToolError>
Fetch HTML content from a URL
This is a shared utility method used by multiple tools to fetch HTML from docs.rs and crates.io.
§Arguments
url- The URL to fetchtool_name- Optional tool name for better error messages (e.g., “lookup_crate”, “lookup_item”)
§Errors
Returns a CallToolError if:
- The HTTP request fails
- The response status is not successful
- Reading the response body fails
Sourcepub fn with_custom_client(
cache: Arc<dyn Cache>,
cache_config: &CacheConfig,
client: Arc<ClientWithMiddleware>,
) -> Self
pub fn with_custom_client( cache: Arc<dyn Cache>, cache_config: &CacheConfig, client: Arc<ClientWithMiddleware>, ) -> Self
Create new document service with custom HTTP client (for testing)