mirror of
https://github.com/flarum/framework.git
synced 2025-01-07 19:13:37 +08:00
Create an API client class.
This should make it easier to make API calls from the frontends.
This commit is contained in:
parent
bc65a44199
commit
6c3acd06bf
36
framework/core/src/Api/Client.php
Normal file
36
framework/core/src/Api/Client.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Api;
|
||||||
|
|
||||||
|
use Flarum\Support\Actor;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class Client
|
||||||
|
{
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
protected $actor;
|
||||||
|
|
||||||
|
public function __construct(Container $container, Actor $actor)
|
||||||
|
{
|
||||||
|
$this->container = $container;
|
||||||
|
$this->actor = $actor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the given API action class, pass the input and return its response.
|
||||||
|
*
|
||||||
|
* @param string $actionClass
|
||||||
|
* @param array $input
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function send($actionClass, array $input = [])
|
||||||
|
{
|
||||||
|
/** @var \Flarum\Api\Actions\JsonApiAction $action */
|
||||||
|
$action = $this->container->make($actionClass);
|
||||||
|
|
||||||
|
$response = $action->handle(new Request($input, $this->actor));
|
||||||
|
|
||||||
|
return json_decode($response->getBody());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user