1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Konsulting\JustGivingApiSdk; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Konsulting\JustGivingApiSdk\Exceptions\ClassNotFoundException; |
7
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\AccountClient; |
8
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\CampaignClient; |
9
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\CharityClient; |
10
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\CountriesClient; |
11
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\CurrencyClient; |
12
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\DonationClient; |
13
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\EventClient; |
14
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\FundraisingClient; |
15
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\LeaderboardClient; |
16
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\OneSearchClient; |
17
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\ProjectClient; |
18
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\SearchClient; |
19
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\SmsClient; |
20
|
|
|
use Konsulting\JustGivingApiSdk\ResourceClients\TeamClient; |
21
|
|
|
use Konsulting\JustGivingApiSdk\Support\Auth\AuthValue; |
22
|
|
|
use Konsulting\JustGivingApiSdk\Support\Response; |
23
|
|
|
use Psr\Http\Client\ClientInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class JustGivingClient |
27
|
|
|
* |
28
|
|
|
* @property AccountClient account |
29
|
|
|
* @property CampaignClient campaign |
30
|
|
|
* @property CharityClient charity |
31
|
|
|
* @property CountriesClient countries |
32
|
|
|
* @property CurrencyClient currency |
33
|
|
|
* @property DonationClient donation |
34
|
|
|
* @property EventClient event |
35
|
|
|
* @property LeaderboardClient leaderboard |
36
|
|
|
* @property OneSearchClient oneSearch |
37
|
|
|
* @property FundraisingClient fundraising |
38
|
|
|
* @property ProjectClient project |
39
|
|
|
* @property SearchClient search |
40
|
|
|
* @property SmsClient sms |
41
|
|
|
* @property TeamClient team |
42
|
|
|
* @property AccountClient Account |
43
|
|
|
* @property CampaignClient Campaign |
44
|
|
|
* @property CharityClient Charity |
45
|
|
|
* @property CountriesClient Countries |
46
|
|
|
* @property CurrencyClient Currency |
47
|
|
|
* @property DonationClient Donation |
48
|
|
|
* @property EventClient Event |
49
|
|
|
* @property LeaderboardClient Leaderboard |
50
|
|
|
* @property ProjectClient Project |
51
|
|
|
* @property SearchClient Search |
52
|
|
|
* @property SmsClient Sms |
53
|
|
|
* @property TeamClient Team |
54
|
|
|
*/ |
55
|
|
|
class JustGivingClient |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* The clients that have been instantiated. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $clients = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The client to execute the HTTP requests. |
66
|
|
|
* |
67
|
|
|
* @var ClientInterface |
68
|
|
|
*/ |
69
|
|
|
protected $httpClient; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The root domain of the API. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $rootDomain; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The API version to use. |
80
|
|
|
* |
81
|
|
|
* @var int |
82
|
|
|
*/ |
83
|
|
|
protected $apiVersion; |
84
|
|
|
/** |
85
|
|
|
* @var AuthValue |
86
|
|
|
*/ |
87
|
|
|
private $auth; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* JustGivingClient constructor. |
91
|
|
|
* |
92
|
|
|
* @param AuthValue $auth |
93
|
|
|
* @param ClientInterface $client |
94
|
|
|
* @param array $options |
95
|
|
|
*/ |
96
|
9 |
|
public function __construct(AuthValue $auth, ClientInterface $client, $options = []) |
97
|
|
|
{ |
98
|
9 |
|
$this->auth = $auth; |
99
|
9 |
|
$this->httpClient = $client; |
100
|
9 |
|
$this->rootDomain = $options['root_domain'] ?? 'https://api.justgiving.com'; |
101
|
9 |
|
$this->apiVersion = $options['api_version'] ?? 1; |
102
|
9 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Proxy a request onto the HTTP client, using the fully qualified URI. |
106
|
|
|
* |
107
|
|
|
* @param string $method |
108
|
|
|
* @param string $uri |
109
|
|
|
* @param array $options |
110
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
111
|
|
|
*/ |
112
|
9 |
|
public function request($method, $uri, $options = []) |
113
|
|
|
{ |
114
|
9 |
|
$headers = $options['headers'] ?? []; |
115
|
9 |
|
$body = $options['body'] ?? null; |
116
|
9 |
|
$version = $options['version'] ?? '1.1'; |
117
|
|
|
|
118
|
9 |
|
if (isset($options['json'])) { |
119
|
5 |
|
$headers += ['Content-Type' => 'application/json']; |
120
|
5 |
|
$body = json_encode($options['json']); |
121
|
|
|
} |
122
|
|
|
|
123
|
9 |
|
$request = new Request($method, $this->buildUri($uri), $this->buildHeaders($headers), $body, $version); |
124
|
|
|
|
125
|
9 |
|
$response = $this->httpClient->sendRequest($request); |
126
|
|
|
|
127
|
9 |
|
return new Response($response); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Build the full URI using the root URI and API version. |
132
|
|
|
* |
133
|
|
|
* @param string $uri |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
9 |
|
private function buildUri($uri) |
137
|
|
|
{ |
138
|
9 |
|
return $this->rootDomain . '/v' . $this->apiVersion . '/' . $uri; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Merge the per-request headers with the auth headers. |
143
|
|
|
* |
144
|
|
|
* @param array $requestHeaders |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
9 |
|
private function buildHeaders($requestHeaders) |
148
|
|
|
{ |
149
|
9 |
|
$defaultHeaders = ['Accept' => 'application/json']; |
150
|
|
|
|
151
|
9 |
|
return array_merge($defaultHeaders, $this->auth->getHeaders(), $requestHeaders); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Allow API classes to be called as properties. Return a singleton client class. |
156
|
|
|
* |
157
|
|
|
* @param string $property |
158
|
|
|
* @return mixed |
159
|
|
|
* @throws \Exception |
160
|
|
|
*/ |
161
|
9 |
|
public function __get($property) |
162
|
|
|
{ |
163
|
9 |
|
$class = __NAMESPACE__ . '\\ResourceClients\\' . ucfirst($property) . 'Client'; |
164
|
|
|
|
165
|
9 |
|
if (! class_exists($class)) { |
166
|
|
|
throw new ClassNotFoundException($class); |
167
|
|
|
} |
168
|
|
|
|
169
|
9 |
|
$this->clients[$class] = isset($this->clients[$class]) |
170
|
3 |
|
? $this->clients[$class] |
171
|
9 |
|
: new $class($this); |
172
|
|
|
|
173
|
9 |
|
return $this->clients[$class]; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|