Files
2026-04-03 00:23:01 +03:00

146 lines
3.9 KiB
PHP

<?php
namespace App\Services\JobApi\Clients;
use App\Services\JobApi\DTO\JobData;
use App\Services\JobApi\Exceptions\ApiException;
use App\Services\JobApi\Transformers\JobTransformer;
use App\Services\JobApi\Transformers\TheMuseTransformer;
use Illuminate\Http\Client\PendingRequest;
class TheMuseClient extends JobApiClient
{
protected function getTransformer(): JobTransformer
{
return new TheMuseTransformer();
}
protected function attachAuthentication(PendingRequest $client): PendingRequest
{
// The Muse API v2 doesn't require authentication for basic queries
// If API key is provided, attach it as a query parameter
if ($this->source->api_key) {
$client = $client->withQueryParameters(['api_key' => $this->source->api_key]);
}
return $client;
}
/**
* Fetch jobs from The Muse API
*
* @param array $params [
* 'page' => int, // Page number (default 0)
* 'descending' => bool, // Sort order (default true)
* 'category' => string, // Job category
* 'level' => string, // e.g., 'Entry Level', 'Mid Level', 'Senior Level'
* 'location' => string, // Location filter
* 'company' => string, // Company name or ID
* ]
* @return array<JobData>
*/
public function fetchJobs(array $params = []): array
{
try {
$queryParams = $this->buildQueryParams($params);
$response = $this->makeRequest('GET', 'jobs', $queryParams);
$data = $response->json();
if (!isset($data['results']) || !is_array($data['results'])) {
throw new ApiException(
'Invalid response structure from The Muse API',
$this->source->name,
['response' => $data]
);
}
$this->updateLastFetched();
return $this->transformer->transformMany($data['results']);
} catch (ApiException $e) {
throw $e;
} catch (\Throwable $e) {
throw new ApiException(
'Failed to fetch jobs from The Muse',
$this->source->name,
['error' => $e->getMessage()],
0,
$e
);
}
}
/**
* Build query parameters for the API request
*/
private function buildQueryParams(array $params): array
{
$queryParams = [
'page' => $params['page'] ?? 0,
'descending' => $params['descending'] ?? true,
];
if (!empty($params['category'])) {
$queryParams['category'] = $params['category'];
}
if (!empty($params['level'])) {
$queryParams['level'] = $params['level'];
}
if (!empty($params['location'])) {
$queryParams['location'] = $params['location'];
}
if (!empty($params['company'])) {
$queryParams['company'] = $params['company'];
}
return $queryParams;
}
/**
* Get available job categories from The Muse
*/
public function getCategories(): array
{
return [
'Account Management',
'Business & Strategy',
'Creative & Design',
'Customer Service',
'Data Science',
'Editorial',
'Education',
'Engineering',
'Finance',
'Healthcare & Medicine',
'HR & Recruiting',
'Legal',
'Marketing & PR',
'Operations',
'Project & Product Management',
'Retail',
'Sales',
'Social Media & Community',
];
}
/**
* Get available experience levels
*/
public function getLevels(): array
{
return [
'Internship',
'Entry Level',
'Mid Level',
'Senior Level',
'Executive',
];
}
}