Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Gorse.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function deleteFeedback(string $feedback_type, string $user_id, string $item_id)
}

/**
* Get recommendation with scores for a user.
* Uses X-API-Version: 2 header to return scores.
* @throws GuzzleException
*/
function getRecommend(string $user_id, ?string $write_back_type = null, ?string $write_back_delay = null, int $n = 10, int $offset = 0): array
Expand All @@ -130,7 +132,12 @@ function getRecommend(string $user_id, ?string $write_back_type = null, ?string
if ($write_back_type) $params['write-back-type'] = $write_back_type;
if ($write_back_delay) $params['write-back-delay'] = $write_back_delay;

return $this->request('GET', '/api/recommend/' . urlencode($user_id), null, $params);
$scores = [];
$response = $this->requestWithHeaders('GET', '/api/recommend/' . urlencode($user_id), null, $params, ['X-API-Version' => '2']);
foreach ($response as $score) {
$scores[] = Score::fromJSON($score);
}
return $scores;
}

/**
Expand Down Expand Up @@ -217,7 +224,15 @@ function getLatest(?string $user_id = null, int $n = 10, int $offset = 0): array
*/
private function request(string $method, string $uri, $body, array $query = [])
{
$options = [RequestOptions::HEADERS => ['X-API-Key' => $this->apiKey]];
return $this->requestWithHeaders($method, $uri, $body, $query, []);
}

/**
* @throws GuzzleException
*/
private function requestWithHeaders(string $method, string $uri, $body, array $query = [], array $headers = [])
{
$options = [RequestOptions::HEADERS => array_merge(['X-API-Key' => $this->apiKey], $headers)];
if ($body != null) {
$options[RequestOptions::JSON] = $body;
}
Expand All @@ -227,4 +242,4 @@ private function request(string $method, string $uri, $body, array $query = [])
$response = $this->client->request($method, $uri, $options);
return json_decode($response->getBody());
}
}
}
21 changes: 14 additions & 7 deletions test/GorseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Gorse\Model\Feedback;
use Gorse\Model\Item;
use Gorse\Model\User;
use Gorse\Model\Score;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -86,20 +87,26 @@ public function testRecommend()
$client = new Gorse(self::ENDPOINT, self::API_KEY);
$client->insertUser(new User("3000", array(), ""));
$items = $client->getRecommend('3000', null, null, 3);
$this->assertIsArray($items);

// Check IDs
$this->assertCount(3, $items);
$this->assertEquals('315', $items[0]->id);
$this->assertEquals('1432', $items[1]->id);
$this->assertEquals('918', $items[2]->id);
}

/**
* @throws GuzzleException
*/
public function testNonPersonalized()
public function testLatest()
{
$client = new Gorse(self::ENDPOINT, self::API_KEY);
// Test getLatest which returns Score[]
$items = $client->getLatest('3000', 3);
$this->assertIsArray($items);
foreach ($items as $item) {
$this->assertInstanceOf(\Gorse\Model\Score::class, $item);
}

// Check IDs
$this->assertCount(3, $items);
$this->assertEquals('315', $items[0]->id);
$this->assertEquals('1432', $items[1]->id);
$this->assertEquals('918', $items[2]->id);
}
}
Loading