@@ -0,0 +1,2416 @@
+/**%0A * %0A */%0Apackage io.spire.examples;%0A%0Aimport java.io.IOException;%0Aimport java.util.Date;%0A%0Aimport io.spire.Spire;%0Aimport io.spire.api.*;%0Aimport io.spire.api.Message.MessageOptions;%0Aimport io.spire.request.ResponseException;%0A%0A/**%0A * @author Jorge Gonzalez%0A *%0A */%0Apublic class SpireApi %7B%0A%0A%09/**%0A%09 * @param args%0A%09 * @throws IOException %0A%09 * @throws ResponseException %0A%09 */%0A%09public static void main(String%5B%5D args) throws ResponseException, IOException %7B%0A%09%09/* This is just for this test. %0A%09%09 * We don't really want to be creating accounts every time, right?%0A%09%09 */%0A%09%09String spireUrl = %22http://build.spire.io/%22;%0A%09%09%0A%09%09/* Setup%0A%09%09 * %0A%09%09 * Normally you would only need to do this:%0A%09%09 * Spire spire = new Spire(); %0A%09%09 */%0A%09%09Spire spire = new Spire(spireUrl);%0A%09%09%0A%09%09Date now = new Date();%0A%09%09long timestamp = now.getTime();%0A%09%09String email = String.format(%22spireNoob%25d@noobspiretest.com%22, timestamp);%0A%09%09String password = %22spire.io.password%22;%0A%09%09%0A%09%09// Register - creates account%0A%09%09spire.register(email, password);%0A%09%09String accounKey = spire.getAccountKey();%0A%09%09%0A%09%09/* %0A%09%09 * Here is how you can use the Api class%0A%09%09 * Can also do:%0A%09%09 * %0A%09%09 * %09Api api = Spire.SpireFactory.createApi();%0A%09%09 * %0A%09%09 * which points to the default Api Url%0A%09%09 */%0A%09    Api api = new Api(spireUrl);%0A%09    api.discover();%0A%09    // create a session%0A%09    Session session = api.createSession(accounKey);%0A%09    // retrieve channels%0A%09    session.channels();%0A%09    // channels are now cached. Now, try looking for channel by name%0A%09    Channel channel = session.getChannels().getChannel(%22foo%22);%0A%09    %0A%09    if(channel == null)%7B%0A%09    %09try %7B%0A%09    %09%09// if the channel named %22foo%22 already exists, raises an error.%0A%09%09%09%09channel = session.createChannel(%22foo%22);%0A%09%09%09%7D catch (ResponseException e) %7B%0A%09%09%09%09e.getResponse().ignore();%0A%09%09%09%09channel = session.getChannels().getChannel(%22foo%22);%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09    %7D%0A%09    %0A    %09Subscription subscription = session.createSubscription(%22subscriptionName%22, %22foo%22);%0A    %09channel.publish(%22message content%22);%0A    %09Events events = subscription.retrieveMessages();%0A    %09int lastMessageIndex = events.getMessages().size() - 1;%0A    %09String lastTimestamp = events.getMessages().get(lastMessageIndex).getTimestamp();%0A    %09%0A    %09channel.publish(%22another message%22);%0A    %09MessageOptions options =  new MessageOptions();%0A    %09options.timestamp = lastTimestamp;%0A    %09events = subscription.retrieveMessages(options);%0A%09%7D%0A%0A%7D%0A

use Spire Api example