diff --git a/README.md b/README.md index bbb9b02..00a7dea 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ You can follow us on Twitter at [@recastai](https://twitter.com/recastai) for up ## License -Copyright (c) [2017] [Recast.AI](https://recast.ai) +Copyright (c) [2018] [Recast.AI](https://recast.ai) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index 62525fd..9eb405c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "recastai/sdk-php", "description": "The official PHP SDK Recast.AI", - "version": "3.1.1", + "version": "4.0.0", "license": "MIT", "homepage": "https://recast.ai", "authors": [ diff --git a/src/apis/Request/Request.php b/src/apis/Request/Request.php index e20e13b..2aa7c50 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -23,6 +23,7 @@ public function __construct($token = null, $language = null) { public function analyseText($text, $options = []) { $token = array_key_exists('token', $options) ? $options['token'] : $this->token; $language = array_key_exists('language', $options) ? $options['language'] : $this->language; + $proxy = array_key_exists('proxy', $options) ? $options['proxy'] : NULL; if (count($token) < 1) { throw new \Exception('Error: Parameter token is missing'); @@ -40,7 +41,7 @@ public function analyseText($text, $options = []) { $response = $client->request('POST', \RecastAI\Constants::REQUEST_ENDPOINT, [ 'headers' => $headers, 'body' => $body - ]); + ], ['proxy' => $proxy]); } catch (\Exception $e) { throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); } @@ -50,49 +51,27 @@ public function analyseText($text, $options = []) { return new \RecastAI\apis\Resources\Response($responseBody); } - public function analyseFile($file, $options = []) { - $token = array_key_exists('token', $options) ? $options['token'] : $this->token; - $language = array_key_exists('language', $options) ? $options['language'] : $this->language; - - if (count($token) < 1) { - throw new \Exception('Error: Parameter token is missing'); - } - - $headers = ['Authorization' => "Token " . $token]; - - $client = new \GuzzleHttp\Client(); - - try { - $response = $client->request('POST', \RecastAI\Constants::REQUEST_ENDPOINT, [ - 'headers' => $headers, - 'body' => $file - ]); - } catch (\Exception $e) { - throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); - } - - $responseBody = json_decode($response->getBody()->getContents())->results; - - return new \RecastAI\apis\Resources\Response($responseBody); - } - - public function converseText($text, $options = []) { + public function converseText($text, $options = [], $memory = NULL, $log_level = 'info') { $token = array_key_exists('token', $options) ? $options['token'] : $this->token; $language = array_key_exists('language', $options) ? $options['language'] : $this->language; $conversation_token = array_key_exists('conversation_token', $options) ? $options['conversation_token'] : NULL; - $memory = array_key_exists('memory', $options) ? $options['memory'] : NULL; + $proxy = array_key_exists('proxy', $options) ? $options['proxy'] : NULL; if (count($token) < 1) { throw new \Exception('Error: Parameter token is missing'); } $headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $token]; - $body = json_encode([ + $body = [ "text" => $text, "language" => $language, "conversation_token" => $conversation_token, - "memory" => $memory - ]); + "log_level" => $log_level, + ]; + if ($memory) { + $body['memory'] = $memory; + } + $body = json_encode($body); $client = new \GuzzleHttp\Client(); @@ -100,7 +79,7 @@ public function converseText($text, $options = []) { $response = $client->request('POST', \RecastAI\Constants::CONVERSE_ENDPOINT, [ 'headers' => $headers, 'body' => $body - ]); + ], ['proxy' => $proxy]); } catch (\Exception $e) { throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); } diff --git a/src/apis/Resources/Conversation.php b/src/apis/Resources/Conversation.php index 3dcc8f7..c18edf3 100644 --- a/src/apis/Resources/Conversation.php +++ b/src/apis/Resources/Conversation.php @@ -117,7 +117,7 @@ public function isNegative() */ public function isVPositive() { - return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VPOSITIVE); + return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_POSITIVE); } /** @@ -125,7 +125,7 @@ public function isVPositive() */ public function isVNegative() { - return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VNEGATIVE); + return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_NEGATIVE); } /** diff --git a/src/apis/Resources/Response.php b/src/apis/Resources/Response.php index 75789a6..89a7e46 100644 --- a/src/apis/Resources/Response.php +++ b/src/apis/Resources/Response.php @@ -221,7 +221,7 @@ public function isNegative() */ public function isVPositive() { - return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VPOSITIVE); + return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_POSITIVE); } /** @@ -229,6 +229,6 @@ public function isVPositive() */ public function isVNegative() { - return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VNEGATIVE); + return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_NEGATIVE); } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index dbfeadc..621ebde 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -47,7 +47,7 @@ public function testClientClassIfAttributesAreOkay() $this->assertEquals($client->language, $language); } - public function testTextRequestIfAllOkay() + public function testAnalyseTextIfAllOkay() { $callResult = self::jsonResponse(); $res = (Object)[ "body" => ($callResult) ]; @@ -63,49 +63,15 @@ public function testTextRequestIfAllOkay() ->method('requestPrivate') ->will($this->returnValue($res)); $result = json_decode($res->body); - $response = $stub->textRequest($result->results->source); + $response = $stub->request->analyseText($result->results->source); $this->assertEquals('200', $response->status); } - public function testTextRequestIfNoToken() + public function testAnalyseTextIfNoToken() { $client = new Client(); - $res = $client->textRequest('Hello world'); - $this->assertEquals($res, 'Token is missing'); - } - - public function testFileRequestIfAllOkay() - { - $callResult = self::jsonResponse(); - - $file = __DIR__ . '/data/file.wav'; - - if (!file_exists($file)) { - $this->markTestSkipped('Audio test file not found'); - } - - $token = 'TestToken'; - $language = 'en'; - - $stub = $this->getMockBuilder('RecastAI\Client') - ->setConstructorArgs(array($token, $language)) - ->setMethods(['requestFilePrivate']) - ->getMock(); - - $stub->expects($this->once()) - ->method('requestFilePrivate') - ->will($this->returnValue($callResult)); - - $response = $stub->fileRequest($file); - $this->assertEquals('200', $response->status); - } - - public function testFileRequestIfNoToken() - { - $client = new Client(); - - $res = $client->fileRequest(__DIR__ . '/data/file.wav'); + $res = $client->request->analyseText('Hello world'); $this->assertEquals($res, 'Token is missing'); } } diff --git a/tests/ConversationTest.php b/tests/ConversationTest.php index 40a2fbc..a61dee2 100644 --- a/tests/ConversationTest.php +++ b/tests/ConversationTest.php @@ -2,7 +2,7 @@ namespace Tests\RecastAI; -use RecastAI\Conversation; +use RecastAI\apis\Resources\Conversation; class ConversationTest extends \PHPUnit_Framework_TestCase { @@ -13,22 +13,24 @@ protected static function jsonResponse() public function testConversationClassWithAllOkay() { + $token = 'TestToken'; $jsonResult = self::jsonResponse(); $res = (Object)[ "body" => ($jsonResult) ]; - $this->assertInstanceOf('RecastAI\Conversation', new Conversation(($res))); + $this->assertInstanceOf('RecastAI\apis\Resources\Conversation', new Conversation($token, $res)); } public function testConversationClassAttributes() { + $token = 'TestToken'; $jsonResult = self::jsonResponse(); $res = (Object)[ "body" => ($jsonResult) ]; - $conversation = new Conversation(($res)); + $conversation = new Conversation($token, json_decode($res->body)->results); $result = json_decode($res->body); - $this->assertEquals($conversation->conversationToken, $result->results->conversation_token); + $this->assertEquals($conversation->conversation_token, $result->results->conversation_token); $this->assertEquals($conversation->replies, $result->results->replies); $this->assertEquals($conversation->action, $result->results->action); - $this->assertEquals($conversation->nextActions, $result->results->next_actions); + $this->assertEquals($conversation->next_actions, $result->results->next_actions); $this->assertEquals($conversation->memory, $result->results->memory); $this->assertEquals($conversation->language, $result->results->language); $this->assertEquals($conversation->processing_language, $result->results->processing_language); @@ -37,17 +39,18 @@ public function testConversationClassAttributes() public function testResponseClassMethods() { + $token = 'TestToken'; $jsonResult = self::jsonResponse(); $res = (Object)[ "body" => ($jsonResult) ]; $result = json_decode($res->body); - $conversation = new Conversation($res); + $conversation = new Conversation($token, json_decode($res->body)->results); $this->assertEquals($conversation->reply(), $result->results->replies[0]); $this->assertEquals($conversation->joinedReplies(), join(' ', $result->results->replies)); $this->assertEquals($conversation->joinedReplies('\n'), join('\n', $result->results->replies)); - $this->assertEquals($conversation->memory(), $result->results->memory); - $this->assertEquals($conversation->memory('loc'), $result->results->memory->loc); + $this->assertEquals($conversation->getMemory(), $result->results->memory); + $this->assertEquals($conversation->getMemory('loc'), $result->results->memory->loc); $this->assertEquals($conversation->isVPositive(), false); $this->assertEquals($conversation->isPositive(), false); $this->assertEquals($conversation->isNeutral(), true); diff --git a/tests/EntityTest.php b/tests/EntityTest.php index 93db246..3534b83 100644 --- a/tests/EntityTest.php +++ b/tests/EntityTest.php @@ -2,7 +2,7 @@ namespace Tests\RecastAI; -use RecastAI\Entity; +use RecastAI\apis\Resources\Entity; class EntityTest extends \PHPUnit_Framework_TestCase { @@ -21,7 +21,7 @@ public function testEntityClassShouldBeInstanciable() 'raw' => 'asparagus', ]; - $this->assertInstanceOf('RecastAI\Entity', new Entity('ingredient', $data2)); + $this->assertInstanceOf('RecastAI\apis\Resources\Entity', new Entity('ingredient', $data2)); } public function testEntityClassShouldHaveAttributes() diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 4cd532e..15130f1 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -2,7 +2,7 @@ namespace Tests\RecastAI; -use RecastAI\Response; +use RecastAI\apis\Resources\Response; class ResponseTest extends \PHPUnit_Framework_TestCase { @@ -14,17 +14,17 @@ protected static function jsonResponse() public function testResponseClassWithAllOkay() { $jsonResult = self::jsonResponse(); - $res = (Object)[ "body" => ($jsonResult) ]; - $this->assertInstanceOf('RecastAI\Response', new Response($res)); + $res = (Object)[ "body" => json_decode($jsonResult) ]; + $this->assertInstanceOf('RecastAI\apis\Resources\Response', new Response($res->body->results)); } public function testResponseClassAttributes() { $jsonResult = self::jsonResponse(); - $res = (Object)[ "body" => ($jsonResult) ]; - $result = json_decode($res->body); + $res = (Object)[ "body" => json_decode($jsonResult) ]; + $result = $res->body; - $response = new Response($res); + $response = new Response($res->body->results); $count = count($response->entities, COUNT_RECURSIVE); @@ -37,7 +37,7 @@ public function testResponseClassAttributes() $this->assertEquals($response->version, $result->results->{'version'}); $this->assertEquals($response->timestamp, $result->results->{'timestamp'}); $this->assertEquals($count, 4); - $this->assertInstanceOf('RecastAI\Entity', $response->entities[0]); + $this->assertInstanceOf('RecastAI\apis\Resources\Entity', $response->entities[0]); $this->assertInternalType('array', $response->entities); $this->assertInternalType('array', $response->intents); } @@ -45,10 +45,10 @@ public function testResponseClassAttributes() public function testResponseClassMethods() { $jsonResult = self::jsonResponse(); - $res = (Object)[ "body" => ($jsonResult) ]; - $result = json_decode($res->body); + $res = (Object)[ "body" => json_decode($jsonResult) ]; + $result = $res->body; - $response = new Response($res); + $response = new Response($res->body->results); $all = count($response->all('location')); $get = $response->get('location');