This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Client.php
139 lines (115 loc) · 3.42 KB
/
Client.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Amelia\Monzo;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Client as Guzzle;
use Amelia\Monzo\Exceptions\MonzoException;
use Amelia\Monzo\Contracts\Client as ClientContract;
use Amelia\Monzo\Exceptions\UnexpectedValueException;
class Client implements ClientContract
{
use UsesTokens, UsesQueryParams;
/**
* The guzzle client.
*
* @var \GuzzleHttp\Client
*/
protected $guzzle;
/**
* Create a new client instance.
*
* @param \GuzzleHttp\Client $guzzle
* @param string|null $id
* @param string|null $secret
*/
public function __construct(Guzzle $guzzle, ?string $id = null, ?string $secret = null)
{
$this->guzzle = $guzzle;
if ($id !== null) {
$this->setClientId($id);
}
if ($secret !== null) {
$this->setClientSecret($secret);
}
}
/**
* Do an API call.
*
* @param string $method
* @param string $endpoint
* @param string|null $key
* @param array $query
* @param array $data
* @param bool $raw
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function call(string $method, string $endpoint, array $query = [], array $data = [], ?string $key = null, bool $raw = false)
{
$endpoint = $this->endpoint($endpoint, $query);
$options = [
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => false,
];
if ($this->token) {
$options['headers']['Authorization'] = 'Bearer ' . $this->token;
}
if ($data) {
$options['form_params'] = $data;
}
$response = $this->guzzle->request($method, $endpoint, $options);
return $raw ? $response : $this->parse($response, $key);
}
/**
* Get an endpoint (including query params).
*
* @param string $endpoint
* @param array $query
* @return string
*/
protected function endpoint(string $endpoint, array $query)
{
$params = $this->buildQueryParams($query);
return static::API_ENDPOINT . '/' . trim($endpoint, '/') . '?' . $params;
}
/**
* Clear all auth tokens/etc on this client instance.
*
* @return void
*/
protected function clear()
{
$this->token = null;
$this->params = [];
}
/**
* Parse out data from the API that we want.
*
* @param \GuzzleHttp\Psr7\Response $response
* @param string $key
* @return array
*/
protected function parse(Response $response, string $key = null)
{
$code = $response->getStatusCode();
$body = $response->getBody()->getContents();
$json = json_decode_response($response, $body);
if ($code >= 400 || $code < 200) {
$errorCode = array_get($json, 'error');
throw MonzoException::fromResponse($response, $body, $errorCode);
}
if ($key !== null && ! array_key_exists($key, $json)) {
throw new UnexpectedValueException("Expected to find a [$key] key within the response; none found.");
}
return $key === null ? $json : $json[$key];
}
/**
* Get a new client instance.
*
* @return \Amelia\Monzo\Client|\Amelia\Monzo\Contracts\Client
*/
public function newClient()
{
return new static($this->guzzle, $this->id, $this->secret);
}
}