|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Konsulting\JustGivingApiSdk\Support; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\HandlerStack; |
|
7
|
|
|
use GuzzleHttp\Middleware; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class GuzzleClientFactory |
|
12
|
|
|
* |
|
13
|
|
|
* Helper class to instantiate a Guzzle HTTP client with the correct configuration for passing into the JustGiving |
|
14
|
|
|
* client. |
|
15
|
|
|
*/ |
|
16
|
|
|
class GuzzleClientFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
protected $apiKey; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $apiVersion; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $username; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $password; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $rootDomain; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
protected $userOptions; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* GuzzleClientFactory constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $rootDomain |
|
40
|
|
|
* @param string $apiKey |
|
41
|
|
|
* @param string $apiVersion |
|
42
|
|
|
* @param string $username |
|
43
|
|
|
* @param string $password |
|
44
|
|
|
* @param array $options |
|
45
|
|
|
*/ |
|
46
|
76 |
|
public function __construct($rootDomain, $apiKey, $apiVersion, $username = '', $password = '', $options = []) |
|
47
|
|
|
{ |
|
48
|
76 |
|
$this->rootDomain = $rootDomain; |
|
49
|
76 |
|
$this->apiKey = $apiKey; |
|
50
|
76 |
|
$this->apiVersion = $apiVersion; |
|
51
|
76 |
|
$this->username = $username; |
|
52
|
76 |
|
$this->password = $password; |
|
53
|
76 |
|
$this->userOptions = $options; |
|
54
|
76 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Static method for easily creating a client. Requires the same parameters as the class constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $rootDomain |
|
60
|
|
|
* @param string $apiKey |
|
61
|
|
|
* @param string $apiVersion |
|
62
|
|
|
* @param string $username |
|
63
|
|
|
* @param string $password |
|
64
|
|
|
* @param array $options |
|
65
|
|
|
* @return Client |
|
66
|
|
|
*/ |
|
67
|
75 |
|
public static function build($rootDomain, $apiKey, $apiVersion, $username = '', $password = '', $options = []) |
|
68
|
|
|
{ |
|
69
|
75 |
|
return (new static($rootDomain, $apiKey, $apiVersion, $username, $password, $options))->createClient(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set up the handler stack with middleware, configure options and instantiate the Guzzle client. |
|
74
|
|
|
* |
|
75
|
|
|
* @return Client |
|
76
|
|
|
*/ |
|
77
|
75 |
|
public function createClient() |
|
78
|
|
|
{ |
|
79
|
75 |
|
$stack = HandlerStack::create(); |
|
80
|
75 |
|
$stack->push(Middleware::mapResponse(function (ResponseInterface $response) { |
|
81
|
63 |
|
return new Response($response); |
|
82
|
75 |
|
})); |
|
83
|
|
|
|
|
84
|
75 |
|
return new Client(array_merge([ |
|
85
|
75 |
|
'http_errors' => false, |
|
86
|
75 |
|
'handler' => $stack, |
|
87
|
75 |
|
'base_uri' => $this->baseUrl(), |
|
88
|
|
|
'headers' => [ |
|
89
|
75 |
|
'Accept' => 'application/json', |
|
90
|
75 |
|
'Authorize' => 'Basic ' . $this->buildAuthenticationValue(), |
|
91
|
75 |
|
'Authorization' => 'Basic ' . $this->buildAuthenticationValue(), |
|
92
|
|
|
] |
|
93
|
75 |
|
], $this->userOptions)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return the base URL string for the API call. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
75 |
|
public function baseUrl() |
|
102
|
|
|
{ |
|
103
|
75 |
|
return $this->rootDomain . $this->apiKey . '/v' . $this->apiVersion . '/'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Build the base 64 encoded string that contains authentication credentials. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
76 |
|
protected function buildAuthenticationValue() |
|
112
|
|
|
{ |
|
113
|
76 |
|
return empty($this->username) |
|
114
|
1 |
|
? '' |
|
115
|
76 |
|
: base64_encode($this->username . ":" . $this->password); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|