Commits

Albert O'Connor committed 1f31a9f

Initial checkin of uwaterlooapi.

  • Participants

Comments (0)

Files changed (5)

+syntax: glob
+.DS_Store
+*.pyc
+build/*
+dist/*
+*egg-info*
+uwaterlooapi README
+===================
+
+Thin Python wrapper for the University of Waterloo Open Data API. The API is documented here: http://api.uwaterloo.ca/
+
+NOTE: This implementation is not yet complete! But adding new methods is easy.
+
+Install
+-------
+
+Using pip::
+
+pip install -e hg+https://bitbucket.org/amjoconn/uwaterlooapi
+
+Usage
+-----
+  
+Using Weather as an example::
+
+>>> from uwaterlooapi import UWaterlooAPI
+>>> api = UWaterlooAPI(api_key="YOU API KEY HERE")
+>>> api.weather()
+Returns weather json
+
+Any positional or unrecongized keyword arguments will be ignored. If "output" or "callback" keyword arguments are provided the raw response is returned.
+
+# Use setuptools if we can
+try:
+    from setuptools.core import setup
+except ImportError:
+    from distutils.core import setup
+
+setup(
+    name='uwaterlooapi',
+    version="0.9.0",
+    description='Thin library wrapper for the University of Waterloo Open Data API.',
+    long_description='See http://api.uwaterloo.ca/ for a descripton of the API.',
+    author='Albert O\'Connor',
+    author_email='info@albertoconnor.ca',
+    url='https://bitbucket.org/amjoconn/uwaterlooapi',
+    download_url='',
+    classifiers=[
+        "Development Status :: 3 - Alpha",
+        "License :: OSI Approved :: BSD License",
+        "Operating System :: OS Independent",
+        "Topic :: Software Development"
+    ],
+    packages=[
+        'uwaterlooapi'
+    ],
+)

File uwaterlooapi/__init__.py

+
+from api import UWaterlooAPI

File uwaterlooapi/api.py

+
+import new
+import urllib
+import json
+
+class UWaterlooAPI(object):
+    def __init__(self, api_key=None, url="http://api.uwaterloo.ca/public/v1/"):
+        self.api_key = api_key
+        self.url = url
+        
+    @classmethod
+    def _register(cls, api_call, name=None):
+        method = new.instancemethod(api_call, None, cls)
+        if not name: name = api_call.__name__
+        setattr(cls, name, api_call)
+    
+class APIFunction(object):
+    method = None
+    
+    def __init__(self, api, args, kwargs):
+        self.api = api
+        self.args = args
+        self.kwargs = kwargs
+
+        if "output" in kwargs or "callback" in kwargs:
+            self.explicit = True
+        else:
+            self.explicit = False
+
+        self.response = None
+    
+    def encode(self):
+        params = self.kwargs
+        # Overwite key and service
+        params["key"] = self.api.api_key
+        params["service"] = self.method
+        if "output" not in params:
+            params["output"] = "json"
+        return urllib.urlencode(params)
+    
+    def execute(self):
+        if self.method is None:
+            raise NotImplementedError, u'Subclass of APIFunction needs to define a valid "method" attribute.'
+        
+        request = "%s?%s" % (self.api.url, self.encode())
+        self.response = urllib.urlopen(request).read()
+        if self.explicit:
+            return self.response
+        else:
+            return json.loads(self.response)['response']['data'] # Unwrap automatically         
+
+def binder(function_class):
+    closed = function_class
+    def _bound(api, *args, **kwargs):
+        func = closed(api, args, kwargs)
+        return func.execute()
+    _bound.__name__ = function_class.__name__
+    return _bound
+
+def bind(cls):
+    UWaterlooAPI._register(binder(cls))
+    return cls
+
+# Geolocation
+
+@bind
+class buildings(APIFunction):
+    method = "Buildings"
+
+@bind
+class parking(APIFunction):
+    method = "ParkingList"
+
+@bind
+class watpark(APIFunction):
+    method = "WatPark"
+
+# Events
+
+@bind
+class daily_events(APIFunction):
+    method = "Events"
+
+@bind
+class calendar_events(APIFunction):
+    method = "CalendarEvents"
+
+@bind
+class university_holidays(APIFunction):
+    method = "Holidays"
+
+# I got tired...
+
+@bind
+class weather(APIFunction):
+    method = "weather"
+