|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of the Tmdb package. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Vincent Faliès <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Vincent Faliès <[email protected]> |
|
13
|
|
|
* @copyright Copyright (c) 2017-2020 |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
namespace VfacTmdb\lib\Guzzle; |
|
18
|
|
|
|
|
19
|
|
|
use VfacTmdb\Interfaces\HttpRequestInterface; |
|
20
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
21
|
|
|
use VfacTmdb\Exceptions\NotFoundException; |
|
22
|
|
|
use VfacTmdb\Exceptions\ServerErrorException; |
|
23
|
|
|
use VfacTmdb\Exceptions\HttpErrorException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* HTTP Client class for all HTTP request |
|
27
|
|
|
* @package Tmdb |
|
28
|
|
|
* @author Vincent Faliès <[email protected]> |
|
29
|
|
|
* @copyright Copyright (c) 2017 |
|
30
|
|
|
*/ |
|
31
|
|
|
class Client implements HttpRequestInterface |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Client variable |
|
36
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $guzzleClient; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* @param \GuzzleHttp\ClientInterface $guzzleClient |
|
43
|
|
|
*/ |
|
44
|
1368 |
|
public function __construct(\GuzzleHttp\ClientInterface $guzzleClient) |
|
45
|
|
|
{ |
|
46
|
1368 |
|
$this->guzzleClient = $guzzleClient; |
|
47
|
1368 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Send response method from specific http method |
|
51
|
|
|
* @param string $method Http method (GET, POST, DELETE) |
|
52
|
|
|
* @param string $url |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
* @param array $form_params |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
36 |
|
private function sendResponse(string $method, string $url, array $options = [], array $form_params = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
24 |
|
switch ($method) { |
|
61
|
36 |
|
case 'GET': |
|
62
|
12 |
|
$params = array_merge($options, [ |
|
63
|
12 |
|
'headers' => ['Content-Type' => 'application/json;charset=utf-8'] |
|
64
|
|
|
]); |
|
65
|
12 |
|
break; |
|
66
|
|
|
default: |
|
67
|
24 |
|
$params = array_merge($options, [ |
|
68
|
24 |
|
'headers' => ['Content-Type' => 'application/json;charset=utf-8'], |
|
69
|
24 |
|
'body' => json_encode($form_params) |
|
70
|
|
|
]); |
|
71
|
24 |
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
36 |
|
return $this->guzzleClient->request($method, $url, $params); |
|
75
|
27 |
|
} catch (RequestException $e) { |
|
76
|
21 |
|
if (is_null($e->getResponse())) { |
|
77
|
3 |
|
throw new HttpErrorException; |
|
78
|
|
|
} |
|
79
|
18 |
|
switch ((int) $e->getResponse()->getStatusCode()) { |
|
80
|
18 |
|
case 404: |
|
81
|
9 |
|
throw new NotFoundException($e->getMessage()); |
|
82
|
|
|
default: |
|
83
|
9 |
|
throw new ServerErrorException($e->getMessage()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get response method |
|
90
|
|
|
* @param string $url |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
12 |
|
public function getResponse(string $url) |
|
94
|
|
|
{ |
|
95
|
12 |
|
return $this->sendResponse('GET', $url); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Post response method |
|
100
|
|
|
* @param string $url |
|
101
|
|
|
* @param array $options |
|
102
|
|
|
* @param array $form_params |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
15 |
|
public function postResponse(string $url, array $options = [], array $form_params = array()) |
|
106
|
|
|
{ |
|
107
|
15 |
|
return $this->sendResponse('POST', $url, $options, $form_params); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Delete response method |
|
112
|
|
|
* @param string $url |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
9 |
|
public function deleteResponse(string $url) |
|
116
|
|
|
{ |
|
117
|
9 |
|
return $this->sendResponse('DELETE', $url); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|