1. Go to this page and download the library: Download laragear/api-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laragear / api-manager example snippets
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->chirp('Hello world!');
namespace App\Http\Apis;
use Laragear\ApiManager\ApiServer;
class Chirper extends ApiServer
{
/**
* The headers to ist of simple actions for this API.
*
* @var array|string[]
*/
public $actions = [
'latest' => '/',
'create' => 'post:new',
];
/**
* Returns the API base URL.
*
* @return string
*/
public function getBaseUrl()
{
return app()->isProduction()
? 'https://chirper.com/api/v1'
: 'https://dev.chirper.com/api/v1';
}
/**
* Returns the Bearer Token used for authentication.
*
* @return string
*/
protected function authToken()
{
return config('services.chirper.secret');
}
}
/**
* The list of simple actions for this API.
*
* @var array|string[]
*/
protected $actions = [
'new chirp' => 'post:new',
'latest' => 'latest',
'view' => 'chirp/{id}',
'edit' => 'update:chirp/{id}',
'delete' => 'delete:chirp/{id}',
];
use App\Http\Apis\Chirper;
// Create a new chirp.
$chirp = Chirper::api()->newChirp(['message' => 'This should be complex']);
use App\Http\Apis\Chirper;
// Edit a chirp.
Chirper::api(['id' => 231])->edit(['message' => 'No, it was a breeze!']);
// Same as:
Chirper::api('chirper')->withUrlParameters(['id' => 231])->edit(['message' => 'No, it was a breeze!']);
use App\Http\Apis\Chirper;
$latestChirps = Chirper::api()->latest;
use Illuminate\Http\Client\PendingRequest;
public function newChirp(PendingRequest $request, string $message)
{
return $request->connectTimeout(10)->post('new', ['message' => $message]);
}
public function noReply(PendingRequest $request)
{
$request->withHeaders(['X-No-Reply' => 'false'])
return $this;
}
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->newChirp('Easy peasy');
use App\Http\Apis\Chirper;
$latest = Chirper::api()->noReply->newChirp('Easy peasy');
/**
* Returns the Basic Authentication to use against the API.
*
* @var array{string:string}|void
*/
public function authBasic()
{
return app()->isProduction()
? ['[email protected]', 'real-password']
: ['[email protected]', 'fake-password'];
}
> // This is supported, but discouraged!
> return ['username' => 'app@chirper', 'password' => 'real-password'];
>
use Illuminate\Http\Client\PendingRequest;
public function beforeBuild(PendingRequest $request)
{
//
}
public function afterBuild(PendingRequest $request)
{
//
}
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->timeout(5)->latest();
use Illuminate\Filesystem\Filesystem;
use Laragear\ApiManager\ApiServer;
class Chirper extends ApiServer
{
public function __construct(protected Filesystem $file)
{
if ($this->file->missing('important_file.txt')) {
throw new RuntimeException('Important file missing!')
}
}
// ...
}
// app\Providers\AppServiceProvider.php
use App\Http\Apis\Chirper;
public function register()
{
$this->app->bind(Chirper::class, function () {
return new Chirper(config('services.chirper.version'));
})
}
use Illuminate\Support\Facades\Http;
use App\Http\Apis\Chirper;
use App\Http\Apis\Twitter;
$responses = Http::pool(fn ($pool) => [
Chirper::api()->on($pool)->chirp('Hello world!'),
Twitter::api()->on($pool)->tweet('Goodbye world!'),
$pool->post('mastodon.org/api', ['message' => 'Greetings citizens!'])
]);
return $responses[0]->ok();
use Illuminate\Support\Facades\Http;
use App\Http\Apis\Chirper;
use App\Http\Apis\Twitter;
$responses = Http::pool(fn ($pool) => [
Chirper::api()->on($pool, 'first')->chirp('Hello world!'),
Twitter::api()->on($pool, 'second')->tweet('Goodbye world!'),
$pool->as('third')->post('mastodon.org/api', ['message' => 'Greetings citizens!'])
]);
return $responses['first']->ok();
namespace App\Http\Apis\Chirper\Responses;
use Illuminate\Http\Client\Response;
class ViewResponse extends Response
{
//
}
public function isPrivate(): bool
{
return $this->json('metadata.is_private', false)
}
/**
* Actions and methods to wrap into a custom response class.
*
* @var array<string, class-string>
*/
protected $responses = [
'view' => Responses\ViewResponse::class,
];
use App\Http\Apis\Chirper;
$chirp = Chirper::api(['id' => 5])->view();
if ($chirp->successful() && $chirp->isPrivate()) {
return 'The chirp cannot be seen publicly.';
}
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Request;
public function test_creates_new_chirp(): void
{
Http::fake(function (Request $request) {
return Http::response([
'posted' => 'ok',
...json_decode($request->body())
], 200);
});
$this->post('spread-message', ['message' => 'Hello world!'])
->assertSee('Posted!');
}