This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
Network Layer & SecClient
Loading…
Network Layer & SecClient
Relevant source files
- src/network.rs
- src/network/fetch_cik_submissions.rs
- src/network/sec_client.rs
- src/utils.rs
- tests/sec_client_tests.rs
Purpose and Scope
This page documents the network layer of the rust-sec-fetcher application, specifically the SecClient HTTP client and its associated infrastructure. The SecClient provides the foundational HTTP communication layer for all SEC EDGAR API interactions, implementing throttling, caching, and retry logic to ensure reliable and compliant data fetching.
This page covers:
- The
SecClientstructure and initialization src/network/sec_client.rs:14-22 - Throttling and rate limiting policies src/network/sec_client.rs:65-88
- HTTP caching and preprocessor cache integration src/network/sec_client.rs:58-63 src/network/sec_client.rs:90-91
- Request/response handling including cache bypass src/network/sec_client.rs:159-184 src/network/sec_client.rs:186-214
- User-Agent management and email validation src/network/sec_client.rs:111-123
For information about the specific network fetching functions that use SecClient, see Data Fetching Functions. For details on the caching system architecture, see Caching & Storage System.
SecClient Architecture Overview
Component Diagram
Sources: src/network/sec_client.rs:14-108 src/network/sec_client.rs:159-214
SecClient Structure and Initialization
SecClient Fields
The SecClient struct maintains core components for networking and caching:
| Field | Type | Purpose |
|---|---|---|
email | String | Contact email for SEC User-Agent header src/network/sec_client.rs15 |
http_client | ClientWithMiddleware | reqwest client with middleware stack src/network/sec_client.rs18 |
cache_policy | Arc<CachePolicy> | Shared cache configuration src/network/sec_client.rs19 |
throttle_policy | Arc<ThrottlePolicy> | Shared throttle configuration src/network/sec_client.rs20 |
preprocessor_cache | Arc<DataStore> | Cache for processed/transformed data src/network/sec_client.rs21 |
Sources: src/network/sec_client.rs:14-22
Construction from ConfigManager
The from_config_manager() constructor performs the following initialization sequence:
- Extract Configuration : Reads
email,app_name,app_version,max_concurrent,min_delay_ms, andmax_retriesfromAppConfigsrc/network/sec_client.rs:31-56 - Create CachePolicy : Configures cache with 1-week TTL (hardcoded) and disables header respect src/network/sec_client.rs:58-63
- Create ThrottlePolicy : Configures rate limiting based on
max_concurrentandmin_delay_mswith a 500ms adaptive jitter src/network/sec_client.rs:82-88 - Initialize Caches : Retrieves both HTTP and preprocessor cache stores from the
ConfigManagersrc/network/sec_client.rs:90-91 - Build Middleware Stack : Combines the
DataStorewith policies usingreqwest_drivehelpers src/network/sec_client.rs:93-98
Sources: src/network/sec_client.rs:26-109
Throttle Policy & Compliance
The SEC’s public guidance for EDGAR states a maximum request rate of 10 requests/second. The SecClient chooses a conservative default of max_concurrent=1 and min_delay_ms=500 (~2 req/s) src/network/sec_client.rs:67-81
| Parameter | AppConfig Field | Default | Purpose |
|---|---|---|---|
base_delay_ms | min_delay_ms | 500 | Minimum delay between requests src/network/sec_client.rs83 |
max_concurrent | max_concurrent | 1 | Concurrent request limit src/network/sec_client.rs84 |
max_retries | max_retries | 3 | Retry attempt limit src/network/sec_client.rs85 |
adaptive_jitter_ms | N/A | 500 | Randomized delay for retry backoff src/network/sec_client.rs87 |
Sources: src/network/sec_client.rs:65-88
Request Flow and Caching
raw_request vs raw_request_bypass_cache
The client provides two primary ways to interact with the network:
raw_request: The standard path. It uses thehttp_clientwhich includes theCacheMiddleware. Requests are served from the on-disk cache if available and within TTL src/network/sec_client.rs:159-184raw_request_bypass_cache: Uses theCacheBypassextension to force a network fetch. This skips both reading from and writing to the cache while still respecting theThrottlePolicysrc/network/sec_client.rs:186-214
Request Pipeline
Sources: src/network/sec_client.rs:159-224 src/network/sec_client.rs:93-98
User-Agent Management
The get_user_agent() method generates a compliant User-Agent string as required by the SEC. It validates the email format every time it is called src/network/sec_client.rs:111-123
Format: {app_name}/{app_version} (+{email})
Example: sec-fetcher/0.1.0 (+user@example.com)
If the email provided in the configuration is invalid according to EmailAddress::is_valid, the client will panic src/network/sec_client.rs:115-118
Sources: src/network/sec_client.rs:111-123 tests/sec_client_tests.rs:7-23
Fetch and Render Pipeline
The fetch_and_render function (exported in src/network.rs) provides a high-level orchestration for retrieving filing documents and converting them to specific views (e.g., Markdown).
- Fetch : Retrieves the raw document from EDGAR using
SecClient. - Parse : Normalizes the document structure.
- Render : Applies a
Viewtransformation (likeMarkdownView).
Sources: src/network.rs:45-46 src/network/fetch_and_render.rs:1-10 (Note: file content for fetch_and_render.rs was not provided in detail, but its existence is noted in the module structure).
Testing Infrastructure
SecClient logic is verified in tests/sec_client_tests.rs using mockito to simulate SEC API responses.
| Test Case | Code Entity | Purpose |
|---|---|---|
test_user_agent | SecClient::get_user_agent | Verifies UA string construction tests/sec_client_tests.rs:7-23 |
test_invalid_email_panic | SecClient::get_user_agent | Ensures panic on malformed email tests/sec_client_tests.rs:105-117 |
test_fetch_json_with_retry_failure | SecClient::fetch_json | Verifies max_retries enforcement tests/sec_client_tests.rs:194-222 |
test_fetch_json_with_retry_backoff | SecClient::fetch_json | Validates recovery after 500 error tests/sec_client_tests.rs:225-233 |
Sources: tests/sec_client_tests.rs:1-233
Dismiss
Refresh this wiki
Enter email to refresh