HttpRequests::request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
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\Traits;
13
14
use GuzzleHttp\Client;
15
use GuzzleHttp\ClientInterface;
16
17
trait HttpRequests
18
{
19
    /**
20
     * @var
21
     */
22
    protected $httpClient;
23
24
    /**
25
     * @param $url
26
     * @param $method
27
     * @param array $options
28
     *
29
     * @return mixed|\Psr\Http\Message\ResponseInterface
30
     */
31
    public function request($url, $method, array $options = [])
32
    {
33
        $options = $this->fixJsonIssue($options);
34
35
        $response = $this->getHttpClient()->request($method, $url, $options);
36
37
        return $response;
38
    }
39
40
    /**
41
     * @param ClientInterface $client
42
     *
43
     * @return $this
44
     */
45
    public function setHttpClient(ClientInterface $client)
46
    {
47
        $this->httpClient = $client;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return Client|ClientInterface
54
     */
55
    public function getHttpClient()
56
    {
57
        if (!($this->httpClient instanceof ClientInterface)) {
58
            if (property_exists($this, 'app') && $this->app['http_client']) {
59
                $this->httpClient = $this->app['http_client'];
60
            } else {
61
                $this->httpClient = new Client($this->app->getConfig());
62
            }
63
        }
64
65
        return $this->httpClient;
66
    }
67
68
    /**
69
     * @param array $options
70
     *
71
     * @return array
72
     */
73
    protected function fixJsonIssue(array $options): array
74
    {
75
        if (isset($options['json']) && is_array($options['json'])) {
76
            $options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json;charset=utf-8']);
77
78
            if (!empty($options['json'])) {
79
                $options['body'] = mb_convert_encoding(\GuzzleHttp\json_encode($options['json']), 'GBK', 'UTF8');
80
            }
81
82
            unset($options['json']);
83
            unset($options['query']);
84
        }
85
86
        if (isset($options['from']) && is_array($options['from'])) {
87
            $options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/x-www-form-urlencoded']);
88
            $options['form_params'] = array_merge($options['body'] ?? [], $options['from']);
89
90
            unset($options['from']);
91
            unset($options['query']);
92
        }
93
94
        if (!$this->app['config']->get('ssl')) {
95
            $options['verify'] = false;
96
        }
97
98
        return $options;
99
    }
100
}
101