From 366fefe4198ed7e18e4bb5c92efb84c26326de4b Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 15 Mar 2013 14:59:46 -0400 Subject: [PATCH] add very early start to cli, add hammock to requirements.txt --- lib/cli/__init__.py | 0 lib/cli/main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 60 insertions(+) create mode 100644 lib/cli/__init__.py create mode 100644 lib/cli/main.py diff --git a/lib/cli/__init__.py b/lib/cli/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/cli/main.py b/lib/cli/main.py new file mode 100644 index 0000000000..feb4fb5fcd --- /dev/null +++ b/lib/cli/main.py @@ -0,0 +1,59 @@ +import hammock +import os +import requests +import sys +import json + +# this is temporary +username = os.getenv("ACOM_USER","admin") +password = os.getenv("ACOM_PASS""admin") +server = os.getenv("ACOM_SERVER","127.0.0.1:8000") + +handle = hammock.Hammock("http://%s/api/v1" % server, auth=(username,password)) + +class Collection(object): + + def __init__(self, handle): + self.handle = handle + self.response = self.accessor().GET() + # TODO: error handling on non-200 + self.data = json.loads(self.response.text) + self.meta = self.data['meta'] + self.objects = self.data['objects'] + self.meta = self.data['meta'] + self.objects = self.data['objects'] + + def accessor(self): + return exceptions.NotImplementedError() + + def __iter__(self): + for x in self.objects: + yield Entry(x) + +class Entry(object): + + def __init__(self, data): + self.data = data + self.resource_uri = data['resource_uri'] + self.accessor = hammock.Hammock(self.resource_uri, auth=(username,password)) + + def __repr__(self): + return repr(self.data) + +class Organizations(Collection): + + def accessor(self): + return self.handle.organizations + +#(Epdb) got.text +#u'{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"active": true, "creation_date": "2013-03-15", "description": "testorg!", "id": 1, "name": "testorg", "resource_uri": "/api/v1/organizations/1/"}]}' +# + +try: + orgs = Organizations(handle) + for x in orgs: + print x +except requests.exceptions.ConnectionError: + print "connect failed" + sys.exit(1) + diff --git a/requirements.txt b/requirements.txt index bd7a98f671..f01880de59 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ django-tastypie==0.9.12 ipython==0.13.1 South==0.7.6 python-dateutil==1.5 +hammock