Created
May 25, 2011 21:32
-
-
Save jeffreyolchovy/992041 to your computer and use it in GitHub Desktop.
Primitive Example of PubSub using the Flow Platform for Feedgen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, json | |
import flow | |
class Context(object): | |
def __init__(self, client): | |
client.set_logger_file('fgc.out') | |
self.client = client | |
self.create_application() | |
self.create_application_flows() | |
def create_application(self): | |
application = flow.Application( | |
name='fgc', description='...', email='[email protected]', | |
url='http://fgc.com') | |
self.application = self.client.save(application) | |
def create_application_flows(self): | |
categories = flow.Bucket( | |
name='root', description='...', | |
path='/apps/%s/categories' % self.application.name) | |
vendors = flow.Bucket( | |
name='vendors', description='...', | |
path='/apps/%s/vendors' % self.application.name) | |
leads = flow.Bucket( | |
name='leads', description='...', | |
path='/apps/%s/leads' % self.application.name) | |
self.client.save(categories) | |
self.client.save(vendors) | |
self.client.save(leads) | |
def create_category(self, name): | |
buk = flow.Bucket( | |
name=name, description='...', | |
path='/apps/%s/categories/%s' % (self.application.name, name)) | |
return self.client.save(buk) | |
def create_categories(self, categories=[]): | |
return [self.create_category(c) for c in categories] | |
def create_vendor(self, name, categories=[]): | |
buk = flow.Bucket( | |
name=name, description='...', | |
path='/apps/%s/vendors/%s' % (self.application.name, name)) | |
buk = self.client.save(buk) | |
for c in categories: | |
track = flow.Track(** | |
{'to': buk.path, | |
'from': '/apps/%s/categories/%s' % (self.application.name, c)}) | |
self.client.save(track) | |
return buk | |
def create_lead(self, categories=[]): | |
leads = [] | |
for c in categories: | |
drop = flow.Drop( | |
path='/apps/%s/categories/%s' % (self.application.name, c), | |
elems={}) | |
leads.append(self.client.save(drop)) | |
return leads | |
def vendor_has_lead(self, vendor, lead): | |
response = json.loads(self.client.http_get('/drop/%s' % vendor.id)) | |
drops = response['body'] | |
for l in lead: | |
for d in drops: | |
if d['id'] == l.id: return True | |
else: | |
return False | |
if __name__ == '__main__': | |
KEY = sys.argv[1] | |
SECRET = sys.argv[2] | |
ACTOR = '000000000000000000000001' | |
client = flow.RestClient(KEY, SECRET, ACTOR) | |
ctx = Context(client) | |
categories = ctx.create_categories(['a', 'b', 'c']) | |
vendor_a = ctx.create_vendor('vendor_a', ['a', 'b']) | |
vendor_b = ctx.create_vendor('vendor_b', ['c']) | |
lead_a = ctx.create_lead(['a']) | |
lead_b = ctx.create_lead(['b', 'c']) | |
if not ctx.vendor_has_lead(vendor_a, lead_a): | |
raise Exception('Vendor A should have received Lead A') | |
if not ctx.vendor_has_lead(vendor_a, lead_b): | |
raise Exception('Vendor A should have received Lead B') | |
if not ctx.vendor_has_lead(vendor_b, lead_b): | |
raise Exception('Vendor B should have received Lead B') | |
if ctx.vendor_has_lead(vendor_b, lead_a): | |
raise Exception('Vendor B should NOT have received Lead A') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expect a more complex example that deals with: publishing-data-to and retrieving-data-from a hierarchical structure of categories, and a single point of entry for "lead" drops to eliminate the overhead of dealing with duplicate leads upon propagation.
The Flow Platform Python Client Library was used in this example. The source for the flow module can be found at https://github.com/jeffreyolchovy/Flow-Python-Client/blob/master/flow.py.