@@ -78,15 +78,15 @@ class Bridge
78
78
*/
79
79
public function __construct ($ username , $ password , $ endpoint )
80
80
{
81
- //Set Username, Password and Endpoint
81
+ // Set Username, Password and Endpoint.
82
82
$ this ->username = $ username ;
83
83
$ this ->password = $ password ;
84
84
$ this ->endpoint = $ endpoint ;
85
85
86
- //Set HttpClient
86
+ // Set HttpClient.
87
87
$ this ->setClient ($ endpoint );
88
88
89
- //Set Token
89
+ // Set Token.
90
90
$ this ->token = new Token ($ this ->username );
91
91
}
92
92
@@ -99,12 +99,13 @@ public function __construct($username, $password, $endpoint)
99
99
*/
100
100
public function appendHeaders ($ headers = [])
101
101
{
102
- //We have some headers to append
102
+ // We have some headers to append.
103
103
if (! empty ($ headers )) {
104
- //Append headers
104
+ // Append headers.
105
105
$ this ->headers = collect ($ this ->headers )->merge ($ headers )->all ();
106
106
}
107
- //Return calling object
107
+
108
+ // Return calling object.
108
109
return $ this ;
109
110
}
110
111
@@ -117,7 +118,7 @@ public function appendHeaders($headers = [])
117
118
*/
118
119
public function cleanOptions ($ options = [])
119
120
{
120
- //If item is null or empty we will remove them
121
+ // If item is null or empty we will remove them.
121
122
return collect ($ options )->reject (function ($ item ) {
122
123
return empty ($ item ) || is_null ($ item );
123
124
})->all ();
@@ -144,7 +145,7 @@ public function clearThrottle()
144
145
*/
145
146
public function delete ($ uri , $ data , $ type = 'json ' )
146
147
{
147
- //Make a Delete and return response
148
+ // Make a Delete and return response.
148
149
return $ this ->send ('DELETE ' , $ uri , $ data , $ type );
149
150
}
150
151
@@ -157,13 +158,11 @@ public function delete($uri, $data, $type = 'json')
157
158
*/
158
159
public function failedRequest (Response $ response )
159
160
{
160
- //Decode Response
161
+ // Decode Response.
161
162
$ decodedResponse = json_decode ($ response ->getBody ()->getContents (), true );
162
- //Get error message
163
- $ errorMessage = (isset ($ decodedResponse ['error_message ' ]) && ! is_null ($ decodedResponse ['error_message ' ])) ?
164
- $ decodedResponse ['error_message ' ] :
165
- $ response ->getReasonPhrase ();
166
- //ClientException
163
+ // Get error message.
164
+ $ errorMessage = (isset ($ decodedResponse ['error_message ' ]) && ! is_null ($ decodedResponse ['error_message ' ])) ? $ decodedResponse ['error_message ' ] : $ response ->getReasonPhrase ();
165
+ // ClientException.
167
166
throw new WatsonBridgeException ($ errorMessage , $ response ->getStatusCode ());
168
167
}
169
168
@@ -176,21 +175,21 @@ public function failedRequest(Response $response)
176
175
*/
177
176
public function fetchToken ($ incrementThrottle = false )
178
177
{
179
- //Increment throttle if needed
178
+ // Increment throttle if needed.
180
179
if ($ incrementThrottle ) {
181
180
$ this ->incrementThrottle ();
182
181
}
183
- //Reset Client
182
+ // Reset Client.
184
183
$ this ->setClient ($ this ->getAuthorizationEndpoint ());
185
- //Get the token response
184
+ // Get the token response.
186
185
$ response = $ this ->get ('v1/token ' , [
187
186
'url ' => $ this ->endpoint ,
188
187
]);
189
- //Extract
188
+ // Extract.
190
189
$ token = json_decode ($ response ->getBody ()->getContents (), true );
191
- //Reset client
190
+ // Reset client.
192
191
$ this ->setClient ($ this ->endpoint );
193
- //Update token
192
+ // Update token.
194
193
$ this ->token ->updateToken ($ token ['token ' ]);
195
194
}
196
195
@@ -204,7 +203,7 @@ public function fetchToken($incrementThrottle = false)
204
203
*/
205
204
public function get ($ uri = '' , $ query = [])
206
205
{
207
- //Make a Post and return response
206
+ // Make a Post and return response.
208
207
return $ this ->send ('GET ' , $ uri , $ query , 'query ' );
209
208
}
210
209
@@ -215,7 +214,7 @@ public function get($uri = '', $query = [])
215
214
*/
216
215
public function getAuth ()
217
216
{
218
- //Return access authorization
217
+ // Return access authorization.
219
218
return [$ this ->username , $ this ->password ];
220
219
}
221
220
@@ -226,10 +225,11 @@ public function getAuth()
226
225
*/
227
226
public function getAuthorizationEndpoint ()
228
227
{
229
- //Parse the endpoint
228
+ // Parse the endpoint.
230
229
$ parsedEndpoint = collect (parse_url ($ this ->endpoint ));
231
- //Return auth url
232
- return $ parsedEndpoint ->get ('scheme ' ) . ':// ' . $ parsedEndpoint ->get ('host ' ) . '/authorization/api/ ' ;
230
+
231
+ // Return auth url.
232
+ return $ parsedEndpoint ->get ('scheme ' ).':// ' .$ parsedEndpoint ->get ('host ' ).'/authorization/api/ ' ;
233
233
}
234
234
235
235
/**
@@ -239,7 +239,7 @@ public function getAuthorizationEndpoint()
239
239
*/
240
240
public function getClient ()
241
241
{
242
- //Return client
242
+ // Return client.
243
243
return $ this ->client ;
244
244
}
245
245
@@ -250,7 +250,7 @@ public function getClient()
250
250
*/
251
251
public function getHeaders ()
252
252
{
253
- //Return headers
253
+ // Return headers.
254
254
return $ this ->headers ;
255
255
}
256
256
@@ -263,23 +263,20 @@ public function getHeaders()
263
263
*/
264
264
public function getRequestOptions ($ initial = [])
265
265
{
266
- //Define options
266
+ // Define options.
267
267
$ options = collect ($ initial );
268
- //Define an auth option
268
+
269
+ // Define an auth option.
269
270
if ($ this ->authMethod == 'credentials ' ) {
270
- $ options = $ options ->merge ([
271
- 'auth ' => $ this ->getAuth (),
272
- ]);
271
+ $ options = $ options ->merge (['auth ' => $ this ->getAuth ()]);
273
272
} elseif ($ this ->authMethod == 'token ' ) {
274
- $ this ->appendHeaders ([
275
- 'X-Watson-Authorization-Token ' => $ this ->getToken (),
276
- ]);
273
+ $ this ->appendHeaders (['X-Watson-Authorization-Token ' => $ this ->getToken ()]);
277
274
}
278
- //Put Headers in options
279
- $ options = $ options-> merge ([
280
- 'headers ' => $ this ->getHeaders (),
281
- ]);
282
- //Clean and return
275
+
276
+ // Put Headers in options.
277
+ $ options = $ options -> merge ([ 'headers ' => $ this ->getHeaders (),]);
278
+
279
+ // Clean and return.
283
280
return $ this ->cleanOptions ($ options ->all ());
284
281
}
285
282
@@ -290,13 +287,13 @@ public function getRequestOptions($initial = [])
290
287
*/
291
288
public function getToken ()
292
289
{
293
- //Token is not valid
290
+ // Token is not valid.
294
291
if (! $ this ->token ->isValid ()) {
295
- //Fetch from Watson
292
+ // Fetch from Watson.
296
293
$ this ->fetchToken ();
297
294
}
298
295
299
- //Return token
296
+ // Return token.
300
297
return $ this ->token ->getToken ();
301
298
}
302
299
@@ -331,7 +328,7 @@ public function isThrottledReached()
331
328
*/
332
329
public function patch ($ uri , $ data , $ type = 'json ' )
333
330
{
334
- //Make a Patch and return response
331
+ // Make a Patch and return response.
335
332
return $ this ->send ('PATCH ' , $ uri , $ data , $ type );
336
333
}
337
334
@@ -346,7 +343,7 @@ public function patch($uri, $data, $type = 'json')
346
343
*/
347
344
public function post ($ uri , $ data , $ type = 'json ' )
348
345
{
349
- //Make a Post and return response
346
+ // Make a Post and return response.
350
347
return $ this ->send ('POST ' , $ uri , $ data , $ type );
351
348
}
352
349
@@ -361,7 +358,7 @@ public function post($uri, $data, $type = 'json')
361
358
*/
362
359
public function put ($ uri , $ data , $ type = 'json ' )
363
360
{
364
- //Make a Put and return response
361
+ // Make a Put and return response.
365
362
return $ this ->send ('PUT ' , $ uri , $ data , $ type );
366
363
}
367
364
@@ -377,19 +374,20 @@ public function put($uri, $data, $type = 'json')
377
374
public function request ($ method = 'GET ' , $ uri = '' , $ options = [])
378
375
{
379
376
try {
380
- //Make the request
377
+ // Make the request.
381
378
return $ this ->getClient ()->request ($ method , $ uri , $ this ->getRequestOptions ($ options ));
382
379
} catch (ClientException $ e ) {
383
- //We are using token auth and probably token expired
380
+ // We are using token auth and probably token expired.
384
381
if ($ this ->authMethod == 'token ' && $ e ->getCode () == 401 && ! $ this ->isThrottledReached ()) {
385
- //Try refresh token
382
+ // Try refresh token.
386
383
$ this ->fetchToken (true );
387
- //Try requesting again
384
+
385
+ // Try requesting again.
388
386
return $ this ->request ($ method , $ uri , $ options );
389
387
}
390
- //Clear throttle for this request
388
+ // Clear throttle for this request.
391
389
$ this ->clearThrottle ();
392
- //Call Failed Request
390
+ // Call Failed Request.
393
391
$ this ->failedRequest ($ e ->getResponse ());
394
392
}
395
393
}
@@ -406,14 +404,15 @@ public function request($method = 'GET', $uri = '', $options = [])
406
404
*/
407
405
private function send ($ method , $ uri , $ data , $ type = 'json ' )
408
406
{
409
- //Make the Request to Watson
407
+ // Make the Request to Watson.
410
408
$ response = $ this ->request ($ method , $ uri , [$ type => $ data ]);
411
- //Request Failed
409
+ // Request Failed.
412
410
if ($ response ->getStatusCode () != 200 ) {
413
- //Throw Watson Bridge Exception
411
+ // Throw Watson Bridge Exception.
414
412
$ this ->failedRequest ($ response );
415
413
}
416
- //We return response
414
+
415
+ // We return response.
417
416
return $ response ;
418
417
}
419
418
@@ -426,10 +425,10 @@ private function send($method, $uri, $data, $type = 'json')
426
425
*/
427
426
public function setClient ($ endpoint = null )
428
427
{
429
- //Create client using API endpoint
428
+ // Create client using API endpoint.
430
429
$ this ->client = new Client ([
431
- 'base_uri ' => ! is_null ($ endpoint ) ? $ endpoint : $ this ->endpoint ,
432
- ]);
430
+ 'base_uri ' => ! is_null ($ endpoint ) ? $ endpoint : $ this ->endpoint
431
+ ]);
433
432
}
434
433
435
434
/**
@@ -441,9 +440,10 @@ public function setClient($endpoint = null)
441
440
*/
442
441
public function useAuthMethodAs ($ method = 'credentials ' )
443
442
{
444
- //Change auth method
443
+ // Change auth method.
445
444
$ this ->authMethod = $ method ;
446
- //Return object
445
+
446
+ // Return object.
447
447
return $ this ;
448
448
}
449
449
}
0 commit comments