Client Implementations

Examples of client-side code for interacting with Tasty. If you have improvements on these, or additional examples in languages not covered here, please submit them.

commandline

curl is very useful for testing out ideas from the command line, particularly with its -X flag. eg,

curl -X POST -d "" http://localhost:9980/service/test/user/bob/item/foo/tag/blah/
curl -X DELETE http://localhost:9980/service/test/user/bob/item/foo/tag/blah/

python

import httplib, urllib
from simplejson import loads as json_to_py



def get_tags(user,item):
    url = "http://tasty.example.com/service/myservice/user/%s/item/%s/" % (user,item)
    try:
        return [t['tag'] for t in json_to_py(urllib.urlopen(url).read())['tags']]
    except json.ReadException:
        return []



def add_tag(user,item,tag):
    conn = httplib.HTTPConnection("tasty.example.com")
    url = "/service/myservice/user/%s/item/%s/tag/%s/" % (user,item,tag)
    conn.request("POST",url)
    response = conn.getresponse()
    conn.close()

sometimes, adding or deleting does not need to be done synchronously. if that's the case, you can eliminate http latency from bogging you down by spawning a new thread to make the request.

from threading import Thread


class Putter(Thread):
    def __init__(self,url):
        self.url = url
    def run(self):
        conn = httplib.HTTPConnection("tasty.example.com")
        conn.request("POST",self.url)
        r = conn.getresponse()
        conn.close()

def add_tag(user,item,tag):
    p = Putter("/service/myservice/user/%s/item/%s/tag/%s/" % (user,item,tag))
    p.start()


but i recommend trying it first without the thread. the performance hit of making an http request to a (presumably) local service may be surprisingly small. a Twisted implementation would probably be even faster and cleaner, but i don't really know twisted very well. sample twisted client code is welcome.

also of interest may be this sample implementation of cloud scaling.

perl

my $TASTY_BASE = "tasty.example.com";
my $TASTY_SERVICE = "example";



sub tasty_get {
    use LWP::Simple;
    use JSON;
    my $url = shift;
    my $full = "http://${TASTY_BASE}/service/${TASTY_SERVICE}/$url";
    my $r = get $full;
    my $json = new JSON;
    my $obj = $json->jsonToObj($r);
    if (!$obj) {
        $obj = {};
    }
    return $obj;
}



use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw(POST);

sub tasty_put {
    my $url = shift;
    my $ua = LWP::UserAgent->new;
    my $req = POST "http://${TASTY_BASE}/service/${TASTY_SERVICE}/$url", [];
    return $ua->request($req)->as_string;
}



sub tasty_delete {
    my $url = shift;
    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(DELETE => "http://${TASTY_BASE}/service/${TASTY_SERVICE}/$url");
    my $res = $ua->request($req);
}


java

TODO

PHP

TODO

JavaScript

TODO