-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathGuzzle.php
More file actions
110 lines (92 loc) · 2.76 KB
/
Guzzle.php
File metadata and controls
110 lines (92 loc) · 2.76 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Cloudflare\API\Adapter;
use Cloudflare\API\Auth\Auth;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
class Guzzle implements Adapter
{
private $client;
/**
* @inheritDoc
*/
public function __construct(Auth $auth, string $baseURI = null)
{
if ($baseURI === null) {
$baseURI = 'https://api.cloudflare.com/client/v4/';
}
$headers = $auth->getHeaders();
$this->client = new Client([
'base_uri' => $baseURI,
'headers' => $headers,
'Accept' => 'application/json'
]);
}
/**
* @inheritDoc
*/
public function get(string $uri, array $data = [], array $headers = []): ResponseInterface
{
return $this->request('get', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface
{
return $this->request('post', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function put(string $uri, array $data = [], array $headers = []): ResponseInterface
{
return $this->request('put', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function patch(string $uri, array $data = [], array $headers = []): ResponseInterface
{
return $this->request('patch', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function delete(string $uri, array $data = [], array $headers = []): ResponseInterface
{
return $this->request('delete', $uri, $data, $headers);
}
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function request(string $method, string $uri, array $data = [], array $headers = [])
{
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
}
try {
$methodData = $this->getMethodData($method, $data);
$response = $this->client->$method($uri, ['headers' => $headers] + $methodData);
} catch (RequestException $err) {
throw ResponseException::fromRequestException($err);
}
return $response;
}
/**
* Gets the data for the request as per the method.
* @param string $method
* @param array $data
* @return array|array[]
*/
public function getMethodData(string $method, array $data): array
{
if ($method === 'get') {
return ['query' => $data];
}
if (array_key_exists('multipart', $data)) {
return $data;
}
return ['json' => $data];
}
}