Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from Millasdur/update/SDK-PHP
Browse files Browse the repository at this point in the history
Updated PHP SDK
  • Loading branch information
hlely authored Aug 10, 2018
2 parents 40f2661 + 589ec33 commit 7b15327
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 97 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
45 changes: 12 additions & 33 deletions src/apis/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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());
}
Expand All @@ -50,57 +51,35 @@ 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();

try {
$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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/apis/Resources/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ public function isNegative()
*/
public function isVPositive()
{
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VPOSITIVE);
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_POSITIVE);
}

/**
* @return bool
*/
public function isVNegative()
{
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VNEGATIVE);
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_NEGATIVE);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/apis/Resources/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ public function isNegative()
*/
public function isVPositive()
{
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VPOSITIVE);
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_POSITIVE);
}

/**
* @return bool
*/
public function isVNegative()
{
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VNEGATIVE);
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VERY_NEGATIVE);
}
}
42 changes: 4 additions & 38 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ];
Expand All @@ -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');
}
}
19 changes: 11 additions & 8 deletions tests/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\RecastAI;

use RecastAI\Conversation;
use RecastAI\apis\Resources\Conversation;

class ConversationTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\RecastAI;

use RecastAI\Entity;
use RecastAI\apis\Resources\Entity;

class EntityTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\RecastAI;

use RecastAI\Response;
use RecastAI\apis\Resources\Response;

class ResponseTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -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);

Expand All @@ -37,18 +37,18 @@ 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);
}

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');
Expand Down

0 comments on commit 7b15327

Please sign in to comment.