commit f847d5dc017d1d90e28f62298b0ed8581d17b888 Author: Josh Sherman Date: Fri Sep 2 23:32:23 2011 -0400 Initial commit. diff --git a/README.md b/README.md new file mode 100644 index 0000000..392570a --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Ubuntu One SyncDaemon REST API Server + +## What's it do? + +Creates a RESTful interface to Ubuntu One's DBus interface that lives on port 3000 and can be utlized to build client side web applications on top of Ubuntu One. + +## Why does this exist? + +This little server is the result of fighting with the PHP DBus bindings and trying it to work in the local user space. This is a rewrite of a Node.js version I wrote previously to utilize actual DBus bindings instead of relying on parsing the output of dbus-send + +## How do I run it? + +Assuming you already have Python (and any additional libraries) on your system and are running Ubuntu (Only OS I'll be testing this one) then all you need to do is run: + + ./ubuntuone.py + +## How do I talk to it? + +Communicating with the server can be done by pointing your web browser to http://localhost:3000/status/current_status + +The format of the URI is /objectPath/method + +The data is returned as JSON + +## What's the future hold? + +* Documentation page when calling the server with no path +* Support for the root object path's methods (connect, disconnect, et cetera) +* POST methods to push as well as pull data +* Potentially spinning off this server into a more generic DBus web interface diff --git a/ubuntuone.py b/ubuntuone.py new file mode 100755 index 0000000..227e468 --- /dev/null +++ b/ubuntuone.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +from BaseHTTPServer import BaseHTTPRequestHandler +import urlparse +import dbus +import json + +bus = dbus.SessionBus() + +class GetHandler(BaseHTTPRequestHandler): + + def do_GET(self): + parts = self.path.lower().split('/') + + payload = '{"error": "invalid request"}' + + if len(parts) == 3: + interface = parts[1] + method = parts[2] + + u1_object = bus.get_object('com.ubuntuone.SyncDaemon', '/' + interface) + u1_interface = dbus.Interface(u1_object, 'com.ubuntuone.SyncDaemon.' + interface.capitalize()) + + try: + func = getattr(u1_interface, method) + except AttributeError: + payload = '{"error": "unknown method"}' + else: + payload = json.dumps(func()) + + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + self.wfile.write(payload) + return + +if __name__ == '__main__': + from BaseHTTPServer import HTTPServer + server = HTTPServer(('', 3000), GetHandler) + print 'Starting server, use to stop' + server.serve_forever()