From 4b1ce6497ad0052f8c523a94abe4f2659c59639b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0rfan=20Evrens?= Date: Thu, 26 Jan 2017 16:43:31 +0300 Subject: [PATCH] some changes made. --- src/predictionio/BaseClient.php | 98 ++--- src/predictionio/EngineClient.php | 59 ++- src/predictionio/EventClient.php | 481 +++++++++++----------- src/predictionio/Exporter.php | 18 +- src/predictionio/FileExporter.php | 17 +- src/predictionio/PredictionIOAPIError.php | 10 +- 6 files changed, 346 insertions(+), 337 deletions(-) diff --git a/src/predictionio/BaseClient.php b/src/predictionio/BaseClient.php index 13ef8d2..0570b76 100644 --- a/src/predictionio/BaseClient.php +++ b/src/predictionio/BaseClient.php @@ -1,62 +1,64 @@ -baseUrl = $baseUrl; - $this->client = new Client([ - 'base_uri' => $this->baseUrl, - 'timeout' => $timeout, - 'connect_timeout' => $connectTimeout - ]); + /** + * @param string $baseUrl Base URL to the server + * @param float $timeout Timeout of the request in seconds. Use 0 to wait indefinitely + * @param float $connectTimeout Number of seconds to wait while trying to connect to a server + */ + public function __construct($baseUrl, $timeout, $connectTimeout) + { + $this->baseUrl = $baseUrl; + $this->client = new Client([ + 'base_uri' => $this->baseUrl, + 'timeout' => $timeout, + 'connect_timeout' => $connectTimeout + ]); - } + } - /** - * Get the status of the Event Server or Engine Instance - * - * @return string status - */ - public function getStatus() { - return $this->client->get('/')->getBody(); - } + /** + * Get the status of the Event Server or Engine Instance + * + * @return string status + */ + public function getStatus() + { + return $this->client->get('/')->getBody(); + } - /** - * Send a HTTP request to the server - * - * @param string $method HTTP request method - * @param string $url Relative or absolute url - * @param string $body HTTP request body - * - * @return array JSON response - * @throws PredictionIOAPIError Request error - */ - protected function sendRequest($method, $url, $body) { - $options = ['headers' => ['Content-Type' => 'application/json'], - 'body' => $body]; + /** + * Send a HTTP request to the server + * + * @param string $method HTTP request method + * @param string $url Relative or absolute url + * @param string $body HTTP request body + * + * @return array JSON response + * @throws PredictionIOAPIError Request error + */ + protected function sendRequest($method, $url, $body) + { + $options = [ + 'headers' => ['Content-Type' => 'application/json'], + 'body' => $body + ]; - try { - $response = $this->client->request($method, $url, $options); - return json_decode($response->getBody(), true); - } catch (ClientException $e) { - throw new PredictionIOAPIError($e->getMessage()); + try { + $response = $this->client->request($method, $url, $options); + return json_decode($response->getBody(), true); + } catch (ClientException $e) { + throw new PredictionIOAPIError($e->getMessage()); + } } - } } -?> diff --git a/src/predictionio/EngineClient.php b/src/predictionio/EngineClient.php index f45ac35..a0e6a36 100644 --- a/src/predictionio/EngineClient.php +++ b/src/predictionio/EngineClient.php @@ -1,37 +1,36 @@ -sendRequest("POST", "/queries.json", json_encode($query)); - } + /** + * Send prediction query to an Engine Instance + * + * @param array $query Query + * + * @return array JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function sendQuery(array $query) + { + return $this->sendRequest("POST", "/queries.json", json_encode($query)); + } } - -?> diff --git a/src/predictionio/EventClient.php b/src/predictionio/EventClient.php index df4286f..4fe961c 100644 --- a/src/predictionio/EventClient.php +++ b/src/predictionio/EventClient.php @@ -1,267 +1,274 @@ -accessKey = $accessKey; - $this->eventUrl = "/events.json?accessKey=$this->accessKey"; - } + /** + * @param string $accessKey Access Key + * @param string $baseUrl Base URL to the Event Server. Default is localhost:7070. + * @param float|int $timeout Timeout of the request in seconds. Use 0 to wait indefinitely + * Default is 0. + * @param float|int $connectTimeout Number of seconds to wait while trying to connect to a server. + * Default is 5. + */ + public function __construct($accessKey, $baseUrl = 'http://localhost:7070', + $timeout = 0, $connectTimeout = 5) + { + parent::__construct($baseUrl, $timeout, $connectTimeout); + $this->accessKey = $accessKey; + $this->eventUrl = "/events.json?accessKey=$this->accessKey"; + } - private function getEventTime($eventTime) { - $result = $eventTime; - if (!isset($eventTime)) { - $result = (new DateTime('NOW'))->format(self::DATE_TIME_FORMAT); - } + private function getEventTime($eventTime) + { + $result = $eventTime; + if (!isset($eventTime)) { + $result = (new DateTime('NOW'))->format(self::DATE_TIME_FORMAT); + } - return $result; - } + return $result; + } - /** - * Set a user entity - * - * @param int|string User Id - * @param array Properties of the user entity to set - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function setUser($uid, array $properties=array(), $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); + /** + * Set a user entity + * + * @param int|string User Id + * @param array $properties Properties of the user entity to set + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function setUser($uid, array $properties = array(), $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); - // casting to object so that an empty array would be represented as {} - if (empty($properties)) $properties = (object)$properties; + // casting to object so that an empty array would be represented as {} + if (empty($properties)) $properties = (object)$properties; - $json = json_encode([ - 'event' => '$set', - 'entityType' => 'user', - 'entityId' => $uid, - 'properties' => $properties, - 'eventTime' => $eventTime, - ]); + $json = json_encode([ + 'event' => '$set', + 'entityType' => 'user', + 'entityId' => $uid, + 'properties' => $properties, + 'eventTime' => $eventTime, + ]); - return $this->sendRequest('POST', $this->eventUrl, $json); - } + return $this->sendRequest('POST', $this->eventUrl, $json); + } - /** - * Unset a user entity - * - * @param int|string User Id - * @param array Properties of the user entity to unset - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function unsetUser($uid, array $properties, $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); - if (empty($properties)) - throw new PredictionIOAPIError('Specify at least one property'); + /** + * Unset a user entity + * + * @param int|string User Id + * @param array $properties Properties of the user entity to unset + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function unsetUser($uid, array $properties, $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); + if (empty($properties)) + throw new PredictionIOAPIError('Specify at least one property'); - $json = json_encode([ - 'event' => '$unset', - 'entityType' => 'user', - 'entityId' => $uid, - 'properties' => $properties, - 'eventTime' => $eventTime, - ]); + $json = json_encode([ + 'event' => '$unset', + 'entityType' => 'user', + 'entityId' => $uid, + 'properties' => $properties, + 'eventTime' => $eventTime, + ]); - return $this->sendRequest('POST', $this->eventUrl, $json); - } + return $this->sendRequest('POST', $this->eventUrl, $json); + } - /** - * Delete a user entity - * - * @param int|string User Id - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function deleteUser($uid, $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); + /** + * Delete a user entity + * + * @param int|string User Id + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function deleteUser($uid, $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); - $json = json_encode([ - 'event' => '$delete', - 'entityType' => 'user', - 'entityId' => $uid, - 'eventTime' => $eventTime, - ]); + $json = json_encode([ + 'event' => '$delete', + 'entityType' => 'user', + 'entityId' => $uid, + 'eventTime' => $eventTime, + ]); - return $this->sendRequest('POST', $this->eventUrl, $json); - } - - /** - * Set an item entity - * - * @param int|string Item Id - * @param array Properties of the item entity to set - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function setItem($iid, array $properties=array(), $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); - if (empty($properties)) $properties = (object)$properties; - $json = json_encode([ - 'event' => '$set', - 'entityType' => 'item', - 'entityId' => $iid, - 'properties' => $properties, - 'eventTime' => $eventTime, - ]); + return $this->sendRequest('POST', $this->eventUrl, $json); + } - return $this->sendRequest('POST', $this->eventUrl, $json); - } + /** + * Set an item entity + * + * @param int|string Item Id + * @param array $properties Properties of the item entity to set + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function setItem($iid, array $properties = array(), $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); + if (empty($properties)) $properties = (object)$properties; + $json = json_encode([ + 'event' => '$set', + 'entityType' => 'item', + 'entityId' => $iid, + 'properties' => $properties, + 'eventTime' => $eventTime, + ]); - /** - * Unset an item entity - * - * @param int|string Item Id - * @param array Properties of the item entity to unset - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function unsetItem($iid, array $properties, $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); - if (empty($properties)) - throw new PredictionIOAPIError('Specify at least one property'); - $json = json_encode([ - 'event' => '$unset', - 'entityType' => 'item', - 'entityId' => $iid, - 'properties' => $properties, - 'eventTime' => $eventTime, - ]); + return $this->sendRequest('POST', $this->eventUrl, $json); + } - return $this->sendRequest('POST', $this->eventUrl, $json); - } + /** + * Unset an item entity + * + * @param int|string Item Id + * @param array $properties Properties of the item entity to unset + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function unsetItem($iid, array $properties, $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); + if (empty($properties)) + throw new PredictionIOAPIError('Specify at least one property'); + $json = json_encode([ + 'event' => '$unset', + 'entityType' => 'item', + 'entityId' => $iid, + 'properties' => $properties, + 'eventTime' => $eventTime, + ]); - /** - * Delete an item entity - * - * @param int|string Item Id - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function deleteItem($iid, $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); + return $this->sendRequest('POST', $this->eventUrl, $json); + } - $json = json_encode([ - 'event' => '$delete', - 'entityType' => 'item', - 'entityId' => $iid, - 'eventTime' => $eventTime, - ]); + /** + * Delete an item entity + * + * @param int|string Item Id + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function deleteItem($iid, $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); - return $this->sendRequest('POST', $this->eventUrl, $json); - } + $json = json_encode([ + 'event' => '$delete', + 'entityType' => 'item', + 'entityId' => $iid, + 'eventTime' => $eventTime, + ]); - /** - * Record a user action on an item - * - * @param string Event name - * @param int|string User Id - * @param int|string Item Id - * @param array Properties of the event - * @param string Time of the event in ISO 8601 format - * (e.g. 2014-09-09T16:17:42.937-08:00). - * Default is the current time. - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function recordUserActionOnItem($event, $uid, $iid, - array $properties=array(), - $eventTime=null) { - $eventTime = $this->getEventTime($eventTime); - if (empty($properties)) $properties = (object)$properties; - $json = json_encode([ - 'event' => $event, - 'entityType' => 'user', - 'entityId' => $uid, - 'targetEntityType' => 'item', - 'targetEntityId' => $iid, - 'properties' => $properties, - 'eventTime' => $eventTime, - ]); + return $this->sendRequest('POST', $this->eventUrl, $json); + } - return $this->sendRequest('POST', $this->eventUrl, $json); - } + /** + * Record a user action on an item + * + * @param string $event Event name + * @param int|string User Id + * @param int|string Item Id + * @param array $properties Properties of the event + * @param string|null $eventTime Time of the event in ISO 8601 format + * (e.g. 2014-09-09T16:17:42.937-08:00). + * Default is the current time. + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function recordUserActionOnItem($event, $uid, $iid, + array $properties = array(), + $eventTime = null) + { + $eventTime = $this->getEventTime($eventTime); + if (empty($properties)) $properties = (object)$properties; + $json = json_encode([ + 'event' => $event, + 'entityType' => 'user', + 'entityId' => $uid, + 'targetEntityType' => 'item', + 'targetEntityId' => $iid, + 'properties' => $properties, + 'eventTime' => $eventTime, + ]); - /** - * Create an event - * - * @param array An array describing the event - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function createEvent(array $data) { - $json = json_encode($data); + return $this->sendRequest('POST', $this->eventUrl, $json); + } - return $this->sendRequest('POST', $this->eventUrl, $json); - } + /** + * Create an event + * + * @param array $data An array describing the event + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function createEvent(array $data) + { + $json = json_encode($data); - /** - * Retrieve an event - * - * @param string Event ID - * - * @return string JSON response - * - * @throws PredictionIOAPIError Request error - */ - public function getEvent($eventId) { - return $this->sendRequest('GET', - "/events/$eventId.json?accessKey=$this->accessKey", ''); - } -} + return $this->sendRequest('POST', $this->eventUrl, $json); + } -?> + /** + * Retrieve an event + * + * @param string $eventId Event ID + * + * @return string JSON response + * + * @throws PredictionIOAPIError Request error + */ + public function getEvent($eventId) + { + return $this->sendRequest('GET', + "/events/$eventId.json?accessKey=$this->accessKey", ''); + } +} diff --git a/src/predictionio/Exporter.php b/src/predictionio/Exporter.php index 35b7a03..4655d2d 100644 --- a/src/predictionio/Exporter.php +++ b/src/predictionio/Exporter.php @@ -1,13 +1,12 @@ -export($json); } -} \ No newline at end of file +} diff --git a/src/predictionio/FileExporter.php b/src/predictionio/FileExporter.php index d0ee711..635639e 100644 --- a/src/predictionio/FileExporter.php +++ b/src/predictionio/FileExporter.php @@ -1,27 +1,28 @@ -file = fopen($fileName, 'w'); } - public function export($json) { + public function export($json) + { fwrite($this->file, "$json\n"); } - public function close() { + public function close() + { fclose($this->file); } } \ No newline at end of file diff --git a/src/predictionio/PredictionIOAPIError.php b/src/predictionio/PredictionIOAPIError.php index ca4cacd..26300f8 100644 --- a/src/predictionio/PredictionIOAPIError.php +++ b/src/predictionio/PredictionIOAPIError.php @@ -1,9 +1,9 @@ - +} \ No newline at end of file