SMS-System

Server (handler.py):

from simpleapi import Namespace, serialize from models import SMS, APIUser class SMSAPI(Namespace): __authentication__ = lambda namespace, access_key: \ APIUser.objects.filter(access_key=access_key).count() > 0 def send(self, to, msg, from='testsender'): sms = SMS.objects.create( to=to msg=msg, from=from ) return { 'sent': sms.send(), 'obj': serialize(sms, excludes=[re.compile('^date'),]) } send.published = True send.constraints = {'to': re.compile(r'^\+\d{2,}\ \d{3,}\ \d{5,}')} def status(self, id): return SMS.objects.get(id=id) status.published = True status.constraints = {'id': int} def last(self, numbers=5): return SMS.objects.all()[:numbers] last.published = True last.constraints = {'numbers': int}

Standalone-Server (app.py):

from simpleapi import Route from handlers import SMSAPI route = Route(SMSAPI, framework='standalone', path=r'^/api/') route.serve() # serves on port 5050 by default

Gunicorn (WSGI-compatible implementation) (app.py):

from simpleapi import Route from handlers import SMSAPI route = Route(SMSAPI, framework='wsgi', path=r'^/api/') # start Gunicorn (with 5 workers): # gunicorn -w 5 app:route

Django-Server (urls.py):

from handlers import SMSAPI urlpatterns = patterns('', (r'^api/$', Route(SMSAPI)) )

Flask-Server (app.py):

from flask import Flask from simpleapi import Route from handlers import SMSAPI app = Flask(__name__) app.route('/api/')(Route(SMSAPI, framework='flask')) if __name__ == '__main__': app.run()

Google AppEngine (main.py):

from google.appengine.ext import webapp from google.appengine.ext.webapp import util from simpleapi import Route from handlers import SMSAPI def main(): application = webapp.WSGIApplication( [('/api/', Route(SMSAPI, framework='appengine'))] ) util.run_wsgi_app(application) if __name__ == '__main__': main()

Client (python/remote):

from simpleapi import Client client = Client(ns='http://remote.tld:8888/api/', access_key='mysecret', transport_type='xml') sms = client.sms(to='555123', msg='Hey yo! This is simpleapi calling.') print "Sent successful?", sms['sent'] sms = client.sms(to='555123', msg='2nd test with own sender', sender='simpleapi') print "Sent successful?", sms['sent'] print "Which sender?", sms['obj']['sender']

Client (python/local):

from simpleapi import DummyClient, Route from handlers import SMSAPI client = DummyClient(Route(SMSAPI, framework='dummy'), access_key='mysecret') sms = client.sms(to='555123', msg='Hey yo! This is simpleapi calling.') print "Sent successful?", sms['sent'] sms = client.sms(to='555123', msg='2nd test with own sender', sender='simpleapi') print "Sent successful?", sms['sent'] print "Which sender?", sms['obj']['sender']

Client (PHP):

require_once("class.client.php"); $client = new Client($ns="http://localhost:8888/api/", $access_key='mysecret'); print("Sent? ".$client->sms(array( 'to' => '555123', 'msg' => 'Hey yo! This is the PHP client sending you a SMS.' ))->{'sent'});

Client (jQuery):

jQuery.get( "/api/", {_call: 'send', to: '555123', 'msg': 'Hey ya!'}, function (return) { if (return.result.sent) alert('Sent successfully!'); else alert('Sending failed!'); } )