SDKPHP-5 Adapted new 0.5 API
diff --git a/examples/SampleImport.php b/examples/SampleImport.php
index 585d958..dc1e48f 100644
--- a/examples/SampleImport.php
+++ b/examples/SampleImport.php
@@ -32,7 +32,8 @@
$tuple = explode("\t", $line);
if (count($tuple) == 3) {
try {
- $client->execute($client->getCommand("user_rate_item", array("uid" => $tuple[0], "iid" => $tuple[1], "rate" => intval($tuple[2]))));
+ $client->identify($tuple[0]);
+ $client->execute($client->getCommand("record_action_on_item", array("pio_action" => "rate", "pio_iid" => $tuple[1], "pio_rate" => intval($tuple[2]))));
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
print $e->getResponse()->getBody()."\n\n";
}
diff --git a/examples/SampleTasks.php b/examples/SampleTasks.php
index ef5d49b..c5f7218 100644
--- a/examples/SampleTasks.php
+++ b/examples/SampleTasks.php
@@ -20,17 +20,17 @@
$uid = "foobar";
- $command = $client->getCommand('create_user', array('uid' => $uid));
+ $command = $client->getCommand('create_user', array('pio_uid' => $uid));
$command->setInactive("true");
$command->set("gender", "F");
$response =$client->execute($command);
print_r($response);
- $command = $client->getCommand('get_user', array('uid' => $uid));
+ $command = $client->getCommand('get_user', array('pio_uid' => $uid));
$response = $client->execute($command);
print_r($response);
- $command = $client->getCommand('delete_user', array('uid' => $uid));
+ $command = $client->getCommand('delete_user', array('pio_uid' => $uid));
$response =$client->execute($command);
print_r($response);
@@ -41,18 +41,18 @@
$iid = "barbaz";
- $command = $client->getCommand('create_item', array('iid' => $iid));
+ $command = $client->getCommand('create_item', array('pio_iid' => $iid));
$command->setItypes(array("dead", "beef"));
$command->setPrice(9.99);
$command->set("weight", "10");
$response = $client->execute($command);
print_r($response);
- $command = $client->getCommand('get_item', array('iid' => $iid));
+ $command = $client->getCommand('get_item', array('pio_iid' => $iid));
$response = $client->execute($command);
print_r($response);
- $command = $client->getCommand('delete_item', array('iid' => $iid));
+ $command = $client->getCommand('delete_item', array('pio_iid' => $iid));
$response = $client->execute($command);
print_r($response);
@@ -61,7 +61,8 @@
// Retrieving Top N Recommendations for a User
print "Get top 10 recommendations for a user:\n";
- $response = $client->execute($client->getCommand("itemrec_get_top_n", array("engine" => "movies", "uid" => "1", "n" => 10)));
+ $client->identify("1");
+ $response = $client->execute($client->getCommand("itemrec_get_top_n", array("pio_engine" => "movies", "pio_n" => 10)));
print_r($response);
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
print $e->getResponse()->getBody()."\n\n";
diff --git a/src/PredictionIO/Command/CreateItem.php b/src/PredictionIO/Command/CreateItem.php
index 92a5b18..2dc8ce3 100644
--- a/src/PredictionIO/Command/CreateItem.php
+++ b/src/PredictionIO/Command/CreateItem.php
@@ -36,7 +36,7 @@
*/
public function setIid($iid)
{
- return $this->iid = $iid;
+ return $this->set("pio_iid", $iid);
}
/**
@@ -51,9 +51,9 @@
public function setItypes($itypes)
{
if (is_array($itypes)) {
- return $this->set("itypes", implode(",", $itypes));
+ return $this->set("pio_itypes", implode(",", $itypes));
} else {
- return $this->set("itypes", $itypes);
+ return $this->set("pio_itypes", $itypes);
}
}
@@ -66,7 +66,7 @@
*/
public function setStartT($startT)
{
- return $this->set("startT", $startT);
+ return $this->set("pio_startT", $startT);
}
/**
@@ -78,7 +78,7 @@
*/
public function setEndT($endT)
{
- return $this->set("endT", $endT);
+ return $this->set("pio_endT", $endT);
}
/**
@@ -90,7 +90,7 @@
*/
public function setPrice($price)
{
- return $this->set("price", $price);
+ return $this->set("pio_price", $price);
}
/**
@@ -102,7 +102,7 @@
*/
public function setProfit($profit)
{
- return $this->set("profit", $profit);
+ return $this->set("pio_profit", $profit);
}
/**
@@ -116,7 +116,7 @@
*/
public function setLatlng($latlng)
{
- return $this->set("latlng", $latlng);
+ return $this->set("pio_latlng", $latlng);
}
/**
diff --git a/src/PredictionIO/Command/CreateUser.php b/src/PredictionIO/Command/CreateUser.php
index 12b038d..d3ebda7 100644
--- a/src/PredictionIO/Command/CreateUser.php
+++ b/src/PredictionIO/Command/CreateUser.php
@@ -31,7 +31,7 @@
*/
public function setUid($uid)
{
- return $this->set("uid", $uid);
+ return $this->set("pio_uid", $uid);
}
/**
@@ -45,7 +45,7 @@
*/
public function setLatlng($latlng)
{
- return $this->set("latlng", $latlng);
+ return $this->set("pio_latlng", $latlng);
}
/**
@@ -57,7 +57,7 @@
*/
public function setInactive($inactive)
{
- return $this->set("inactive", $inactive);
+ return $this->set("pio_inactive", $inactive);
}
/**
diff --git a/src/PredictionIO/Command/DeleteItem.php b/src/PredictionIO/Command/DeleteItem.php
index e5d990c..7f6f42b 100644
--- a/src/PredictionIO/Command/DeleteItem.php
+++ b/src/PredictionIO/Command/DeleteItem.php
@@ -23,7 +23,7 @@
*/
public function setIid($iid)
{
- return $this->set('iid', $iid);
+ return $this->set('pio_iid', $iid);
}
/**
@@ -31,7 +31,7 @@
*/
protected function build()
{
- $this->request = $this->client->createRequest(RequestInterface::DELETE, 'items/' . $this->get('iid'));
+ $this->request = $this->client->createRequest(RequestInterface::DELETE, 'items/' . $this->get('pio_iid'));
}
}
diff --git a/src/PredictionIO/Command/DeleteUser.php b/src/PredictionIO/Command/DeleteUser.php
index 991fa6b..e7eaee6 100644
--- a/src/PredictionIO/Command/DeleteUser.php
+++ b/src/PredictionIO/Command/DeleteUser.php
@@ -23,7 +23,7 @@
*/
public function setUid($uid)
{
- return $this->set('uid', $uid);
+ return $this->set('pio_uid', $uid);
}
/**
@@ -31,7 +31,7 @@
*/
protected function build()
{
- $this->request = $this->client->createRequest(RequestInterface::DELETE, 'users/' . $this->get('uid'));
+ $this->request = $this->client->createRequest(RequestInterface::DELETE, 'users/' . $this->get('pio_uid'));
}
}
diff --git a/src/PredictionIO/Command/GetItem.php b/src/PredictionIO/Command/GetItem.php
index febff74..ffb37de 100644
--- a/src/PredictionIO/Command/GetItem.php
+++ b/src/PredictionIO/Command/GetItem.php
@@ -24,7 +24,7 @@
*/
public function setIid($iid)
{
- return $this->set('iid', $iid);
+ return $this->set('pio_iid', $iid);
}
/**
@@ -32,7 +32,7 @@
*/
protected function build()
{
- $this->request = $this->client->createRequest(RequestInterface::GET, 'items/' . $this->get('iid'));
+ $this->request = $this->client->createRequest(RequestInterface::GET, 'items/' . $this->get('pio_iid'));
}
}
diff --git a/src/PredictionIO/Command/GetUser.php b/src/PredictionIO/Command/GetUser.php
index a634f98..ffcbdb2 100644
--- a/src/PredictionIO/Command/GetUser.php
+++ b/src/PredictionIO/Command/GetUser.php
@@ -23,7 +23,7 @@
*/
public function setUid($uid)
{
- return $this->set('uid', $uid);
+ return $this->set('pio_uid', $uid);
}
/**
@@ -31,7 +31,7 @@
*/
protected function build()
{
- $this->request = $this->client->createRequest(RequestInterface::GET, 'users/' . $this->get('uid'));
+ $this->request = $this->client->createRequest(RequestInterface::GET, 'users/' . $this->get('pio_uid'));
}
}
diff --git a/src/PredictionIO/Command/ItemrecGetTopN.php b/src/PredictionIO/Command/ItemrecGetTopN.php
index d1a5e38..10cad36 100644
--- a/src/PredictionIO/Command/ItemrecGetTopN.php
+++ b/src/PredictionIO/Command/ItemrecGetTopN.php
@@ -11,7 +11,6 @@
* Retrieve top N recommended items for this specific user.
*
* @guzzle engine type="string" required="true"
- * @guzzle uid type="string" required="true"
* @guzzle n type="integer" required="true"
* @guzzle itypes type="string"
* @guzzle latlng type="string"
@@ -29,19 +28,7 @@
*/
public function setEngine($engine)
{
- return $this->set('engine', $engine);
- }
-
- /**
- * Set the "uid" parameter for the current command
- *
- * @param string $uid User ID
- *
- * @return ItemrecGetTopN
- */
- public function setUid($uid)
- {
- return $this->set('uid', $uid);
+ return $this->set('pio_engine', $engine);
}
/**
@@ -53,7 +40,7 @@
*/
public function setN($n)
{
- return $this->set('n', $n);
+ return $this->set('pio_n', $n);
}
/**
@@ -68,9 +55,9 @@
public function setItypes($itypes)
{
if (is_array($itypes)) {
- return $this->set('itypes', implode(',', $itypes));
+ return $this->set('pio_itypes', implode(',', $itypes));
} else {
- return $this->set('itypes', $itypes);
+ return $this->set('pio_itypes', $itypes);
}
}
@@ -85,7 +72,7 @@
*/
public function setLatlng($latlng)
{
- return $this->set('latlng', $latlng);
+ return $this->set('pio_latlng', $latlng);
}
/**
@@ -97,7 +84,7 @@
*/
public function setWithin($within)
{
- return $this->set('within', $within);
+ return $this->set('pio_within', $within);
}
/**
@@ -109,7 +96,7 @@
*/
public function setUnit($unit)
{
- return $this->set('unit', $unit);
+ return $this->set('pio_unit', $unit);
}
/**
@@ -117,7 +104,8 @@
*/
protected function build()
{
- $this->request = $this->client->createRequest(RequestInterface::GET, 'engines/itemrec/' . $this->get('engine') . '/topn', null, $this->getAll());
+ $this->set('pio_uid', $this->client->getIdentity());
+ $this->request = $this->client->createRequest(RequestInterface::GET, 'engines/itemrec/' . $this->get('pio_engine') . '/topn', null, $this->getAll());
}
}
diff --git a/src/PredictionIO/Command/RecordActionOnItem.php b/src/PredictionIO/Command/RecordActionOnItem.php
new file mode 100644
index 0000000..382db3f
--- /dev/null
+++ b/src/PredictionIO/Command/RecordActionOnItem.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace PredictionIO\Command;
+
+use Guzzle\Http\Message\RequestInterface;
+use Guzzle\Service\Command\AbstractCommand;
+
+/**
+ * Record an action on an item by a user
+ *
+ * @guzzle action type="string" required="true"
+ * @guzzle iid type="string" required="true"
+ * @guzzle price type="float"
+ * @guzzle t type="string"
+ * @guzzle latlng type="string"
+ */
+class RecordActionOnItem extends AbstractCommand
+{
+ /**
+ * Set the "action" parameter for the current command
+ *
+ * @param string $action Action name
+ *
+ * @return RecordActionOnItem
+ */
+ public function setAction($action)
+ {
+ return $this->set('pio_action', $action);
+ }
+
+ /**
+ * Set the "iid" parameter for the current command
+ *
+ * @param string $iid Item ID
+ *
+ * @return RecordActionOnItem
+ */
+ public function setIid($iid)
+ {
+ return $this->set('pio_iid', $iid);
+ }
+
+ /**
+ * Set the "t" parameter for the current command
+ *
+ * @param string $t Time
+ *
+ * @return RecordActionOnItem
+ */
+ public function setT($t)
+ {
+ return $this->set('pio_t', $t*1000);
+ }
+
+ /**
+ * Set the "latlng" parameter for the current command
+ *
+ * In "latitude,longitude" format, e.g. "20.17,114.08"
+ *
+ * @param string $latlng Latitude and longitude
+ *
+ * @return RecordActionOnItem
+ */
+ public function setLatlng($latlng)
+ {
+ return $this->set('pio_latlng', $latlng);
+ }
+
+ /**
+ * Create the request object that will carry out the command. Used internally by Guzzle.
+ */
+ protected function build()
+ {
+ $this->set('pio_uid', $this->client->getIdentity());
+ $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i', null, $this->getAll());
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserActionItem.php b/src/PredictionIO/Command/UserActionItem.php
deleted file mode 100644
index bd7ae12..0000000
--- a/src/PredictionIO/Command/UserActionItem.php
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Parent class of User<Action>Item classes
- *
- * Do not use this directly. Use its subclasses.
- */
-class UserActionItem extends AbstractCommand
-{
- /**
- * Set the "uid" parameter for the current command
- *
- * @param string $uid User ID
- *
- * @return UserActionItem
- */
- public function setUid($uid)
- {
- return $this->set('uid', $uid);
- }
-
- /**
- * Set the "iid" parameter for the current command
- *
- * @param string $iid Item ID
- *
- * @return UserActionItem
- */
- public function setIid($iid)
- {
- return $this->set('iid', $iid);
- }
-
- /**
- * Set the "t" parameter for the current command
- *
- * @param string $t Time
- *
- * @return UserActionItem
- */
- public function setT($t)
- {
- return $this->set('t', $t*1000);
- }
-
- /**
- * Set the "latlng" parameter for the current command
- *
- * In "latitude,longitude" format, e.g. "20.17,114.08"
- *
- * @param string $latlng Latitude and longitude
- *
- * @return UserActionItem
- */
- public function setLatlng($latlng)
- {
- return $this->set('latlng', $latlng);
- }
-
- /**
- * Actual implementation in subclasses
- */
- protected function build()
- {
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserConversionItem.php b/src/PredictionIO/Command/UserConversionItem.php
deleted file mode 100644
index 69484aa..0000000
--- a/src/PredictionIO/Command/UserConversionItem.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Add Conversion
- *
- * A user performs an action that completes an activity that is beneficial to your business success
- *
- * @guzzle uid type="string" required="true"
- * @guzzle iid type="string" required="true"
- * @guzzle price type="float"
- * @guzzle t type="string"
- * @guzzle latlng type="string"
- */
-class UserConversionItem extends UserActionItem
-{
- /**
- * Set the "price" parameter for the current command
- *
- * @param float $price Price
- *
- * @return UserConversionItem
- */
- public function setPrice($price)
- {
- return $this->set('price', $price);
- }
-
- /**
- * Create the request object that will carry out the command. Used internally by Guzzle.
- */
- protected function build()
- {
- $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i/conversion', null, $this->getAll());
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserDislikeItem.php b/src/PredictionIO/Command/UserDislikeItem.php
deleted file mode 100644
index 1459c9f..0000000
--- a/src/PredictionIO/Command/UserDislikeItem.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Add Dislike
- *
- * A user dislikes an item
- *
- * @guzzle uid type="string" required="true"
- * @guzzle iid type="string" required="true"
- * @guzzle t type="string"
- * @guzzle latlng type="string"
- */
-class UserDislikeItem extends UserActionItem
-{
- /**
- * Create the request object that will carry out the command. Used internally by Guzzle.
- */
- protected function build()
- {
- $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i/dislike', null, $this->getAll());
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserLikeItem.php b/src/PredictionIO/Command/UserLikeItem.php
deleted file mode 100644
index 4c9a6f0..0000000
--- a/src/PredictionIO/Command/UserLikeItem.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Add Like
- *
- * A user likes an item
- *
- * @guzzle uid type="string" required="true"
- * @guzzle iid type="string" required="true"
- * @guzzle t type="string"
- * @guzzle latlng type="string"
- */
-class UserLikeItem extends UserActionItem
-{
- /**
- * Create the request object that will carry out the command. Used internally by Guzzle.
- */
- protected function build()
- {
- $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i/like', null, $this->getAll());
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserRateItem.php b/src/PredictionIO/Command/UserRateItem.php
deleted file mode 100644
index a6f736c..0000000
--- a/src/PredictionIO/Command/UserRateItem.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Add Rating
- *
- * A user rates an item
- *
- * @guzzle uid type="string" required="true"
- * @guzzle iid type="string" required="true"
- * @guzzle rate type="integer" required="true"
- * @guzzle t type="string"
- * @guzzle latlng type="string"
- */
-class UserRateItem extends UserActionItem
-{
- /**
- * Set the "rate" parameter for the current command
- *
- * @param integer $rate Rating
- *
- * @return UserRateItem
- */
- public function setRate($rate)
- {
- return $this->set('rate', $rate);
- }
-
- /**
- * Create the request object that will carry out the command. Used internally by Guzzle.
- */
- protected function build()
- {
- $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i/rate', null, $this->getAll());
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/Command/UserViewItem.php b/src/PredictionIO/Command/UserViewItem.php
deleted file mode 100644
index f4ccecb..0000000
--- a/src/PredictionIO/Command/UserViewItem.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-namespace PredictionIO\Command;
-
-use Guzzle\Http\Message\RequestInterface;
-use Guzzle\Service\Command\AbstractCommand;
-
-/**
- * Add View
- *
- * A user views an item
- *
- * @guzzle uid type="string" required="true"
- * @guzzle iid type="string" required="true"
- * @guzzle t type="string"
- * @guzzle latlng type="string"
- */
-class UserViewItem extends UserActionItem
-{
- /**
- * Create the request object that will carry out the command. Used internally by Guzzle.
- */
- protected function build()
- {
- $this->request = $this->client->createRequest(RequestInterface::POST, 'actions/u2i/view', null, $this->getAll());
- }
-}
-
-?>
\ No newline at end of file
diff --git a/src/PredictionIO/PredictionIOClient.php b/src/PredictionIO/PredictionIOClient.php
index dc46c20..4717bb0 100644
--- a/src/PredictionIO/PredictionIOClient.php
+++ b/src/PredictionIO/PredictionIOClient.php
@@ -28,20 +28,22 @@
* <code>
* // (your user registration logic)
* $uid = get_user_from_your_db();
- * $command = $client->getCommand('create_user', array('uid' => $uid));
+ * $command = $client->getCommand('create_user', array('pio_uid' => $uid));
* $response = $client->execute($command);
* // (other work to do for the rest of the page)
* </code>
*
* <b>Import a User Action (View) form Your App</b>
* <code>
- * $client->execute($client->getCommand('user_view_item', array('uid' => '4', 'iid' => '15')));
+ * $client->identify('4');
+ * $client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => '15')));
* // (other work to do for the rest of the page)
* </code>
*
* <b>Retrieving Top N Recommendations for a User</b>
* <code>
- * $client->execute($client->getCommand('itemrec_get_top_n', array('engine' => 'test', 'uid' => '4', 'n' => 10)));
+ * $client->identify('4');
+ * $client->execute($client->getCommand('itemrec_get_top_n', array('pio_engine' => 'test', 'pio_n' => 10)));
* // (work you need to do for the page (rendering, db queries, etc))
* </code>
*
@@ -50,6 +52,8 @@
*/
class PredictionIOClient extends Client
{
+ private $apiuid = "";
+
/**
* Factory method to create a new PredictionIOClient
*
@@ -73,6 +77,29 @@
}
/**
+ * Identify the user ID that will be used by all subsequent recording of actions on items, and recommendations retrieval.
+ *
+ * @param string $uid User ID.
+ */
+ public function identify($uid)
+ {
+ $this->apiuid = $uid;
+ }
+
+ /**
+ * Returns the identity (user ID) that will be used by all subsequent recording of actions on items, and recommendations retrieval.
+ *
+ * @returns string
+ */
+ public function getIdentity()
+ {
+ if (empty($this->apiuid)) {
+ throw new UnidentifiedUserException("Must call identify() before performing any user-related commands.");
+ }
+ return $this->apiuid;
+ }
+
+ /**
* Create and return a new Guzzle\Http\Message\RequestInterface configured for the client.
*
* Used internally by the library. Do not use directly.
@@ -87,9 +114,9 @@
public function createRequest($method = Guzzle\Http\Message\RequestInterface::GET, $uri = null, $headers = null, $body = null)
{
if (is_array($body)) {
- $body['appkey'] = $this->getConfig()->get("appkey");
+ $body['pio_appkey'] = $this->getConfig()->get("appkey");
} else {
- $body = array('appkey' => $this->getConfig()->get("appkey"));
+ $body = array('pio_appkey' => $this->getConfig()->get("appkey"));
}
// Remove Guzzle internals to prevent them from going to the API
@@ -110,3 +137,8 @@
return $request;
}
}
+
+/**
+ * Thrown when user-related commands are called before identify() is called.
+ */
+class UnidentifiedUserException extends \Exception { }