Skip to content

Instantly share code, notes, and snippets.

@saranyan
Created February 7, 2012 18:30
Show Gist options
  • Save saranyan/1761131 to your computer and use it in GitHub Desktop.
Save saranyan/1761131 to your computer and use it in GitHub Desktop.
PHP and Ruby sample to send Avro messages to X.commerce fabric
AUTHORIZATION : Bearer QkEAARgyi3MPFsmP8St6L2Yc07DkYKQRt5MNo/7caN75haGqDTFDNH/meyYIpKa19Qt1rw==
X_XC_MESSAGE_GUID : f72b7e90-f7ee-4964-901b-6e2427a7c680
X_XC_PUBLISHER : TUQAAXOrYY3iyGbbL6IvNPGWJMaw4CrLBE7QTXIUFuJgo6gBIWPrCwOkXfhMLTbOJXAepw==
X_XC_SCHEMA_URI : https://ocl.xcommercecloud.com/marketplace/profile/deleted/1.0.0
X_XC_SCHEMA_VERSION : 1.0.0
X_XC_TENANT_ID : VEkAAalGt+ye8hDbMXeh8BAquNa72oifsoQNEtjQ9mTzZ8czA7M0+AcJQvDVVsN51DUgNw==
//PHP
//This schema is derived out of the topic and schema version.
$content = file_get_contents("https://ocl.xcommercecloud.com/marketplace/profile/delete/1.0.0");
$schema = AvroSchema::parse($content);
$message = array("xProfileID" => "123");
$datum_writer= new AvroIODatumWriter($schema);
$write_io = new AvroStringIO();
$encoder = new AvroIOBinaryEncoder($write_io);
$datum_writer->write($message,$encoder);
curl_setopt($ch, CURLOPT_POSTFIELDS, $write_io->string());
#Ruby
#get the schema
file = HTTParty.get("https://ocl.xcommercecloud.com/marketplace/profile/delete/1.0.0")
schema = Avro::Schema.parse(file.parsed_response.to_s.gsub(/\=\>/,':'))
#construct a test message
message = "{\"xProfileID\": \"123\"}"
#define a topic
topic = "marketplace/profile/delete"
#parse the schema and encode the message
stringwriter = StringIO.new
datumwriter = Avro::IO::DatumWriter.new(schema)
encoder = Avro::IO::BinaryEncoder.new(stringwriter)
datumwriter.write(JSON.parse(message),encoder)
#post to fabric
http_action = HTTParty.post("http://api.sandbox.x.com/fabric/#{topic}", {:body => stringwriter.string, :headers => {'Content-Type' => 'avro/binary', 'Authorization' => 'Bearer QUkAAV4C7dkO5NH/c1IGdFeZeaqMbeiZe1yoGp...', 'X-XC-SCHEMA-VERSION' => "1.0.0"}})
//PHP Version
//send the test message to the local fabric
$ch = curl_init();
// The URL is the host:port for the XFabric, plus the topic
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.x.com/marketplace/profile/delete");
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: avro/binary", "Authorization: Bearer QUkAAV4C7dkO5NH/c1IGdFeZeaqMbeiZe1yoGp...","X-XC-SCHEMA-VERSION: 1.0.0"));
//PHP example
//get it from X-XC-SCHEMA-URI, the default location
$headers = getallheaders();
$content = file_get_contents($headers['X-XC-SCHEMA-URI']);
//parse the conent
$schema = AvroSchema::parse($content);
//the request body contains the message
$post_data = file_get_contents("php://input");
$schema = AvroSchema::parse($content);
$datum_reader = new AvroIODatumReader($schema);
$read_io = new AvroStringIO($post_data);
$decoder = new AvroIOBinaryDecoder($read_io);
//read the message
$message = $datum_reader->read($decoder);
#Ruby example
#hash of all HTTP headers
for header in request.env.select {|k,v| k.match("^HTTP.*") }
headers["#{header[0].split('_',2)[1]}"] = "#{header[1]}"
end
#get the schema from the specific header
file = HTTParty.get("#{headers["X_XC_SCHEMA_URI"]}")
#Parse the schema
schema = Avro::Schema.parse(file.parsed_response.to_s.gsub(/\=\>/,':'))
stringreader = StringIO.new(request.body.string)
decoder = Avro::IO::BinaryDecoder.new(stringreader)
datumreader = Avro::IO::DatumReader.new(schema)
#read the message
read_value = datumreader.read(decoder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment