20150302 trying the new restful api for openfire 3 10 0 beta - plembo/onemoretech GitHub Wiki

title: Trying the new RESTful api for Openfire 3.10.0 Beta link: https://onemoretech.wordpress.com/2015/03/02/trying-the-new-restful-api-for-openfire-3-10-0-beta/ author: phil2nc description: post_id: 9420 created: 2015/03/02 15:26:11 created_gmt: 2015/03/02 20:26:11 comment_status: closed post_name: trying-the-new-restful-api-for-openfire-3-10-0-beta status: publish post_type: post

Trying the new RESTful api for Openfire 3.10.0 Beta

A trio of scripts used to perform RESTful operations on a Openfire 3.10.0 (Beta) server. The latest Beta version of Openfire XMPP Server (3.10.0) features a new REST API Plugin. This allows developers and administrators to perform operations on the server using HTTP gets and puts. Following are three scripts using that API, in perl, python and php. All do the same thing: get the roster for a particular XMPP user from the Openfire server for their domain. Authentication is by secret for simplicity (the plugin also will accept Basic HTTP Authentication, which is preferable for security reasons). This perl script uses Miles Crawford's REST::Client module to do its work.

#!/usr/bin/perl
use strict;
use REST::Client;
my $secret = 'xxxxxxxxxxxxxxx';
my $url = "http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster";
my $client = REST::Client->new();
$client->addHeader('Authorization', $secret);
my $response = $client->GET($url);
print $client->responseCode(), "\n";
print $client->responseContent();

__END__;

For the python variation on the same theme, I've used the urllib2 module. It's no accident that this is the shortest of the three scripts. Makes me wonder what else I could do more efficiently in python.

#!/usr/bin/python
import urllib2
secret = 'xxxxxxxxxxxxx'
url = 'http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster'
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', secret)]
response = opener.open(url).read()
print response

Although there are at least two REST client libraries for php (HTTPful and Guzzle), in the example below I've fallen back to a file_get_contents sample method I found on Decibel Developer (thus avoiding the horrors of curl). See the file_get_contents doc for background. This was the only explicitly streams oriented method I used.

<?php
$secret = 'xxxxxxxxxxxxxxx';
$url = "http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster";
$headers = array( 'http' => array( 'method' => "GET", 'header' => "Authorization: " . $secret . "\r\n" ) ); $context = stream_context_create($headers);
$response = file_get_contents($url, false, $context);
echo $response;
?>

Copyright 2004-2019 Phil Lembo