Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MiguelNavas19/miapibcv/llms.txt
Use this file to discover all available pages before exploring further.
System Overview
Mi API BCV is built on Laravel 12 and follows a layered architecture that separates concerns between data collection, storage, and API delivery. The system uses the Strategy pattern for extensibility and Laravel’s built-in features for caching, scheduling, and database management.High-Level Architecture
Data Update Flow
Core Components
API Layer
Location:app/Http/Controllers/Api/ScraperController.php
The API controller handles HTTP requests and provides two main endpoints:
- GET / - Returns today’s rates from all banks
- GET /info/{date}/{source?} - Returns historical rates with optional filtering
Data Collection Layer
ScraperService (app/Services/ScraperService.php)
Handles HTTP requests and HTML parsing for each bank’s website:
- Configures HTTP client with browser headers
- Uses Symfony DomCrawler for HTML parsing
- Applies bank-specific parsing logic
- Normalizes decimal values
app/Services/UrlProviderService.php)
Acts as the Strategy pattern context, managing concrete URL strategies for each bank.
Strategy Pattern Implementation
UrlStrategy Interface (app/Interfaces/UrlStrategy.php)
UrlBcv- Scrapes Banco Central de VenezuelaUrlBanplus- Scrapes Banplus news tickerUrlBnc- Scrapes Banco Nacional de CréditoUrlBdv- Fetches from Banco de Venezuela (JSON API)
- Defines the target URL
- Calls ScraperService with bank identifier
- Returns the parsed exchange rate value
Data Model
ReferenceRecord (app/Models/ReferenceRecord.php)
Eloquent model representing exchange rate records:
app/Observers/ReferenceObserver.php)
Automatically invalidates cache when records are created or updated, ensuring API responses stay fresh.
Scheduled Tasks
FetchExchangeRates Command (app/Console/Commands/FetchExchangeRates.php)
- Checks if today’s rate already exists
- Fetches new rate via appropriate strategy
- Saves to database
- Logs success/failure
- Continues with next bank if one fails
Design Patterns
Strategy Pattern
Purpose: Allow easy addition of new bank sources without modifying existing code Benefits:- Open/Closed Principle: Open for extension, closed for modification
- Each bank’s scraping logic is isolated
- Easy to test individual strategies
- Simple to add new banks
Observer Pattern
Purpose: Automatically invalidate cache when data changes Implementation: ReferenceObserver listens to Eloquent model events Benefits:- Decouples cache management from business logic
- Ensures data consistency
- Centralized cache invalidation logic
Repository Pattern (via Eloquent)
Purpose: Abstract database operations Implementation: Laravel’s Eloquent ORM Benefits:- Database-agnostic code
- Query builder and relationships
- Migration support
Laravel Components Used
HTTP Client
Cache Facade
Console Commands
Artisan commands for manual and scheduled rate updates.Task Scheduling
Laravel’s scheduler (configured via cron) runsrates:update daily.
Directory Structure
Data Flow Examples
API Request Flow
- Client sends
GET /request - Route dispatches to
ScraperController::show() - Controller checks cache for today’s rates
- If cache miss, query database
- Format response as JSON
- Cache result for 1 hour
- Return to client
Scheduled Update Flow
- Cron triggers
php artisan rates:update - Command iterates:
['bdv', 'banplus', 'bnc', 'bcv'] - For each bank:
- Check if today’s record exists (skip if yes)
- Call
UrlProviderService::getStrategy($bank) - Strategy calls
ScraperService::scrapeData($url, $bank) - ScraperService fetches HTML and parses
- Strategy returns float value
- Command saves to
ReferenceRecord - Observer invalidates cache
- Log completion
Performance Optimizations
Caching Strategy
- TTL: 1 hour (rates don’t change frequently)
- Key Format:
tasas_bancos_YYYY-MM-DD - Invalidation: Automatic via Observer
- Driver: Configurable (file, database, Redis)
Database Indexing
Recommended indexes:HTTP Optimization
- Reuse HTTP client connections
- Set reasonable timeouts
- Handle failures gracefully
Error Handling
API Layer
- 404 when no rates available
- 400 for invalid date formats
- 404 for unknown routes (fallback handler)
Scraper Layer
- Try/catch around each bank fetch
- Log errors but continue with next bank
- Throw exceptions for missing DOM elements
Command Layer
- Per-bank error isolation
- Comprehensive logging
- Graceful degradation
Security Considerations
Input Validation
Rate Limiting
No built-in rate limiting (consider adding Laravel’s throttle middleware for production).CORS
Configure inconfig/cors.php for production deployments.
Next Steps
ScraperService Deep Dive
Learn how web scraping works in detail
Strategy Pattern
Understand the Strategy pattern implementation
Adding Banks
Extend the system with new data sources
Scheduler Setup
Configure automatic rate updates