This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b81d3f6
commit bff8a48
Showing
5 changed files
with
320 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
# PHP IBM Watson API Bridge | ||
|
||
[![Build Status](https://travis-ci.org/findbrok/php-watson-api-bridge.svg?branch=master)](https://travis-ci.org/findbrok/php-watson-api-bridge) | ||
|
||
A simple PHP wrapper for IBM Watson API | ||
|
||
### Installation | ||
|
||
``` | ||
composer require findbrok/php-watson-api-bridge | ||
``` | ||
|
||
### Usage | ||
|
||
Before using the package checkout [Watson API Explorer](https://watson-api-explorer.mybluemix.net/), | ||
to get a sense of what you can and cannot do with Watson | ||
|
||
```php | ||
require 'vendor/autoload.php' | ||
|
||
use FindBrok\WatsonBridge\Bridge; | ||
|
||
//Create a new bridge Object | ||
$bridge = new Bridge('username', 'password', 'baseUrl'); | ||
|
||
//Simple get request | ||
$queryParams = ['foo' => 'bar']; | ||
$response = $bridge->get('uri', $queryParams); | ||
|
||
//Simple post request | ||
$dataToPost = ['foo' => 'bar']; | ||
$response = $bridge->post('uri', $dataToPost, 'json'); | ||
``` | ||
|
||
The Package uses [Guzzle](http://docs.guzzlephp.org/en/latest/testing.html) to perform requests, | ||
all your responses will be instances of ```GuzzleHttp\Psr7\Response``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace FindBrok\WatsonBridge\Exceptions; | ||
|
||
use Exception; | ||
use RuntimeException; | ||
|
||
/** | ||
* Class WatsonBridgeException | ||
* | ||
* @package FindBrok\WatsonBridge\Exceptions | ||
*/ | ||
class WatsonBridgeException extends RuntimeException | ||
{ | ||
/** | ||
* Default error message | ||
* | ||
* @var string | ||
*/ | ||
protected $message = 'An error occurred while performing request to Watson'; | ||
|
||
/** | ||
* Create a new instance of WatsonBridgeException | ||
* | ||
* @param string $message | ||
* @param int $code | ||
* @param Exception|null $previous | ||
*/ | ||
public function __construct($message = '', $code = 400, Exception $previous = null) | ||
{ | ||
//Format message | ||
$message = 'Watson Bridge: '.(($message != '') ? $message : $this->message); | ||
//Call parent exception | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,74 @@ | ||
<?php | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
/** | ||
* Class TestCase | ||
*/ | ||
class TestCase extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Watson bridge | ||
* | ||
* @var | ||
*/ | ||
protected $bridge; | ||
|
||
/** | ||
* Setup test | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->bridge = $this->getMockBuilder('FindBrok\WatsonBridge\Bridge') | ||
->disableOriginalConstructor() | ||
->setMethods(['getClient']) | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* Test a Successful Get request | ||
*/ | ||
public function testGetRequestResponseOk() | ||
{ | ||
// Create a mock and queue one response | ||
$mock = new MockHandler([ | ||
new Response(200, ['X-Foo' => 'Bar']), | ||
]); | ||
$handler = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handler]); | ||
|
||
$this->bridge->method('getClient')->willReturn($client); | ||
|
||
$this->assertEquals(200, $this->bridge->get('version/watson-api-method', ['foo' => 'bar'])->getStatusCode()); | ||
} | ||
|
||
/** | ||
* Test a Get request which fails | ||
* | ||
* @expectedException \FindBrok\WatsonBridge\Exceptions\WatsonBridgeException | ||
*/ | ||
public function testGetRequestWithException() | ||
{ | ||
// Create a mock and queue one response | ||
$mock = new MockHandler([ | ||
new ClientException( | ||
'Watson Error', | ||
new Request('GET', 'version/watson-api-method'), | ||
new Response(400, ['X-Foo' => 'Bar'], collect(['error_code' => 400, 'error_message' => 'Watson Error'])->toJson()) | ||
) | ||
]); | ||
$handler = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handler]); | ||
|
||
$this->bridge->method('getClient')->willReturn($client); | ||
|
||
$this->bridge->get('version/watson-api-method', ['foo' => 'bar']); | ||
|
||
$this->setExpectedException('\FindBrok\WatsonBridge\Exceptions\WatsonBridgeException'); | ||
} | ||
} |