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