/** * Generates a curl request to the Ingestion Endpoint */ public function ingest($data_set) { if (strlen($this->_source_id) == 0) { throw new DataSift_Exception_InvalidData('Cannot make request without a source ID'); } if (empty($data_set)) { throw new DataSift_Exception_InvalidData('Cannot make request without a valid data set'); } return $this->_user->post($this->getSourceId(), $data_set, array(), true); }
/** * Make a call to a DataSift API endpoint. * * @param DataSift_User $user The user's username. * @param string $endPoint The endpoint of the API call. * @param array $headers The headers to be sent. * @param array $successCode The codes defined as a success for the call. * @param array $params The parameters to be passed along with the request. * @param string $userAgent The HTTP User-Agent header. * * @return array The response from the server. * @throws DataSift_Exception_APIError * @throws DataSift_Exception_RateLimitExceeded * @throws DataSift_Exception_NotYetImplemented */ public static function call(DataSift_User $user, $endPoint, $method, $params = array(), $headers = array(), $userAgent = DataSift_User::USER_AGENT, $qs = array(), $ingest = false) { $decodeCode = array(self::HTTP_OK, self::HTTP_NO_CONTENT); // Curl is required if (!function_exists('curl_init')) { throw new DataSift_Exception_NotYetImplemented('Curl is required for DataSift_ApiClient'); } if (empty($headers)) { $headers = array('Auth: ' . $user->getUsername() . ':' . $user->getAPIKey(), 'Expect:', 'Content-Type: application/json'); } $ssl = $user->useSSL(); // Build the full endpoint URL if ($ingest) { $url = 'http' . ($ssl ? 's' : '') . '://' . $user->getIngestUrl() . $endPoint; } else { $url = 'http' . ($ssl ? 's' : '') . '://' . $user->getApiUrl() . $user->getApiVersion() . '/' . $endPoint; } $ch = self::initialize($method, $ssl, $url, $headers, $params, $userAgent, $qs, $ingest); $res = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); $res = self::parseHTTPResponse($res); if ($user->getDebug()) { $log['headers'] = $res['headers']; $log['status'] = $info['http_code']; $log['body'] = self::decodeBody($res); $user->setLastResponse($log); } $retval = array('response_code' => $info['http_code'], 'data' => strlen($res['body']) == 0 ? array() : self::decodeBody($res), 'rate_limit' => isset($res['headers']['x-ratelimit-limit']) ? $res['headers']['x-ratelimit-limit'] : -1, 'rate_limit_remaining' => isset($res['headers']['x-ratelimit-remaining']) ? $res['headers']['x-ratelimit-remaining'] : -1); return $retval; }
/** * Returns the user agent this library should use for all API calls. * * @return string */ public function usage($start = false, $end = false, $period = null) { $params = array(); if ($start) { $params['start'] = $start; } if ($end) { $params['end'] = $end; } if (isset($period)) { $params['period'] = $period; } return $this->_user->get('account/usage', $params); }
public function testGetUsage() { $response = array('response_code' => 200, 'data' => array('start' => date('Y-m-d H:i:s', time()), 'end' => date('Y-m-d H:i:s', time()), 'streams' => array('stream1', 'stream2')), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); $usage = $this->user->getUsage(); $this->assertTrue(isset($usage['start']), 'Usage data does not contain a start date'); $this->assertTrue(isset($usage['end']), 'Usage data does not contain a start date'); $this->assertInternalType('array', $usage['streams'], 'Usage data does not contain a valid stream array'); }
/** * List Historics queries. * * @param DataSift_User $user The user object. * @param int $page The start page. * @param int $per_page The start page. * * @throws DataSift_Exception_InvalidData * @throws DataSift_Exception_APIError */ public static function listHistorics($user, $page = 1, $per_page = 20) { try { $res = $user->post('historics/get', array('page' => $page, 'max' => $page)); $retval = array('count' => $res['count'], 'historics' => array()); foreach ($res['data'] as $historic) { $retval['historics'][] = new self($user, $historic); } return $retval; } catch (DataSift_Exception_APIError $e) { switch ($e->getCode()) { case 400: // Missing or invalid parameters throw new DataSift_Exception_InvalidData($e->getMessage()); default: throw new DataSift_Exception_APIError('Unexpected APIError code: ' . $e->getCode() . ' [' . $e->getMessage() . ']'); } } }
/** * Updates a recording with a new hash and or name * * @param string $id The id of the existing recording * @param string $hash The new hash of the pylon recording * @param string $name The new updated name of the recording * * @throws DataSift_Exception_InvalidData */ public function update($id = false, $hash = false, $name = false) { if ($id) { $this->_id = $id; } if ($hash) { $this->_hash = $hash; } if ($name) { $this->_name = $name; } $params = array('id' => $this->_id, 'hash' => $this->_hash, 'name' => $this->_name); $this->_user->put('pylon/update', $params); }
/** * Constructor. Do not use this directly, use the factory method instead. * * @param DataSift_User $user The user this consumer will run as. * @param mixed $definition CSDL string, a Definition object, or an array of hashes. * @param DataSift_IStreamConsumerEventHandler $eventHandler The object that will receive events. * * @throws DataSift_Exception_InvalidData * @throws DataSiftExceotion_CompileFailed * @throws DataSift_Exception_APIError */ protected function __construct($user, $definition, $eventHandler) { if (!$user instanceof DataSift_User) { throw new DataSift_Exception_InvalidData('Please supply a valid DataSift_User object when creating a DataSift_StreamConsumer object.'); } if (is_array($definition) && count($definition) > 0) { // Yes, we're multi $this->_is_multi = true; // Get the hashes foreach ($definition as $d) { if ($d instanceof DataSift_Definition) { $this->_hashes[] = $d->getHash(); } else { $this->_hashes[] = $d; } } } elseif (is_string($definition)) { // Convert the CSDL into a Definition object $this->_definition = $user->createDefinition($definition); } elseif ($definition instanceof DataSift_Definition) { // Already a Definition object $this->_definition = $definition; } else { throw new DataSift_Exception_InvalidData('The definition must be a CSDL string, a DataSift_Definition object, or an array of stream hashes.'); } // Set the user $this->_user = $user; // Validate and set the event handler if (!$eventHandler instanceof DataSift_IStreamConsumerEventHandler) { throw new DataSift_Exception_InvalidData('Your event handler object must implement the DataSift_IStreamConsumerEventHandler interface.'); } $this->_eventHandler = $eventHandler; // Ask for the definition hash - this will compile the definition if // necessary if (!$this->_is_multi) { $this->_definition->getHash(); } }
/** * Page through recent push subscription log entries, specifying the sort * order. * * @param DataSift_User $user The user making the request. * @param int $page Which page to fetch. * @param int $per_page Based on this page size. * @param string $order_by Which field to sort by. * @param string $order_dir In asc[ending] or desc[ending] order. * @param string $id Push subscription ID. * * @return array Of LogEntry objects. * @throws DataSift_Exception_APIError * @throws DataSift_Exception_InvalidData * @throws DataSift_Exception_AccessDenied */ public static function getLogs($user, $page = 1, $per_page = 20, $order_by = self::ORDERBY_REQUEST_TIME, $order_dir = self::ORDERDIR_ASC, $id = false) { if ($page < 1) { throw new DataSift_Exception_InvalidData('The specified page number is invalid'); } if ($per_page < 1) { throw new DataSift_Exception_InvalidData('The specified per_page value is invalid'); } $params = array(); if ($id !== false && strlen($id) > 0) { $params['id'] = $id; } $params['page'] = $page; $params['per_page'] = $per_page; $params['order_by'] = $order_by; $params['order_dir'] = $order_dir; $res = $user->post('push/log', $params); $retval = array('count' => $res['count'], 'log_entries' => array()); foreach ($res['log_entries'] as $log) { $retval['log_entries'][] = new DataSift_Push_LogEntry($log); } return $retval; }
/** * Gets a single DataSift_Source object by ID * * @param DataSift_User $user The user making the request. * @param string $id The id of the Source to fetch * * @return DataSift_Source * * @throws DataSift_Exception_APIError * @throws DataSift_Exception_AccessDenied */ public static function get(DataSift_User $user, $id) { $params = array('id' => $id); return new self($user, $user->post('source/get', $params)); }
// Ignore the other events for the purposes of this example. public function onConnect($consumer) { } public function onDeleted($consumer, $interaction, $hash) { } public function onStatus($consumer, $type, $info) { } public function onWarning($consumer, $message) { } public function onError($consumer, $message) { } public function onDisconnect($consumer) { } public function onStopped($consumer, $reason) { } } // Create the user $user = new DataSift_User('username', 'api_key'); // Create a definition looking for the word "datasift" $def = $user->createDefinition('interaction.content contains "datasift"'); // Get an HTTP stream consumer for that definition $consumer = $def->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler()); // Consume it - this will not return unless the stream gets disconnected $consumer->consume();
* Called when the stream is disconnected. * * @param DataSift_StreamConsumer $consumer The consumer object. */ public function onDisconnect($consumer) { echo 'Disconnected' . PHP_EOL; } /** * Called when the consumer has stopped. * * @param DataSift_StreamConsumer $consumer The consumer object. * @param string $reason The reason the consumer stopped. */ public function onStopped($consumer, $reason) { echo PHP_EOL . 'Stopped: ' . $reason . PHP_EOL . PHP_EOL; } } // Drop the script name from the command line arguments array_shift($_SERVER['argv']); // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the consumer echo "Getting the consumer...\n"; $consumer = $user->getMultiConsumer(DataSift_StreamConsumer::TYPE_HTTP, $_SERVER['argv'], new EventHandler()); // And start consuming echo "Consuming...\n--\n"; $consumer->consume(); // The consumer will never end
* @author Paul Mozo <*****@*****.**> * @copyright 2011 MediaSift Ltd. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @link http://www.mediasift.com */ /** * This script creates a pull subscription and requests data from it periodically. * */ // Include the DataSift library require dirname(__FILE__) . '/../../lib/datasift.php'; // Include the configuration - put your username and API key in this file require dirname(__FILE__) . '/../../config.php'; // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); //Define some CSDL $csdl = 'interaction.content contains "coffee" AND interaction.type == "tumblr"'; // Create the stream definition $stream_definition = $user->createDefinition($csdl); //Create the push definition $push_definition = $user->createPushDefinition(); $push_definition->setOutputType('pull'); $push_sub = $push_definition->subscribeDefinition($stream_definition, 'My PHP pull subscription'); echo "Pull subscription created, ID: " . $push_sub->getId() . "\n"; //Pull 10 times, every 2 seconds for ($i = 1; $i <= 10; $i++) { sleep(2); echo "Pull number {$i}\n"; $interactions = $push_sub->pull(); foreach ($interactions as $interaction) {