wiki:UserGuidelinesREST

REST Interface

Resources are accessible via RESTful URIs to allow the creation of Mash-ups:

JSON uploads should be PUT to http://server.domain/eden/module/resource.json

NB JSON must not include trailing commas in lists (just like Internet Explorer). [This is a SimpleJSON limitation]

Testing

RESTclient

Use pre-emptive authentication, when you need to login.

AJAX

Authenticate using:

function make_base_auth(user, password) {
 var tok = user + ':' + pass;
 var hash = Base64.encode(tok);
 return 'Basic ' + hash;
}

var auth = make_basic_auth('username@example.com', 'password');
var url = 'http://host.domain/eden/controller/function?vars';

// RAW
request = new XMLHttpRequest();
request.setRequestHeader('Authorization', auth);
request.open('PUT', url, true); // async=true
request.send(bodyContent);

// ExtJS
Ext.Ajax.request({
    url : url,
    method : 'GET',
    headers : { Authorization : auth }
});

// jQuery
$.ajax({
    url : url,
    method : 'GET',
    beforeSend : function(req) {
        req.setRequestHeader('Authorization', auth);
    }
});

Thanks to:

CURL

For testing/PUT using CURL follow the example

In the example - name%40example.com represents your login name@example.com and password is to be replaced by your password.

person.xml is the file name to PUT

curl -H 'content-type:application/xml' -T person.xml http://name%40example.com:password@localhost:8000/sahana/pr/person.xml&ignore_errors=False

WGET

Use the  --auth-no-challenge option in order to login to Eden.

WordPress integration

Courtesy of timClicks. This example is for HumanityRoad's WordPress to pull in Hospitals data from Sahana Eden:

<?php
$download_error = False;
$hospital_source_file = 'http://humanityroad.sahanafoundation.org/eden/hms/hospital.json';

$hospital_table_format = <<<HOSPITAL_TABLE_ROWS
	<tr class="hospital-info">
		<td>
			<h4><a href="%s">%s</a></h4>
			<p>%s<br />%s</p>
		</td>
		<td>
			<h4>&nbsp;</h4>
			<dl class="hospital-d">
				<dt>Phone</dt>
				<dd>%s</dd>
				<dt>Website:</dt>
				<dd>%s</dd>
				<dt>Email:</dt>
				<dd>%s</dd>
			</dl>
		</td>
		<td>
		<h4>Location</h4>
			<dl class="hospital-d">
				<dt>Latitude</dt>
				<dd>%s</dd>
				</dt>Longitude</dt>
				<dd>%s</dd>
			</dl>
		</td>
	</tr>
	<tr class="hospital-comments">
		<td colspan="3">
			<dl class="hospital-d">
				<dt>Comments:</dt>
				<dd>%s</dd>
			</dl>
		</td>
	</tr>
	<tr class="hospital-tech-details">
		<td colspan="3">
			<span>Modified by:</span>
			<span>%s</span> | 
			<span>Last Modified</span>
			<span>%s</span> | 
			<span>Global Identifier:</span>
			<span>%s</span>
		</td>
	</tr>
	<tr>
		<td colspan="3"><hr /></td>
	</tr>
HOSPITAL_TABLE_ROWS;


$head = <<<HEAD
<!DOCTYPE html><html><head>

<style>
td {
	margin: 1em 0;
	padding: 0;
}

dt {
	position: relative;
	left: 0;
	top: 1.1em;
	width: 5em;
	font-weight: bold;
	font-family:monospace;
}

dl.hospital-d dd {
	border-left: 1px solid #000;
	margin: 0 0 0 6em;
	padding: 0 0 .5em .5em;
}

.hospital-tech-details {
	color:#aaa;
	font-size:0.8em;
}
</style>

</head><body>
HEAD;

function process_comments($comment_string){
	$a = explode("\r\n", $comment_string);
	foreach($a as &$para){
		$para = $para.'<br />';
	};
	return '<p>'.implode($a).'</p>';
};

// untested
function check_and_set($item, $assoc_array) {
	if (array_key_exists($item, $assoc_array)) {
		$val = $assoc_array[$item];
	} else {
		$val = '&nbsp;';
	};
	return $val;
};

function process_hospital_info($hospital) {
	global $hospital_table_format;

	$city = check_and_set('city', $hospital);
	$website = check_and_set('website', $hospital);
	$name = check_and_set('name', $hospital);
	$address = check_and_set('address', $hospital);
	$phone = check_and_set('phone', $hospital);
	$email = check_and_set('email', $hospital);
	$lat = check_and_set('@lat', $hospital['$k_location_id']);
	$lon = check_and_set('@lon', $hospital['$k_location_id']);
	$comments = check_and_set('comments', $hospital);
	$modified_by = check_and_set('@modified_by', $hospital);
	$last_modified = check_and_set('@modified_on', $hospital);
	$uuid = check_and_set('@uuid', $hospital);

	return sprintf($hospital_table_format, $website, $name, $address, $city, $phone, $website, $email, $lat, $lon, $comments, $modified_by, $last_modified, $uuid);
};



echo $head;
echo '<h3>yo!</h3>';

$hospital_json = file_get_contents($hospital_source_file);
if ($hospital_json === False) {
	$download_error = True;
} else {
	$hospitals = json_decode($hospital_json, True);
};

foreach ($hospitals['$_hms_hospital'] as &$hospital) {
	$hospital_table[] = process_hospital_info($hospital);
}
print '<table border="0" style="padding:3px;">';
print implode($hospital_table);
print '</table>';

echo '</body>';
?>

UserGuidelines

Last modified 11 years ago Last modified on 10/04/13 13:27:42
Note: See TracWiki for help on using the wiki.