unforgivencl/larachileancongress
Installation
composer require unforgivencl/larachileancongress:dev-master
Update config/app.php to include:
'providers' => [
Unforgivencl\LaraChileanCongress\LaraChileanCongressServiceProvider::class,
],
'aliases' => [
'ChileanCongress' => Unforgivencl\LaraChileanCongress\Facades\LaraChileanCongress::class,
],
First Use Case Fetch all senators (default endpoint):
$senators = ChileanCongress::senators()->getSenators()->fetch();
Output: JSON array of senator data (e.g., id, name, party).
src/Facades/LaraChileanCongress.php for available endpoints (senators, delegates, votation, lawproject).src/Traits/ResponseHandler.php for parsing logic.setDelegates() or setSenators() to toggle between delegate/senator endpoints (default: senators).Fetching Legislators
// Senators (default)
$senators = ChileanCongress::senators()->getSenators()->fetch();
// Delegates (requires explicit switch)
$delegates = ChileanCongress::delegate()->setDelegates()->getDelegates()->fetch();
Use case: Populate a dropdown or list view with legislator names/IDs.
Votation Data
// Senators' votation by number
$votation = ChileanCongress::votation()
->number('8575') // Replace with actual votation ID
->getSenatorsVotation()
->fetch();
Use case: Display votation results in a legislative tracker.
Law Projects
// Law project details
$lawProject = ChileanCongress::lawproject()
->number('1') // Replace with actual law project ID
->getLawProject()
->fetch();
Use case: Show law project status (e.g., "In Committee") in a legislative dashboard.
Caching Responses Cache frequent queries (e.g., all senators) to reduce API calls:
$senators = Cache::remember('senators_list', now()->addHours(1), function () {
return ChileanCongress::senators()->getSenators()->fetch();
});
Error Handling
Wrap API calls in a try-catch to handle XML/JSON parsing errors:
try {
$data = ChileanCongress::lawproject()->number('1')->getLawProject()->fetch();
} catch (\Exception $e) {
Log::error("API Error: " . $e->getMessage());
return response()->json(['error' => 'Failed to fetch data'], 500);
}
Dynamic Endpoints Extend the facade for custom endpoints (e.g., committees):
// In a service class
public function getCommittees() {
return ChileanCongress::custom()
->setEndpoint('committees') // Hypothetical; check package for extensibility
->getData()
->fetch();
}
Endpoint Defaults
setDelegates() for delegate endpoints returns empty data.ChileanCongress::delegate()->setDelegates() before fetching.XML-to-JSON Quirks
Senator[0] instead of Senator).$normalized = collect($rawData)->map(function ($item) {
return array_values($item)[0]; // Flatten single-item arrays
})->values();
Rate Limiting
opendata.congreso.cl may throttle requests. Add delays between calls:
sleep(1); // 1-second delay between API calls
Deprecated Methods
dev-master). Assume breaking changes in future updates.Raw XML Inspection To debug parsing issues, log the raw XML response:
$rawXml = ChileanCongress::senators()->getSenators()->getRawResponse();
Log::debug($rawXml);
Facade Method Chaining Ensure method chaining is correct. Example of a broken call:
// ❌ Wrong: Missing `setDelegates()`
$delegates = ChileanCongress::delegate()->getDelegates()->fetch(); // Returns empty
Fix: Always set the endpoint first.
Endpoint URLs Hardcoded URLs may change. Override them in the service provider:
// In `AppServiceProvider@boot`
$this->app->singleton('chileancongress', function () {
$client = new \Unforgivencl\LaraChileanCongress\ChileanCongressClient();
$client->setBaseUrl('https://custom-api-url.cl'); // Override if needed
return $client;
});
Add New Endpoints
Extend the ChileanCongressClient class to support unsupported endpoints (e.g., committees):
// In a custom service class
public function getCommittees() {
$response = Http::get('https://opendata.congreso.cl/committees');
return json_decode($response->body(), true);
}
Custom Response Handling
Override the ResponseHandler trait to modify JSON structure:
// In a custom handler
public function parseXmlToJson($xml) {
$data = simplexml_load_string($xml);
// Custom logic to restructure data
return json_decode(json_encode($data), true);
}
Testing Mock the HTTP client for unit tests:
$mock = Mockery::mock(\Unforgivencl\LaraChileanCongress\ChileanCongressClient::class);
$mock->shouldReceive('getSenators')->andReturn(['data' => 'mocked']);
$this->app->instance('chileancongress', $mock);
How can I help you explore Laravel packages today?