3
3
namespace Slakbal \Citrix ;
4
4
5
5
use GuzzleHttp \Client as HttpClient ;
6
- use GuzzleHttp \Exception \ClientException ;
7
- use GuzzleHttp \Exception \RequestException ;
8
6
use Illuminate \Support \Facades \Cache ;
7
+ use Illuminate \Support \Facades \Log ;
9
8
10
9
/**
11
10
* Provides common functionality for Citrix classes
@@ -35,6 +34,8 @@ abstract class CitrixAbstract
35
34
36
35
private $ url ;
37
36
37
+ private $ httpResponse ;
38
+
38
39
private $ response ;
39
40
40
41
private $ status ;
@@ -70,20 +71,22 @@ public function __construct($authType)
70
71
}
71
72
72
73
73
- private function directAuthentication ()
74
+ public function hasAccessObject ()
74
75
{
75
- $ directAuth = new DirectAuthenticate ();
76
+ if (Cache::has ('citrix_access_object ' )) {
77
+ return true ;
78
+ }
76
79
77
- $ this ->authObject = $ directAuth ->authenticate ();
78
- $ this ->rememberAccessObject ($ this ->authObject );
80
+ return false ;
79
81
}
80
82
81
83
82
- private function oauth2Authentication ()
84
+ private function directAuthentication ()
83
85
{
84
- //to be implemented
85
- $ this ->authObject = null ;
86
- Cache::forget ('citrix_access_object ' );
86
+ $ directAuth = new DirectAuthenticate ();
87
+
88
+ $ this ->authObject = $ directAuth ->authenticate ();
89
+ $ this ->rememberAccessObject ($ this ->authObject );
87
90
}
88
91
89
92
@@ -93,13 +96,11 @@ public function rememberAccessObject($authObject)
93
96
}
94
97
95
98
96
- public function hasAccessObject ()
99
+ private function oauth2Authentication ()
97
100
{
98
- if (Cache::has ('citrix_access_object ' )) {
99
- return true ;
100
- }
101
-
102
- return false ;
101
+ //to be implemented
102
+ $ this ->authObject = null ;
103
+ Cache::forget ('citrix_access_object ' );
103
104
}
104
105
105
106
@@ -109,12 +110,6 @@ public function getOrganizerKey()
109
110
}
110
111
111
112
112
- public function getAccessToken ()
113
- {
114
- return $ this ->authObject ->access_token ;
115
- }
116
-
117
-
118
113
public function getAccountKey ()
119
114
{
120
115
return $ this ->authObject ->account_key ;
@@ -133,26 +128,12 @@ public function getAuthObject()
133
128
}
134
129
135
130
136
- public function getParams ()
137
- {
138
- return $ this ->params ;
139
- }
140
-
141
-
142
131
public function getStatus ()
143
132
{
144
133
return $ this ->status ;
145
134
}
146
135
147
136
148
- public function setParams ($ params )
149
- {
150
- $ this ->params = $ params ;
151
-
152
- return $ this ;
153
- }
154
-
155
-
156
137
public function addParam ($ key , $ value )
157
138
{
158
139
$ this ->params [ $ key ] = $ value ;
@@ -161,32 +142,12 @@ public function addParam($key, $value)
161
142
}
162
143
163
144
164
- public function getUrl ()
165
- {
166
- return $ this ->url ;
167
- }
168
-
169
-
170
- public function setUrl ($ url )
171
- {
172
- $ this ->url = $ url ;
173
-
174
- return $ this ;
175
- }
176
-
177
-
178
145
public function getResponse ()
179
146
{
180
147
return $ this ->response ;
181
148
}
182
149
183
150
184
- public function getResponseCollection ()
185
- {
186
- return collect ($ this ->response );
187
- }
188
-
189
-
190
151
public function setResponse ($ response )
191
152
{
192
153
if (is_object ($ response )) {
@@ -203,17 +164,9 @@ public function setResponse($response)
203
164
}
204
165
205
166
206
- public function getHttpMethod ()
207
- {
208
- return $ this ->httpMethod ;
209
- }
210
-
211
-
212
- public function setHttpMethod ($ httpMethod )
167
+ public function getResponseCollection ()
213
168
{
214
- $ this ->httpMethod = strtoupper ($ httpMethod );
215
-
216
- return $ this ;
169
+ return collect ($ this ->response );
217
170
}
218
171
219
172
@@ -237,7 +190,7 @@ public function sendRequest()
237
190
238
191
case 'GET ' :
239
192
240
- $ response = $ this ->http_client ->get ($ this ->getUrl (), [
193
+ $ this -> httpResponse = $ this ->http_client ->get ($ this ->getUrl (), [
241
194
'headers ' => [
242
195
'Content-Type ' => 'application/json; charset=utf-8 ' ,
243
196
'Accept ' => 'application/json ' ,
@@ -249,7 +202,7 @@ public function sendRequest()
249
202
250
203
case 'POST ' :
251
204
252
- $ response = $ this ->http_client ->post ($ this ->getUrl (), [
205
+ $ this -> httpResponse = $ this ->http_client ->post ($ this ->getUrl (), [
253
206
'headers ' => [
254
207
'Content-Type ' => 'application/json; charset=utf-8 ' ,
255
208
'Accept ' => 'application/json ' ,
@@ -259,9 +212,9 @@ public function sendRequest()
259
212
]);
260
213
break ;
261
214
262
- case 'DELETE ' :
215
+ case 'PUT ' :
263
216
264
- $ response = $ this ->http_client ->delete ($ this ->getUrl (), [
217
+ $ this -> httpResponse = $ this ->http_client ->put ($ this ->getUrl (), [
265
218
'headers ' => [
266
219
'Content-Type ' => 'application/json; charset=utf-8 ' ,
267
220
'Accept ' => 'application/json ' ,
@@ -271,38 +224,95 @@ public function sendRequest()
271
224
]);
272
225
break ;
273
226
227
+ case 'DELETE ' :
228
+
229
+ $ this ->httpResponse = $ this ->http_client ->delete ($ this ->getUrl (), [
230
+ 'headers ' => [
231
+ 'Content-Type ' => 'application/json; charset=utf-8 ' ,
232
+ 'Accept ' => 'application/json ' ,
233
+ 'Authorization ' => 'OAuth oauth_token= ' . $ this ->getAccessToken (),
234
+ ],
235
+ ]);
236
+ break ;
237
+
274
238
default :
275
239
276
240
break ;
277
241
}
242
+ } catch (\Exception $ e ) {
278
243
279
- } catch (ClientException $ e ) {
280
-
281
- $ this ->response = [];
244
+ Log::error ('CITRIX - ' . $ e ->getMessage ());
245
+ $ this ->response = [
246
+ 'error ' => true ,
247
+ 'message ' => $ e ->getMessage ()
248
+ ];
282
249
283
250
return $ this ;
284
251
285
- } catch ( RequestException $ e ) {
252
+ }
286
253
287
- $ this ->response = [];
254
+ //if no error carry on to build the response
255
+ $ this ->response = [
256
+ 'error ' => false ,
257
+ 'status ' => $ this ->httpResponse ->getStatusCode (),
258
+ 'body ' => $ this ->parseBody ($ this ->httpResponse ->getBody ())
259
+ ];
288
260
289
- return $ this ;
261
+ return $ this ;
262
+ }
290
263
291
- } catch (\Exception $ e ) {
292
264
293
- $ this ->response = [];
265
+ public function getHttpMethod ()
266
+ {
267
+ return $ this ->httpMethod ;
268
+ }
294
269
295
- return $ this ;
296
- }
297
270
298
- //if no error carry on to process the response
271
+ public function setHttpMethod ($ httpMethod )
272
+ {
273
+ $ this ->httpMethod = strtoupper ($ httpMethod );
274
+
275
+ return $ this ;
276
+ }
277
+
278
+
279
+ public function getUrl ()
280
+ {
281
+ return $ this ->url ;
282
+ }
283
+
284
+
285
+ public function setUrl ($ url )
286
+ {
287
+ $ this ->url = $ url ;
288
+
289
+ return $ this ;
290
+ }
291
+
292
+
293
+ public function getAccessToken ()
294
+ {
295
+ return $ this ->authObject ->access_token ;
296
+ }
297
+
298
+
299
+ public function getParams ()
300
+ {
301
+ return $ this ->params ;
302
+ }
299
303
300
- $ this ->response = $ response ->getBody ();
301
- //dd( (string)$this->response );
302
304
303
- $ this ->response = json_decode ($ this ->response , false , 512 , JSON_BIGINT_AS_STRING );
305
+ public function setParams ($ params )
306
+ {
307
+ $ this ->params = $ params ;
304
308
305
309
return $ this ;
306
310
}
307
311
312
+
313
+ public function parseBody ($ body )
314
+ {
315
+ return json_decode ($ body , false , 512 , JSON_BIGINT_AS_STRING );
316
+ }
317
+
308
318
}
0 commit comments