REST API Examples

PHP

<?php

require_once('OAuth.php');

$consumer_key = 'anonymous';
$consumer_secret = 'anonymous';
$oauth_key = 'ACCESS_TOKEN';
$oauth_secret = 'ACCESS_SECRET';
$app_id = "stashboard";

$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$token = new OAuthToken($oauth_key, $oauth_secret);

// Set up a request function
function request($consumer, $token, $url, $method = "GET", $data = null) {

	$sign = new OAuthSignatureMethod_HMAC_SHA1();
	$request = OAuthRequest::from_consumer_and_token(
		$consumer, $token, $method, $url, $data
	);

	$request->sign_request($sign, $consumer, $token);
	$ch = curl_init($request->get_normalized_http_url());

	if ($method == "POST") {
		curl_setopt($ch, CURLOPT_POST ,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS , $request->to_postdata());
	}

	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
	curl_setopt($ch, CURLOPT_HEADER ,0);  // DO NOT RETURN HTTP HEADERS

	return curl_exec($ch);
}

// Fill in your website
$base_url = "https://$app_id.appspot.com/admin/api/v1";

$data = array(
				"name" => "An Example Service",
				"description" => "An example service, created "
					. "using the StashBoard API",
			);

$r = request($consumer, $token, $base_url . "/services", "POST", $data);
$service = json_decode($r);

// GET the list of possible status images
$r = request($consumer, $token, $base_url . "/status-images", "GET");
$data = json_decode($r);
$images = $data->images;

// Pick a the first image
$image = $images[0];

// POST to the Statuses Resources to create a new Status
$data = array(
				"name" => "Example Status",
				"description" => "An example status, means nothing",
				"level" => "NORMAL",
				"image" => $image->name,
			);

$r = request($consumer, $token, $base_url . "/statuses", "POST", $data);
$status = json_decode($r);

// Create a new event with the given status and given service
$data = array(
				"message" => "Our first event! So exciting",
				"status" => $status->id,
			);

$r = request($consumer, $token, $service->url . "/events", "POST", $data);
$event = json_decode($r);

print_r($event);

Ruby

require 'rubygems'
require 'oauth'
require 'json'

oauth_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
oauth_secret = 'YYYYYYYYYYYYYYYYYYYYYYYY'

# Fill in your website
base = "https://stashboard.appspot.com"

@consumer=OAuth::Consumer.new "anonymous",
                              "anonymous",
                              {:site=>base}

@token = OAuth::AccessToken.new(@consumer, oauth_key, oauth_secret)

# POST to the Services Resource to create a new service. Save the response for
# later
@response = @token.post("/admin/api/v1/services", {
    :name => "An Example Service",
    :description => "An example service, created using the StashBoard API",
})
srvice = JSON.parse(@response.body)

# GET the list of possible status images
@response = @token.get("/admin/api/v1/status-images")
data = JSON.parse(@response.body)
images = data['images']

# Pick a random image for our status
image = images[rand(images.length)]


# POST to the Statuses Resources to create a new Status
@response = @token.post("/admin/api/v1/statuses", {
    :name => "Maintenance",
    :description => "The web service is under-going maintenance",
    :level => "WARNING",
    :image => image["name"],
})

status = JSON.parse(@response.body)

@response = @token.post("/admin/api/v1/services/" + srvice["id"] + "/events", {
    :message => "Our first event! So exciting",
    :status => status["id"],
})
event = JSON.parse(@response.body)

puts event

Python

import oauth2 as oauth
import json
import urllib
import unittest
import random

# Stashboard application id
app_id = "stashboard-hrd"

# These keys can be found at /admin/credentials
consumer_key = 'anonymous'
consumer_secret = 'anonymous'
oauth_key = 'ACCESS_TOKEN'
oauth_secret = 'ACCESS_SECRET'

# Create your consumer with the proper key/secret.
# If you register your application with google, these values won't be
# anonymous and anonymous.
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
token = oauth.Token(oauth_key, oauth_secret)

# Create our client.
client = oauth.Client(consumer, token=token)

# Base url for all rest requests
base_url = "https://%s.appspot.com/admin/api/v1" % app_id


# CREATE a new service
data = urllib.urlencode({
        "name": "Generic Web Service",
        "description": "An example web service or REST API",
        })

resp, content = client.request(base_url + "/services",
                               "POST", body=data)
service = json.loads(content)

# GET the list of possible status images
resp, content = client.request(base_url + "/status-images", "GET")
data = json.loads(content)
images = data["images"]

# Pick a random image for our status
image = random.choice(images)

# POST to the Statuses Resources to create a new Status
data = urllib.urlencode({
    "name": "Maintenance",
    "description": "The web service is under-going maintenance",
    "image": image["name"],
    "level": "WARNING",
})

resp, content = client.request(base_url + "/statuses", "POST", body=data)
status = json.loads(content)

# Create a new event with the given status and given service
data = urllib.urlencode({
    "message": "Our first event! So exciting",
    "status": status["id"],
})

resp, content = client.request(service["url"] + "/events", "POST", body=data)
print json.loads(content)