-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuzzleCommunicationService.php
More file actions
224 lines (177 loc) · 4.53 KB
/
Copy pathGuzzleCommunicationService.php
File metadata and controls
224 lines (177 loc) · 4.53 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace biologis\JIRA_PHP_API;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
/**
* Class GuzzleCommunicationService
* @package biologis\JIRA_PHP_API
*
* Implementation of ICommunicationService using guzzle.
*/
class GuzzleCommunicationService implements ICommunicationService {
/**
* Request timeout.
* @var float
*/
private $timeout;
/**
* URL to JIRA REST servive, e.g. https://jira.yourdomain.com/rest/api/2/
* @var string
*/
private $jiraURL;
/**
* User credentials username and password stored in an array.
* @var array
*/
private $jiraCredentials;
/**
* Reference to guzzle client.
* @var \GuzzleHttp\Client
*/
private $guzzleHTTPClient;
/**
* GuzzleCommunicationService constructor.
* @param string $jiraURL URL to JIRA REST servive
* @param array $jiraCredentials jira username and password
*/
function __construct($jiraURL, $jiraCredentials) {
$this->jiraURL = $jiraURL;
$this->jiraCredentials = $jiraCredentials;
$this->timeout = 10.0;
$this->guzzleHTTPClient = new Client([
'base_uri' => $this->jiraURL,
'timeout' => $this->timeout,
]);
}
/**
* @return float
*/
public function getTimeout() {
return $this->timeout;
}
/**
* @return string
*/
public function getJiraURL() {
return $this->jiraURL;
}
/**
* @return Client
*/
public function getGuzzleHTTPClient() {
return $this->guzzleHTTPClient;
}
/**
* @return array
*/
public function getJiraCredentials() {
return $this->jiraCredentials;
}
/**
* @param array $credentials array with username and password
*/
public function setJiraCredentials($credentials = array()) {
$this->jiraCredentials = $credentials;
}
/**
* @param string $path
* @param \stdClass $data
* @return bool|\stdClass
*/
public function put($path, \stdClass $data) {
// serialize data
$data_json = json_encode($data);
$options = array(
'body' => $data_json,
'headers' => array(
'Content-type' => 'application/json',
)
);
$this->addCredentials($options);
try {
$response = $this->guzzleHTTPClient->request('PUT', $path, $options);
if ($response->getStatusCode() == 204) {
return new \stdClass();
}
else {
return false;
}
} catch(\Exception $e) {
return false;
}
}
/**
* @param string $path
* @param \stdClass $data
* @param int $expectedStatusCode
* @return bool|mixed
*/
public function post($path, \stdClass $data, $expectedStatusCode = 200) {
// serialize data
$data_json = json_encode($data);
$options = array(
'body' => $data_json,
'headers' => array(
'Content-type' => 'application/json',
)
);
$this->addCredentials($options);
try {
$response = $this->guzzleHTTPClient->request('POST', $path, $options);
// TODO status 201 has to be moved to the appropriate calls of post as parameter
if ($response->getStatusCode() == 201 || $response->getStatusCode() == $expectedStatusCode) {
$response_content = json_decode($response->getBody()->getContents());
return $response_content;
}
else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* @param string $path
* @param array $parameters
* @return bool
*/
public function delete($path, $parameters = array()) {
// TODO: Implement delete() method.
return false;
}
/**
* @param \biologis\JIRA_PHP_API\relative $path
* @param array $parameters
* @return bool|mixed
*/
public function get($path, $parameters = array()) {
$query_parameters= http_build_query(array_filter($parameters));
$relative_path = (!empty($query_parameters)) ? $path . '?' . $query_parameters : $path;
$options = array();
$this->addCredentials($options);
try {
$response = $this->guzzleHTTPClient->request('GET', $relative_path, $options);
if ($response->getStatusCode() == 200) {
$response_content = json_decode($response->getBody()->getContents());
return $response_content;
}
else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* Add basic auth credentials to options array.
* @param $options
*/
private function addCredentials(&$options) {
$options += array(
'auth' => array(
$this->jiraCredentials['username'],
$this->jiraCredentials['password']
)
);
}
}