forked from ghassani/surveymonkey-v3-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
189 lines (169 loc) · 4.68 KB
/
Client.php
File metadata and controls
189 lines (169 loc) · 4.68 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
* This file is part of the surveymonkey-v3-api-php package.
*
* (c) Gassan Idriss <ghassani@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Gassan Idriss <ghassani@gmail.com>
*/
namespace Spliced\SurveyMonkey;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Exception\ClientException;
use Spliced\SurveyMonkey\Api;
class Client
{
/** @const string */
const BASE_ENDPOINT = 'https://api.surveymonkey.net/v3/';
/** @var HttpClient */
protected $httpClient;
/** @var string */
protected $apiKey;
/** @var string */
protected $accessToken;
/**
* Include all traits to expose API methods
*/
use Api\CommonTrait;
use Api\UsersTrait;
use Api\SurveysTrait;
use Api\CollectorsTrait;
use Api\ResponsesTrait;
use Api\ContactTrait;
use Api\WebooksTrait;
use Api\BenchmarkTrait;
use Api\ErrorsTrait;
use Api\QuestionBankTrait;
use Api\SurveyFoldersTrait;
use Api\TranslationsTrait;
use Api\ResponseCountsAndTrendsTrait;
use Api\OrganizationsTrait;
/**
* Client constructor.
*
* @param $apiKey
* @param $accessToken
*/
public function __construct($apiKey, $accessToken)
{
$this->apiKey = $apiKey;
$this->accessToken = $accessToken;
$this->initHttpClient();
}
/**
* setAccessToken
*
* @param string $accessToken
*
* @return Client
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
$this->initHttpClient();
return $this;
}
/**
* getAccessToken
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* setApiKey
*
* @param string $apiKey
*
* @return Client
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
return $this;
}
/**
* getApiKey
*
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* getHttpClient
*
* @return \GuzzleHttp\Client
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* @param RequestInterface
* @return Response
* @throws SurveyMonkeyApiException if we got an error response from SurveyMonkey
* @throws \GuzzleHttp\Exception\GuzzleException on unexpected HTTP errors
*/
public function sendRequest(RequestInterface $request)
{
try {
$response = $this->httpClient->send($request);
} catch (ClientException $e) {
return new Response($request, $e->getResponse());
} catch (\Exception $e) {
throw new SurveyMonkeyApiException($e->getMessage(), $e->getCode(), $e);
}
return new Response($request, $response);
}
/**
* createRequest
*
* @param string $method
* @param string $uri
* @param array $options Guzzle compatible request options
* @param array|null $body Request body if applicable, using associative arrays for named properties & numeric
* arrays for array data types.
* @return RequestInterface
*/
private function createRequest($method, $uri, array $options = [], $body = null)
{
if (empty($body)) {
// Survey Monkey moved to CloudFront on 2020-05-23
// CloudFront issues 403 Forbidden with empty json body
// Previously this was set to an empty json object string. See https://stackoverflow.com/a/41150809/2803757
$bodyString = null;
} elseif (is_array($body) || $body instanceof \stdClass) {
$bodyString = json_encode($body);
}
$ret = new Request($method, $uri, [], $bodyString);
if (isset($options['query'])) {
$uri = $ret->getUri()->withQuery(is_array($options['query']) ? http_build_query($options['query']) : $options['query']);
return $ret->withUri($uri, true);
}
return $ret;
}
/**
* initHttpClient
*
* @return void
*/
private function initHttpClient()
{
$this->httpClient = new HttpClient([
'base_uri' => static::BASE_ENDPOINT,
'headers' => [
'User-Agent' => 'ghassani/surveymonkey-v3-api-php',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->getAccessToken(),
]
]);
}
}