-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.php
183 lines (156 loc) · 4.58 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Maicol07\Flarum\Api;
use Illuminate\Cache\ArrayStore;
use Illuminate\Support\Arr;
use Maicol07\Flarum\Api\Response\Factory;
use Psr\Http\Message\ResponseInterface;
/**
* Class Client
* @package Maicol07\Client\Api
* @mixin Fluent
*/
class Client
{
/** @var Cache */
protected static $cache;
/** @var \GuzzleHttp\Client */
protected $client;
/** @var Fluent */
protected $fluent;
/** @var bool Whether to enforce specific markup/variables setting. */
protected $authorized = false;
/** @var bool */
protected $strict = true;
/**
* Client constructor.
*
* @param string $host Full FQDN hostname to your Flarum forum, eg http://example.com/forum
* @param array $authorization Holding either "token" or "username" and "password" as keys.
* @param array $options Custom options for the HTTP Client
*/
public function __construct(string $host, array $authorization = [], array $options = [])
{
$this->client = new \GuzzleHttp\Client(array_merge([
'base_uri' => "$host/api/",
'headers' => $this->getHeaders($authorization)
], $options));
$this->fluent = new Fluent($this);
static::$cache = new Cache(new ArrayStore);
}
/**
* Request to Flarum the specified resource
*
* @return bool|Resource\Collection|Resource\Item|null
*
* @noinspection PhpInconsistentReturnPointsInspection
* @noinspection MissingReturnTypeInspection
*/
public function request()
{
$method = $this->fluent->getMethod();
$options = $this->getVariablesForMethod();
$additional_headers = $this->fluent->getHeaders();
if (!empty($additional_headers)) {
$headers = array_merge($this->client->getConfig('headers'), $additional_headers);
$options = array_merge($options, ['headers' => $headers]);
}
try {
$response = $this->client->{$method}((string)$this->fluent, $options);
assert($response instanceof ResponseInterface);
} finally {
// Reset the fluent builder for a new request.
$this->fluent->reset();
}
/** @noinspection NotOptimalIfConditionsInspection */
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
return Factory::build($response);
}
}
/**
* Get request headers
*
* @param array $authorization
*
* @return string[]
*/
protected function getHeaders(array $authorization = []): array
{
$headers = [
'Accept' => 'application/vnd.api+json, application/json',
'User-Agent' => 'Maicol07 Flarum Api Client'
];
$token = Arr::get($authorization, 'token');
if ($token) {
$this->authorized = true;
Arr::set($headers, 'Authorization', "Token $token; userId=1");
}
return $headers;
}
/**
* Get the cache object
*
* @return Cache
*/
public static function getCache(): Cache
{
return self::$cache;
}
/**
* Get body variables
*
* @return array|array[]
*/
protected function getVariablesForMethod(): array
{
$variables = $this->fluent->getVariables();
if (empty($variables)) {
return [];
}
if ($this->fluent->getMethod() === 'get') {
return $variables;
}
return in_array($this->fluent->getType(), $this->fluent->typesWithoutJsonApi, true) ? [
'json' => $variables
] : [
'json' => ['data' => $variables]
];
}
/**
* @param string $name
* @param mixed $arguments
* @return false|mixed
*
* @noinspection MissingReturnTypeInspection
* @noinspection MissingParameterTypeDeclarationInspection
*/
public function __call(string $name, $arguments)
{
return call_user_func_array([$this->fluent, $name], $arguments);
}
public function getFluent(): Fluent
{
return $this->fluent;
}
public function getClient(): \GuzzleHttp\Client
{
return $this->client;
}
public function isStrict(): bool
{
return $this->strict;
}
public function setStrict(bool $strict): Client
{
$this->strict = $strict;
return $this;
}
/**.
* Checks if user is authorized (an authorization header with the api token exists)
*
* @return bool
*/
public function isAuthorized(): bool
{
return $this->authorized;
}
}