Overview

Namespaces

  • PHP
  • predictionio

Classes

  • BaseClient
  • EngineClient
  • EventClient

Exceptions

  • PredictionIOAPIError
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace predictionio;
  4: use GuzzleHttp\Client;
  5: use \DateTime;
  6:  
  7: /**
  8:  * Client for connecting to an Event Server
  9:  *
 10:  */
 11: class EventClient extends BaseClient {
 12:   const DATE_TIME_FORMAT = DateTime::ISO8601;
 13:   private $accessKey;
 14:   private $eventUrl;
 15: 
 16:   /**
 17:    * @param string Access Key
 18:    * @param string Base URL to the Event Server. Default is localhost:7070.
 19:    * @param float Timeout of the request in seconds. Use 0 to wait indefinitely
 20:    *              Default is 0.
 21:    * @param float Number of seconds to wait while trying to connect to a server.
 22:    *              Default is 5.                
 23:    */
 24:   public function __construct($accessKey, $baseUrl='http://localhost:7070',
 25:                               $timeout=0, $connectTimeout=5 ) {
 26:     parent::__construct($baseUrl, $timeout, $connectTimeout);
 27:     $this->accessKey = $accessKey;
 28:     $this->eventUrl = "/events.json?accessKey=$this->accessKey";
 29:   }
 30: 
 31:   private function getEventTime($eventTime) {
 32:     $result = $eventTime;
 33:     if (!isset($eventTime)) {
 34:       $result = (new DateTime('NOW'))->format(self::DATE_TIME_FORMAT);
 35:     } 
 36: 
 37:     return $result;
 38:   }
 39: 
 40:   /**
 41:    * Set a user entity
 42:    *
 43:    * @param int|string User Id 
 44:    * @param array Properties of the user entity to set
 45:    * @param string Time of the event in ISO 8601 format
 46:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
 47:    *               Default is the current time.
 48:    *
 49:    * @return string JSON response
 50:    * 
 51:    * @throws PredictionIOAPIError Request error
 52:    */
 53:   public function setUser($uid, array $properties=array(), $eventTime=null) {
 54:     $eventTime = $this->getEventTime($eventTime);
 55: 
 56:     // casting to object so that an empty array would be represented as {}
 57:     if (empty($properties)) $properties = (object)$properties;
 58: 
 59:     $json = json_encode([
 60:         'event' => '$set',
 61:         'entityType' => 'user',
 62:         'entityId' => $uid,
 63:         'properties' => $properties,
 64:         'eventTime' => $eventTime,
 65:     ]);
 66: 
 67:     return $this->sendRequest('POST', $this->eventUrl, $json);
 68:   }
 69: 
 70:   /**
 71:    * Unset a user entity
 72:    *
 73:    * @param int|string User Id 
 74:    * @param array Properties of the user entity to unset
 75:    * @param string Time of the event in ISO 8601 format
 76:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
 77:    *               Default is the current time.
 78:    *
 79:    * @return string JSON response
 80:    * 
 81:    * @throws PredictionIOAPIError Request error
 82:    */
 83:   public function unsetUser($uid, array $properties, $eventTime=null) {
 84:     $eventTime = $this->getEventTime($eventTime);
 85:     if (empty($properties)) 
 86:       throw new PredictionIOAPIError('Specify at least one property'); 
 87: 
 88:     $json = json_encode([
 89:         'event' => '$unset',
 90:         'entityType' => 'user',
 91:         'entityId' => $uid,
 92:         'properties' => $properties,
 93:         'eventTime' => $eventTime,
 94:     ]);
 95: 
 96:     return $this->sendRequest('POST', $this->eventUrl, $json);
 97:   }
 98: 
 99:   /**
100:    * Delete a user entity
101:    *
102:    * @param int|string User Id 
103:    * @param string Time of the event in ISO 8601 format
104:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
105:    *               Default is the current time.
106:    *
107:    * @return string JSON response
108:    * 
109:    * @throws PredictionIOAPIError Request error
110:    */
111:   public function deleteUser($uid, $eventTime=null) {
112:     $eventTime = $this->getEventTime($eventTime);
113: 
114:     $json = json_encode([
115:         'event' => '$delete',
116:         'entityType' => 'user',
117:         'entityId' => $uid,
118:         'eventTime' => $eventTime,
119:     ]);
120: 
121:     return $this->sendRequest('POST', $this->eventUrl, $json);
122:   }
123:  
124:   /**
125:    * Set an item entity
126:    *
127:    * @param int|string Item Id 
128:    * @param array Properties of the item entity to set
129:    * @param string Time of the event in ISO 8601 format
130:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
131:    *               Default is the current time.
132:    *
133:    * @return string JSON response
134:    * 
135:    * @throws PredictionIOAPIError Request error
136:    */
137:    public function setItem($iid, array $properties=array(), $eventTime=null) {
138:     $eventTime = $this->getEventTime($eventTime);
139:     if (empty($properties)) $properties = (object)$properties;
140:     $json = json_encode([
141:         'event' => '$set',
142:         'entityType' => 'item',
143:         'entityId' => $iid,
144:         'properties' => $properties,
145:         'eventTime' => $eventTime,
146:     ]);
147: 
148:     return $this->sendRequest('POST', $this->eventUrl, $json);
149:   }
150: 
151:   /**
152:    * Unset an item entity
153:    *
154:    * @param int|string Item Id 
155:    * @param array Properties of the item entity to unset
156:    * @param string Time of the event in ISO 8601 format
157:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
158:    *               Default is the current time.
159:    *
160:    * @return string JSON response
161:    * 
162:    * @throws PredictionIOAPIError Request error
163:    */
164:   public function unsetItem($iid, array $properties, $eventTime=null) {
165:     $eventTime = $this->getEventTime($eventTime);
166:     if (empty($properties))
167:         throw new PredictionIOAPIError('Specify at least one property'); 
168:     $json = json_encode([
169:         'event' => '$unset',
170:         'entityType' => 'item',
171:         'entityId' => $iid,
172:         'properties' => $properties,
173:         'eventTime' => $eventTime,
174:     ]);
175: 
176:     return $this->sendRequest('POST', $this->eventUrl, $json);
177:   }
178: 
179:   /**
180:    * Delete an item entity
181:    *
182:    * @param int|string Item Id 
183:    * @param string Time of the event in ISO 8601 format
184:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
185:    *               Default is the current time.
186:    *
187:    * @return string JSON response
188:    * 
189:    * @throws PredictionIOAPIError Request error
190:    */
191:   public function deleteItem($iid, $eventTime=null) {
192:     $eventTime = $this->getEventTime($eventTime);
193: 
194:     $json = json_encode([
195:         'event' => '$delete',
196:         'entityType' => 'item',
197:         'entityId' => $iid,
198:         'eventTime' => $eventTime,
199:     ]);
200: 
201:     return $this->sendRequest('POST', $this->eventUrl, $json);
202:   }
203: 
204:   /**
205:    * Record a user action on an item
206:    *
207:    * @param string Event name
208:    * @param int|string User Id 
209:    * @param int|string Item Id 
210:    * @param array Properties of the event
211:    * @param string Time of the event in ISO 8601 format
212:    *               (e.g. 2014-09-09T16:17:42.937-08:00).
213:    *               Default is the current time.
214:    *
215:    * @return string JSON response
216:    * 
217:    * @throws PredictionIOAPIError Request error
218:    */
219:    public function recordUserActionOnItem($event, $uid, $iid, 
220:                                          array $properties=array(),
221:                                          $eventTime=null) {
222:     $eventTime = $this->getEventTime($eventTime);
223:     if (empty($properties)) $properties = (object)$properties;
224:     $json = json_encode([
225:         'event' => $event,
226:         'entityType' => 'user',
227:         'entityId' => $uid,
228:         'targetEntityType' => 'item',
229:         'targetEntityId' => $iid,
230:         'properties' => $properties,
231:         'eventTime' => $eventTime,
232:     ]);
233: 
234:     return $this->sendRequest('POST', $this->eventUrl, $json);
235:   }
236: 
237:   /**
238:    * Create an event
239:    *
240:    * @param array An array describing the event
241:    *
242:    * @return string JSON response
243:    * 
244:    * @throws PredictionIOAPIError Request error
245:    */
246:   public function createEvent(array $data) {
247:     $json = json_encode($data);
248: 
249:     return $this->sendRequest('POST', $this->eventUrl, $json);
250:   }
251: 
252:   /**
253:    * Retrieve an event
254:    *
255:    * @param string Event ID
256:    *
257:    * @return string JSON response
258:    * 
259:    * @throws PredictionIOAPIError Request error
260:    */
261:   public function getEvent($eventId) {
262:     return $this->sendRequest('GET', 
263:       "/events/$eventId.json?accessKey=$this->accessKey", '');
264:   }
265: }
266: 
267: ?>
268: 
PredictionIO API PHP Client API documentation generated by ApiGen 2.8.0