Merge branch 'develop'
diff --git a/README.md b/README.md
index aaeb7ea..4bedecc 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@
* Phing (http://www.phing.info/)
* ApiGen (http://apigen.org/)
-Note: This SDK only supports Prediction IO version 0.8 or higher.
+Note: This SDK only supports Prediction IO version 0.8.2 or higher.
Support
=======
@@ -46,7 +46,7 @@
{
"require": {
- "predictionio/predictionio": "~0.8.0"
+ "predictionio/predictionio": "~0.8.2"
}
}
@@ -81,8 +81,8 @@
```PHP
use predictionio\EventClient;
-$app_id = 8;
-$client = new EventClient($app_id, 'http://localhost:7070');
+$accessKey = 'j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O';
+$client = new EventClient($accessKey, 'http://localhost:7070');
```
Set a User Record from Your App
diff --git a/examples/ItemrankImport.php b/examples/ItemrankImport.php
index f9dea16..4b33d75 100644
--- a/examples/ItemrankImport.php
+++ b/examples/ItemrankImport.php
@@ -4,7 +4,7 @@
use predictionio\EventClient;
// check Event Server status
-$client = new EventClient(88);
+$client = new EventClient("j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O");
$response=$client->getStatus();
echo($response);
diff --git a/examples/SampleEvent.php b/examples/SampleEvent.php
index 65138e0..7e91c5a 100644
--- a/examples/SampleEvent.php
+++ b/examples/SampleEvent.php
@@ -6,7 +6,8 @@
try {
// check Event Server status
- $client = new EventClient(25);
+ $client = new EventClient(
+ "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O");
$response=$client->getStatus();
echo($response);
@@ -50,7 +51,6 @@
// create event
$response=$client->createEvent(array(
- 'appId' => 25,
'event' => 'my_event',
'entityType' => 'pio_user',
'entityId' => '8',
@@ -59,7 +59,7 @@
print_r($response);
// get event
- $response=$client->getEvent('event_id_goes_here');
+ $response=$client->getEvent('U_7eotSbeeK0BwshqEfRFAAAAUm-8gOyjP3FR73aBFo');
print_r($response);
} catch (PredictionIOAPIError $e) {
diff --git a/src/predictionio/EventClient.php b/src/predictionio/EventClient.php
index 13918f5..ac7d2eb 100644
--- a/src/predictionio/EventClient.php
+++ b/src/predictionio/EventClient.php
@@ -10,19 +10,22 @@
*/
class EventClient extends BaseClient {
const DATE_TIME_FORMAT = DateTime::ISO8601;
- private $appId;
+ private $accessKey;
+ private $eventUrl;
/**
+ * @param string Access Key
* @param string Base URL to the Event Server. Default is localhost:7070.
* @param float Timeout of the request in seconds. Use 0 to wait indefinitely
* Default is 0.
* @param float Number of seconds to wait while trying to connect to a server.
* Default is 5.
*/
- public function __construct($appId, $baseUrl='http://localhost:7070',
+ public function __construct($accessKey, $baseUrl='http://localhost:7070',
$timeout=0, $connectTimeout=5 ) {
parent::__construct($baseUrl, $timeout, $connectTimeout);
- $this->appId = $appId;
+ $this->accessKey = $accessKey;
+ $this->eventUrl = "/events.json?accessKey=$this->accessKey";
}
private function getEventTime($eventTime) {
@@ -57,12 +60,11 @@
'event' => '$set',
'entityType' => 'pio_user',
'entityId' => $uid,
- 'appId' => $this->appId,
'properties' => $properties,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -87,12 +89,11 @@
'event' => '$unset',
'entityType' => 'pio_user',
'entityId' => $uid,
- 'appId' => $this->appId,
'properties' => $properties,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -114,11 +115,10 @@
'event' => '$delete',
'entityType' => 'pio_user',
'entityId' => $uid,
- 'appId' => $this->appId,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -141,12 +141,11 @@
'event' => '$set',
'entityType' => 'pio_item',
'entityId' => $iid,
- 'appId' => $this->appId,
'properties' => $properties,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -170,12 +169,11 @@
'event' => '$unset',
'entityType' => 'pio_item',
'entityId' => $iid,
- 'appId' => $this->appId,
'properties' => $properties,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -197,11 +195,10 @@
'event' => '$delete',
'entityType' => 'pio_item',
'entityId' => $iid,
- 'appId' => $this->appId,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -230,12 +227,11 @@
'entityId' => $uid,
'targetEntityType' => 'pio_item',
'targetEntityId' => $iid,
- 'appId' => $this->appId,
'properties' => $properties,
'eventTime' => $eventTime,
]);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -248,10 +244,9 @@
* @throws PredictionIOAPIError Request error
*/
public function createEvent(array $data) {
- $data['appId'] = $this->appId;
$json = json_encode($data);
- return $this->sendRequest('POST', '/events.json', $json);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
}
/**
@@ -264,7 +259,8 @@
* @throws PredictionIOAPIError Request error
*/
public function getEvent($eventId) {
- return $this->sendRequest('GET', "/events/$eventId.json", '');
+ return $this->sendRequest('GET',
+ "/events/$eventId.json?accessKey=$this->accessKey", '');
}
}
diff --git a/tests/Unit/EventClientTest.php b/tests/Unit/EventClientTest.php
index eaf72ed..a065ce6 100644
--- a/tests/Unit/EventClientTest.php
+++ b/tests/Unit/EventClientTest.php
@@ -12,7 +12,8 @@
protected $history;
protected function setUp() {
- $this->eventClient=new EventClient(8);
+ $this->eventClient=new EventClient(
+ "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O");
$this->history=new History();
$mock = new Mock([new Response(200)]);
$this->eventClient->client->getEmitter()->attach($this->history);
@@ -27,11 +28,10 @@
$this->assertEquals('$set',$body['event']);
$this->assertEquals('pio_user',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals(20,$body['properties']['age']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testSetUserWithEventTime() {
@@ -53,11 +53,10 @@
$this->assertEquals('$unset',$body['event']);
$this->assertEquals('pio_user',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals(20,$body['properties']['age']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testUnsetUserWithEventTime() {
@@ -86,10 +85,9 @@
$this->assertEquals('$delete',$body['event']);
$this->assertEquals('pio_user',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testDeleteUserWithEventTime() {
@@ -111,11 +109,10 @@
$this->assertEquals('$set',$body['event']);
$this->assertEquals('pio_item',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals('book',$body['properties']['type']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testSetItemWithEventTime() {
@@ -137,11 +134,10 @@
$this->assertEquals('$unset',$body['event']);
$this->assertEquals('pio_item',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals('book',$body['properties']['type']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testUnsetItemWithEventTime() {
@@ -170,10 +166,9 @@
$this->assertEquals('$delete',$body['event']);
$this->assertEquals('pio_item',$body['entityType']);
$this->assertEquals(1,$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testDeleteItemWithEventTime() {
@@ -197,11 +192,10 @@
$this->assertEquals(1,$body['entityId']);
$this->assertEquals('pio_item',$body['targetEntityType']);
$this->assertEquals(888,$body['targetEntityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals(2,$body['properties']['count']);
$this->assertNotNull($body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testRecordActionWithEventTime() {
@@ -217,7 +211,6 @@
public function testCreateEvent() {
$this->eventClient->createEvent(array(
- 'appId' => 8,
'event' => 'my_event',
'entityType' => 'user',
'entityId' => 'uid',
@@ -236,7 +229,6 @@
$this->assertEquals('my_event',$body['event']);
$this->assertEquals('user',$body['entityType']);
$this->assertEquals('uid',$body['entityId']);
- $this->assertEquals(8,$body['appId']);
$this->assertEquals(1,$body['properties']['prop1']);
$this->assertEquals('value2',$body['properties']['prop2']);
$this->assertEquals(array(1,2,3),$body['properties']['prop3']);
@@ -245,23 +237,7 @@
$this->assertEquals(4.56,$body['properties']['prop6']);
$this->assertEquals('2004-12-13T21:39:45.618-07:00',$body['eventTime']);
$this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json',$request->getUrl());
- }
-
- public function testCreateEventUsesAppIdInClient() {
- $this->eventClient->createEvent(array(
- 'appId' => 99,
- 'event' => 'my_event',
- 'entityType' => 'user',
- 'entityId' => 'uid',
- 'properties' => array('prop1'=>1),
- 'eventTime' => '2004-12-13T21:39:45.618-07:00'
- ));
- $request=$this->history->getLastRequest();
- $body=json_decode($request->getBody(), true);
-
- // ignores appId in data and uses the one defined in the event client
- $this->assertEquals(8,$body['appId']);
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUrl());
}
public function testGetEvent() {
@@ -270,7 +246,7 @@
$body=json_decode($request->getBody(), true);
$this->assertEquals('GET',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events/event_id.json',
+ $this->assertEquals('http://localhost:7070/events/event_id.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',
$request->getUrl());
}