public function generateAvroCseMessage() { $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*'); $cse_product_details = array(); foreach ($collection as $p) { //$message = array("Items" => array(array("sku" => $p->getSku(), "title" => $p->getName(),"price" => $p->getPrice())) ); //Mage::log($p); // $message = array("Items" => array(array("sku" => $p->getSku(), "title" => $p->getName(),"price" => $p->getPrice())) ); // Mage::log($message); $cse_product_detail = array("sku" => $p->getSku(), "title" => $p->getName(), "description" => $p->getDescription(), "manufacturer" => "NA", "MPN" => "NA", "GTIN" => "NA", "brand" => "NA", "category" => "NA", "images" => array(), "link" => $p->getUrlPath(), "condition" => "New", "salePrice" => array("amount" => floatval($p->getPrice()), "code" => "USD"), "originalPrice" => array("amount" => floatval($p->getPrice()), "code" => "USD"), "availability" => "InStock", "taxRate" => array("country" => "US", "region" => "TX", "rate" => 8.5, "taxShipping" => false), "shipping" => array("country" => "US", "region" => "TX", "service" => "UPS", "price" => array("amount" => 3.99, "code" => "USD")), "shippingWeight" => floatval($p->getWeight()), "attributes" => array(), "variations" => array(), "offerId" => "NA", "cpc" => array("amount" => 0.0, "code" => "USD")); // Push the product info for the current product into the the $cse_product_details array array_push($cse_product_details, $cse_product_detail); //Mage::log($cse_product_details); } $content = file_get_contents("http://workshop.dev/fabric_post/contracts/cse.avpr"); if ($content == false) { echo "Error reading schema!\n"; } // Parse the CSE Avro schema and place results in an AvroSchema object $schema = AvroSchema::parse($content); // Create an AvroIODataWriter object for the supplied AvroSchema. // An AvroIODataWriter object handles schema-specific writing of data to the encoder and // ensures that each datum written is consistent with the writer's schema. $datum_writer = new AvroIODatumWriter($schema); // Create an AvroStringIO object - this is an AvroIO wrapper for string I/O $write_io = new AvroStringIO(); // Create an AvroIOBinaryEncoder object. // This object encodes and writes Avro data to the supplied AvroIO object using Avro binary encoding. $encoder = new AvroIOBinaryEncoder($write_io); // The result is stored in the AvroStringIO object $write_io created above $message = array("products" => $cse_product_details, "productFeedType" => "Full"); $datum_writer->write($message, $encoder); Mage::log($message); return $write_io->string(); }
public function saveInventoryData($observer) { $p = $observer->getEvent()->getProduct(); // Load the Avro protocol definition //shown as an example. we are using the same contract from innovate demo. $schema = AvroSchema::parse(file_get_contents("/tmp/innovate_sample.avpr")); $datum_writer = new AvroIODatumWriter($schema); $write_io = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($write_io); $attributes = $p->toArray(); // Set up an item $message = array("Items" => array(array("sku" => $p->getSku(), "title" => $p->getName(), "currentPrice" => $p->getPrice(), "url" => $p->getProductUrl(), "dealOfTheDay" => $attributes["deal_of_the_day"]))); $datum_writer->write($message, $encoder); $ch = curl_init(); // The URL is the host:port for the XFabric, plus the topic curl_setopt($ch, CURLOPT_URL, "https://localhost:8080/inventory/updated"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); //note that we are defining schema uri ourselves here for the listening capability //for a standard contract message this will be automatically created. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: avro/binary", "Authorization: Bearer QVVUSElELTEA/u4OWAoV2/U2vinS0mC8B22S/ejfKnXRz8tKqtscwB4=", "X-XC-SCHEMA-VERSION: 1.0.0", "X-XC-SCHEMA-URI: http://localhost/Items.avsc")); // Our message curl_setopt($ch, CURLOPT_POSTFIELDS, $write_io->string()); $response = curl_exec($ch); Mage::log($response); $error = curl_error($ch); $result = array('header' => '', 'body' => '', 'curl_error' => '', 'http_code' => '', 'last_url' => ''); if ($error != "") { $result['curl_error'] = $error; } curl_close($ch); }
function sendPing($message) { // Get variables include 'variables.php'; // Get Avro and encode the message include_once 'avro.php'; $content = file_get_contents($schema_uri); $schema = AvroSchema::parse($content); $datum_writer = new AvroIODatumWriter($schema); $write_io = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($write_io); $current = "<h2>Sending Ping</h2>" . date('Y-m-d h:i:s') . "\n"; try { $datum_writer->write($message, $encoder); $message = $write_io->string(); try { // Set up cURL $curl = curl_init(); // Initiate it // Send a header curl_setopt($curl, CURLOPT_HEADER, TRUE); // Send as POST curl_setopt($curl, CURLOPT_POST, TRUE); // Don't worry about ssl stuff for now // (You'll want to when you've got a full capability up and running) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // Stop cURL if it takes longer than 5 seconds curl_setopt($curl, CURLOPT_TIMEOUT, 5); // Give returned information back to cURL curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // Craft cURL data // Address XFabric and topic curl_setopt($curl, CURLOPT_URL, $fabricURL . "/message/ping"); // Set message as POST curl_setopt($curl, CURLOPT_POSTFIELDS, $message); // Set the headers curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: " . $authToken, "X-XC-SCHEMA-VERSION: " . $version, "Content-Type: " . $type, "X-XC-DESTINATION-ID: " . $destID, "Content-Length: " . strlen($message))); // Send the message $result = curl_exec($curl) . "\n"; // Check that it worked if (substr($result, 0, 15) == "HTTP/1.1 200 OK") { $current .= "Ping successfully sent! \nMessage:\n" . $message; } else { $current .= "Error in sending ping. \nResponse: " . $result; } } catch (Exception $e) { // Oh no! An exception! $current .= "Error in script: " . $e; } } catch (Exception $e) { // Oh, no! An exception! $current .= "Message is not able to be avro-encoded!"; } // Write to log file file_put_contents($file, $current); }
public function encodeText($text, $rawSchema) { /* @deprecated backward compatibility with Messaging Framework 0.0.1 */ if ($rawSchema instanceof Xcom_Xfabric_Model_Message_Abstract) { $rawSchema = $rawSchema->getSchema()->getRawSchema(); } $schema = AvroSchema::parse($rawSchema); $datum_writer = new AvroIODatumWriter($schema); $write_io = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($write_io); $datum_writer->write($text, $encoder); $encodedText = $write_io->string(); return $encodedText; }
public function test_string_rep() { $writers_schema_json = '"null"'; $writers_schema = AvroSchema::parse($writers_schema_json); $datum_writer = new AvroIODatumWriter($writers_schema); $strio = new AvroStringIO(); $this->assertEquals('', $strio->string()); $dw = new AvroDataIOWriter($strio, $datum_writer, $writers_schema_json); $dw->close(); $this->assertEquals(57, strlen($strio->string()), AvroDebug::ascii_string($strio->string())); $read_strio = new AvroStringIO($strio->string()); $datum_reader = new AvroIODatumReader(); $dr = new AvroDataIOReader($read_strio, $datum_reader); $read_data = $dr->data(); $datum_count = count($read_data); $this->assertEquals(0, $datum_count); }
$data_writer->append($datum); } // Tidy up $data_writer->close(); // Open $file_name (by default for reading) using the writer's schema // included in the file $data_reader = AvroDataIO::open_file($file_name); echo "from file:\n"; // Read each datum foreach ($data_reader->data() as $datum) { echo var_export($datum, true) . "\n"; } $data_reader->close(); // Create a data string // Create a string io object. $io = new AvroStringIO(); // Create a datum writer object $writers_schema = AvroSchema::parse($writers_schema_json); $writer = new AvroIODatumWriter($writers_schema); $data_writer = new AvroDataIOWriter($io, $writer, $writers_schema); foreach ($data as $datum) { $data_writer->append($datum); } $data_writer->close(); $binary_string = $io->string(); // Load the string data string $read_io = new AvroStringIO($binary_string); $data_reader = new AvroDataIOReader($read_io, new AvroIODatumReader()); echo "from binary string:\n"; foreach ($data_reader->data() as $datum) { echo var_export($datum, true) . "\n";
/** * Entry point to process one procedure call. * @param string $call_request the serialized procedure call request * @param Transceiver $transceiver the transceiver used for the response * @return string|null the serialiazed procedure call response or null if it's a one-way message * @throw AvroException */ public function respond($call_request, Transceiver $transceiver) { $buffer_reader = new AvroStringIO($call_request); $decoder = new AvroIOBinaryDecoder($buffer_reader); $buffer_writer = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($buffer_writer); $error = null; $response_metadata = array(); try { $remote_protocol = $this->process_handshake($decoder, $encoder, $transceiver); if (is_null($remote_protocol)) { return $buffer_writer->string(); } $request_metadata = $this->meta_reader->read($decoder); $remote_message_name = $decoder->read_string(); if (!isset($remote_protocol->messages[$remote_message_name])) { throw new AvroException("Unknown remote message: {$remote_message_name}"); } $remote_message = $remote_protocol->messages[$remote_message_name]; if (!isset($this->local_protocol->messages[$remote_message_name])) { throw new AvroException("Unknown local message: {$remote_message_name}"); } $local_message = $this->local_protocol->messages[$remote_message_name]; $datum_reader = new AvroIODatumReader($remote_message->request, $local_message->request); $request = $datum_reader->read($decoder); try { $response_datum = $this->invoke($local_message, $request); // if it's a one way message we only send the handshake if needed if ($this->local_protocol->messages[$remote_message_name]->is_one_way()) { return $buffer_writer->string() == "" ? null : $buffer_writer->string(); } } catch (AvroRemoteException $e) { $error = $e; } catch (Exception $e) { $error = new AvroRemoteException($e->getMessage()); } $this->meta_writer->write($response_metadata, $encoder); $encoder->write_boolean(!is_null($error)); if (is_null($error)) { $datum_writer = new AvroIODatumWriter($local_message->response); $datum_writer->write($response_datum, $encoder); } else { $datum_writer = new AvroIODatumWriter($remote_message->errors); $datum_writer->write($error->getDatum(), $encoder); } } catch (AvroException $e) { $error = new AvroRemoteException($e->getMessage()); $buffer_writer = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($buffer_writer); $this->meta_writer->write($response_metadata, $encoder); $encoder->write_boolean(!is_null($error)); $datum_writer = new AvroIODatumWriter($this->system_error_schema); $datum_writer->write($error->getMessage(), $encoder); } return $buffer_writer->string(); }
// list($headers, $content) = explode("\r\n\r\n", $response, 2); // $schema_uri = F3::get('host_uri')."/sync_schema"; // // // Deserialize the data in the Ping message's body // $content = Web::http('GET '.$schema_uri); // $schema = AvroSchema::parse($content); // $datum_reader = new AvroIODatumReader($schema); // $read_io = new AvroStringIO($content); // $decoder = new AvroIOBinaryDecoder($read_io); // $message = $datum_reader->read($decoder); // echo $message; }); F3::route('POST /shipper_to_cart', function () { $json_schema = Web::http('GET ' . F3::get('SESSION.ship_uri')); $avro_schema = AvroSchema::parse($json_schema); $datum_writer = new AvroIODatumWriter($avro_schema); $write_io = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($write_io); $ch = curl_init(); //get around php magic quotes if needed //for json_decode to work if (get_magic_quotes_gpc()) { $d = stripslashes($_POST['json_message']); } else { $d = $_POST['json_message']; } $message = json_decode($d, true); //$message = array("order" => array("orderID" => "123", "orderType" => "InHouse")); $datum_writer->write($message, $encoder); curl_setopt($ch, CURLOPT_URL, F3::get('SESSION.ship_topic')); // pure evil! just for the sake of this example. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);