Installation Add the package via Composer in your Laravel project:
composer require antwebes/chatea-client-lib
Ensure your composer.json includes the package under require.
Basic Initialization Import the client in your Laravel service or controller:
use Antwebes\ChateaClientLib\Client;
use Antwebes\ChateaClientLib\OAuth2;
// Initialize OAuth2 (if needed)
$oauth = new OAuth2([
'client_id' => env('CHATEA_CLIENT_ID'),
'client_secret' => env('CHATEA_CLIENT_SECRET'),
'redirect_uri' => env('CHATEA_REDIRECT_URI'),
]);
// Initialize the client
$client = new Client($oauth);
First API Call (Example: Fetching Resources) Use URI templates to fetch data (e.g., users, messages):
$users = $client->get('/users', [
'limit' => 10,
'offset' => 0,
]);
The package supports resource iterators for paginated results.
$authUrl = $oauth->getAuthorizationUrl();
return redirect()->to($authUrl);
Handle the callback in Laravel’s routes:
Route::get('/callback', function () {
$token = $oauth->handleAuthorizationCallback(request()->query);
session(['chatea_token' => $token]);
});
session, cache, or database (e.g., config('services.chatea.token')).{user_id}) for RESTful routes:
$user = $client->get('/users/{user_id}', ['user_id' => 123]);
$messages = $client->get('/messages', [
'since' => Carbon::now()->subDays(7),
'limit' => 50,
]);
$iterator = $client->getIterator('/posts');
foreach ($iterator as $post) {
// Process each post
}
Customize pagination via options:
$iterator = $client->getIterator('/posts', [
'limit' => 20,
'page' => 1,
]);
class ChateaService {
protected $client;
public function __construct(Client $client) {
$this->client = $client;
}
public function fetchUser($userId) {
return $this->client->get('/users/{user_id}', ['user_id' => $userId]);
}
}
Bind the service in Laravel’s AppServiceProvider:
$this->app->bind(ChateaService::class, function ($app) {
return new ChateaService(new Client($app->make(OAuth2::class)));
});
try {
$data = $client->get('/protected-data');
} catch (\Antwebes\ChateaClientLib\Exception\ApiException $e) {
Log::error("Chatea API Error: " . $e->getMessage());
return response()->json(['error' => 'Failed to fetch data'], 500);
}
if ($oauth->isTokenExpired()) {
$refreshedToken = $oauth->refreshToken(session('chatea_refresh_token'));
session(['chatea_token' => $refreshedToken]);
}
redirect_uri in OAuth2 config matches Chatea’s registered URI.$userId = urlencode(123); // If passed directly
$client->get('/users/{user_id}', ['user_id' => $userId]);
/Users vs /users).$cacheKey = "chatea_users_{$userId}";
$user = Cache::remember($cacheKey, now()->addHours(1), function () use ($client, $userId) {
return $client->get('/users/{user_id}', ['user_id' => $userId]);
});
getIterator for large datasets to avoid memory issues.$client->setDebug(true); // If the package exposes this method
$response = $client->get('/debug-endpoint');
dd($response->getBody());
$client->setDefaultOption('headers', [
'X-Custom-Header' => 'value',
]);
mkdir docs
phpdoc -d ./vendor/antwebes/chatea-client-lib/src -t docs/ --template responsive
Note: Paths may need adjustment based on Composer’s vendor directory.How can I help you explore Laravel packages today?