Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

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

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:

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:

FieldTypePurpose
emailStringContact email for SEC User-Agent header src/network/sec_client.rs15
http_clientClientWithMiddlewarereqwest client with middleware stack src/network/sec_client.rs18
cache_policyArc<CachePolicy>Shared cache configuration src/network/sec_client.rs19
throttle_policyArc<ThrottlePolicy>Shared throttle configuration src/network/sec_client.rs20
preprocessor_cacheArc<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:

  1. Extract Configuration : Reads email, app_name, app_version, max_concurrent, min_delay_ms, and max_retries from AppConfig src/network/sec_client.rs:31-56
  2. Create CachePolicy : Configures cache with 1-week TTL (hardcoded) and disables header respect src/network/sec_client.rs:58-63
  3. Create ThrottlePolicy : Configures rate limiting based on max_concurrent and min_delay_ms with a 500ms adaptive jitter src/network/sec_client.rs:82-88
  4. Initialize Caches : Retrieves both HTTP and preprocessor cache stores from the ConfigManager src/network/sec_client.rs:90-91
  5. Build Middleware Stack : Combines the DataStore with policies using reqwest_drive helpers 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

ParameterAppConfig FieldDefaultPurpose
base_delay_msmin_delay_ms500Minimum delay between requests src/network/sec_client.rs83
max_concurrentmax_concurrent1Concurrent request limit src/network/sec_client.rs84
max_retriesmax_retries3Retry attempt limit src/network/sec_client.rs85
adaptive_jitter_msN/A500Randomized 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:

  1. raw_request : The standard path. It uses the http_client which includes the CacheMiddleware. Requests are served from the on-disk cache if available and within TTL src/network/sec_client.rs:159-184
  2. raw_request_bypass_cache : Uses the CacheBypass extension to force a network fetch. This skips both reading from and writing to the cache while still respecting the ThrottlePolicy src/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).

  1. Fetch : Retrieves the raw document from EDGAR using SecClient.
  2. Parse : Normalizes the document structure.
  3. Render : Applies a View transformation (like MarkdownView).

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 CaseCode EntityPurpose
test_user_agentSecClient::get_user_agentVerifies UA string construction tests/sec_client_tests.rs:7-23
test_invalid_email_panicSecClient::get_user_agentEnsures panic on malformed email tests/sec_client_tests.rs:105-117
test_fetch_json_with_retry_failureSecClient::fetch_jsonVerifies max_retries enforcement tests/sec_client_tests.rs:194-222
test_fetch_json_with_retry_backoffSecClient::fetch_jsonValidates 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