BaseClient.php
1.6 KB
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
<?php
namespace Lackoxygen\TiktokOpen\Base\Traits;
use Lackoxygen\TiktokOpen\Base\Client\Request;
use Lackoxygen\TiktokOpen\Base\Event\Fail as FailEvent;
use Lackoxygen\TiktokOpen\Base\Event\Request as RequestEvent;
use Lackoxygen\TiktokOpen\Base\Event\Response as ResponseEvent;
use Lackoxygen\TiktokOpen\Base\Utils\Json;
use GuzzleHttp\Exception\ClientException;
trait BaseClient
{
public function get($url, $params = [])
{
return $this->request('GET', $url, $params);
}
public function post($url, $params = [])
{
return $this->request('POST', $url, $params);
}
public function put($url, $params = [])
{
return $this->request('PUT', $url, $params);
}
public function delete($url, $params = [])
{
return $this->request('DELETE', $url, $params);
}
public function request(string $method, string $path, array $data = [])
{
$request = new Request(
$method,
$this->requestOption,
$path,
$data,
$this->withSession
);
RequestEvent::dispatch($this->app, $request);
try {
$response = $this->app['guzzleHttp']->request(
$method,
$path,
$request->toArray()
);
ResponseEvent::dispatch($response);
$response->getBody()->rewind();
return Json::unmarshal($response->getBody()->getContents());
} catch (ClientException $exception) {
FailEvent::dispatch($exception);
} finally {
$this->refresh();
}
}
}