Created
March 30, 2012 15:08
-
-
Save saranyan/2252166 to your computer and use it in GitHub Desktop.
HT build a X.commerce capability PingPong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//get schema for ping | |
$schema_uri = "https://api.x.com/ocl/message/ping/1.0.0"; | |
$content = file_get_contents($schema_uri); | |
$schema = AvroSchema::parse($content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#get schema for ping | |
file = HTTParty.get("https://api.x.com/ocl/message/ping/1.0.0") | |
schema = Avro::Schema.parse(file.parsed_response.to_s.gsub(/\=\>/,':')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//php - define a JSON message | |
$message = array( | |
"payload" => "test" | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ruby - define a JSON message | |
message = {"payload" => "test"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
type: "record", | |
name: "Ping", | |
version: "1.0.0", | |
topic: "/message/ping", | |
namespace: "com.x.system", | |
fields: [ | |
{ | |
name: "payload", | |
type: [ | |
"null", | |
"string" | |
], | |
default: null | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$datum_writer = new AvroIODatumWriter($schema); | |
$write_io = new AvroStringIO(); | |
$encoder = new AvroIOBinaryEncoder($write_io); | |
$datum_writer->write($message, $encoder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stringwriter = StringIO.new | |
datumwriter = Avro::IO::DatumWriter.new(schema) | |
encoder = Avro::IO::BinaryEncoder.new(stringwriter) | |
datumwriter.write(message,encoder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.x.com/fabric/message/ping"); | |
#make sure you are handling ssl certification if needed | |
#that code has been omitted for brevity | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: avro/binary", "Authorization: Bearer ZN7UiioumrxSlbS1qdzbu0GH32mJIP/1vZWugLP/eOGonDYqcTz0/+1OyNVdviaC7rwkF9pP","X-XC-DESTINATION-ID: 24+4FfGxHYE+KsK6IVFMU0F7wLUYc+hy3mIGC92zF8eC7raTQ3pa3l6L3IE/PuaV92gz4eZc","X-XC-SCHEMA-VERSION: 1.0.0")); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $write_io->string()); | |
$response = curl_exec($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fabric | |
include HTTParty | |
base_uri 'https://api.sandbox.x.com/fabric' | |
end | |
response = Fabric.post("/message/ping", \ | |
{:body => stringwriter.string, :headers => {'Content-Type' => 'avro/binary', \ | |
'Authorization' => "Bearer jS2t0mxpNWtXsoMQtcoyw2NtyDZDz+aHjuIb+z1PtEiqmPXkzEoWJH4NnBuL5MHWXI1WHyWc", \ | |
'X-XC-DESTINATION-ID' => '24+4FfGxHYE+KsK6IVFMU0F7wLUYc+hy3mIGC92zF8eC7raTQ3pa3l6L3IE/PuaV92gz4eZc',\ | |
'X-XC-SCHEMA-VERSION' => "1.0.0"}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment