BaseClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A httpPostFrom() 0 3 1
A getBaseUrl() 0 3 1
A httpPostJson() 0 3 1
A getAccessToken() 0 3 1
A request() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the strays/baidu-ai.
5
 *
6
 * (c) strays <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Strays\BaiDuAi\Kernel;
13
14
use Strays\BaiDuAi\Kernel\Contracts\AccessTokenInterface;
15
use Strays\BaiDuAi\Kernel\Traits\HttpRequests;
16
17
class BaseClient
18
{
19
    use HttpRequests { request as performRequest; }
20
21
    /**
22
     * @var ServiceContainer
23
     */
24
    protected $app;
25
26
    /**
27
     * @var mixed|AccessTokenInterface
28
     */
29
    protected $accessToken;
30
31
    /**
32
     * @var array
33
     */
34
    protected static $defaults = [
35
        'charset' => 'UTF-8',
36
    ];
37
38
    /**
39
     * BaseClient constructor.
40
     *
41
     * @param ServiceContainer          $app
42
     * @param AccessTokenInterface|null $accessToken
43
     */
44
    public function __construct(ServiceContainer $app, AccessTokenInterface $accessToken = null)
45
    {
46
        $this->app = $app;
47
48
        $this->accessToken = $accessToken ?? $this->app['access_token'];
49
    }
50
51
    /**
52
     * @param string $url
53
     * @param array  $data
54
     * @param array  $query
55
     *
56
     * @return string
57
     */
58
    public function httpPostJson(string $url, array $data = [], array $query = [])
59
    {
60
        return $this->request($this->getBaseUrl($url), 'POST', ['query' => $query, 'json' => $data]);
61
    }
62
63
    /**
64
     * @param string $url
65
     * @param array  $data
66
     * @param array  $query
67
     *
68
     * @return string
69
     */
70
    public function httpPostFrom(string $url, array $data = [], array $query = [])
71
    {
72
        return $this->request($this->getBaseUrl($url), 'POST', ['query' => $query, 'from' => $data]);
73
    }
74
75
    /**
76
     * @param string $url
77
     * @param string $method
78
     * @param array  $options
79
     *
80
     * @return string
81
     */
82
    protected function request(string $url, string $method, array $options = [])
83
    {
84
        if (!$this->accessToken) {
85
            $this->getAccessToken();
86
        }
87
88
        $response = $this->performRequest($url, $method, $options);
89
90
        return $response->getBody()->getContents();
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    protected function getAccessToken()
97
    {
98
        return $this->accessToken->applyToRequest();
99
    }
100
101
    /**
102
     * 组装根url.
103
     *
104
     * @param string $url
105
     *
106
     * @return string
107
     */
108
    protected function getBaseUrl(string $url)
109
    {
110
        return $url.'?'.http_build_query(array_merge(self::$defaults, $this->getAccessToken()));
111
    }
112
}
113