Passed
Push — master ( cb5015...0619f7 )
by Kuts
01:43
created

GuzzleClient::getResponseFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ExileeD\Inoreader\Client;
4
5
use ExileeD\Inoreader\Exception\InoreaderException;
6
use GuzzleHttp\Client as HttpClient;
7
use GuzzleHttp\Exception\GuzzleException;
8
use Psr\Http\Message\ResponseInterface;
9
10
class GuzzleClient implements ClientInterface
11
{
12
13
    /**
14
     * @var HttpClient
15
     */
16
    protected $client;
17
    /**
18
     * @var array
19
     */
20
    private $options = [
21
        'base_uri'    => 'https://www.inoreader.com/reader/api/0/',
22
        'user_agent'  => 'inoreader-php/1.0.0 (+https://github.com/exileed/inoreader-api)',
23
        'timeout'     => 10,
24
        'verify_peer' => true,
25
    ];
26
27
    public function __construct()
28
    {
29
        $this->client = new HttpClient([
30
            'base_uri' => $this->getApiBaseUrl(),
31
        ]);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getApiBaseUrl(): string
38
    {
39
        return $this->options[ 'base_uri' ];
40
    }
41
42
    public function get($endpoint, $params = [], $headers = []): ResponseInterface
43
    {
44
        $query = [
45
            'query' => $params,
46
        ];
47
48
        return $this->request($endpoint, $query, 'GET', $headers);
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function request($endpoint, $params = [], $method = 'GET', array $headers = []): ResponseInterface
55
    {
56
57
        $options = array_merge($params, [
58
            'User-Agent' => $this->options[ 'user_agent' ],
59
            'headers'    => $headers,
60
        ]);
61
62
        try {
63
            return $this->getClient()->request($method, $endpoint, $options);
64
        } catch (GuzzleException $e) {
65
            throw new InoreaderException($e->getMessage());
66
        }
67
    }
68
69
    /**
70
     * @access public
71
     * @return HttpClient
72
     */
73
    public function getClient(): HttpClient
74
    {
75
        return $this->client;
76
    }
77
78
    /**
79
     * @param HttpClient $client
80
     */
81
    public function setClient(HttpClient $client): void
82
    {
83
        $this->client = $client;
84
    }
85
86
    public function post($endpoint, $params = [], $headers = []): ResponseInterface
87
    {
88
89
        $body = [
90
            'form_params' => $params,
91
        ];
92
93
        return $this->request($endpoint, $body, 'POST', $headers);
94
    }
95
}