Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

reabstraction for SBA

  • Loading branch information...
commit e6c9166eea85564e0c2fe237079e5beeecee2e12 1 parent b29f142
@johnfn johnfn authored
Showing with 64 additions and 13 deletions.
  1. +3 −1 broadband_api.py
  2. +45 −12 generic_api.py
  3. +16 −0 sba_businesses_api.py
View
4 broadband_api.py
@@ -2,9 +2,11 @@
# Simple Python wrapper around the Broadband API provided by the FCC.
+APIS = [("get_data", "http://data.fcc.gov/api/speedtest/find")]
+
class BroadbandApi(GenericAPI):
def __init__(self):
- GenericAPI.__init__(self, [("get_data", "http://data.fcc.gov/api/speedtest/find")])
+ GenericAPI.__init__(self, APIS)
# Sample use of BroadbandApi
View
57 generic_api.py
@@ -9,15 +9,30 @@ class BadJSONException(Exception):
def __str__(self):
return "The returned JSON was invalid."
+class NonexistentStyleException(Exception):
+ def __str__(self):
+ return "The requested API style was invalid."
+
# Perform requests to generic APIs.
+PHP_STYLE = 0 # &x=1&y=2
+SBA_WEIRD_STYLE = 1
+
class BaseAPIRequest:
def __init__(self, url):
self.url = url
- # Formats a URL with the provided keyword arguments.
- def format_url(self, **args):
+ if self.url.startswith("http://api.sba.gov") or self.url.startswith("api.sba.gov"):
+ self.api_style = SBA_WEIRD_STYLE
+ else:
+ self.api_style = PHP_STYLE
+
+ def __format_url_php_style(self, *ordered_args, **args):
+ """
+ Format the url with the arguments in PHP style. That is,
+ ?x=5&y=6
+ """
if args is None: raise NoArgumentsException
args["format"] = "json" #Ask for JSON formatted response
@@ -28,9 +43,25 @@ def format_url(self, **args):
append = append[:-1] #Take off last '&'
self.formatted_url = self.url + "?" + append
+ def __format_url_weird_style(self, *ordered_args, **args):
+ """
+ Format the url with the arguments in the weird style given by SBA.
+ """
+
+ self.formatted_url = self.url + args["type"] + ".json"
+
+ # Formats a URL with the provided keyword arguments.
+ def format_url(self, *ordered_args, **args):
+ if self.api_style == PHP_STYLE:
+ self.__format_url_php_style(*ordered_args, **args)
+ elif self.api_style == SBA_WEIRD_STYLE:
+ self.__format_url_weird_style(*ordered_args, **args)
+ else:
+ raise NonexistentStyleException
+
# Requests the API and returns the JSON object.
- def request(self, **args):
- self.format_url(**args)
+ def request(self, *ordered_args, **args):
+ self.format_url(*ordered_args, **args)
#print self.formatted_url
t = urllib.urlopen(self.formatted_url).read().strip()
if t.startswith("callback("):
@@ -46,13 +77,15 @@ def request(self, **args):
class GenericAPI:
- # __init__
- # Parameters: APIS, a list of tuples of form (FUNCTIONNAME, LINK).
- #
- # Creates functions of name FUNCTIONNAME that perform an API call to LINK
- # when called, giving back the response as JSON.
- #
- # Returns: Nothing
+ """
+ __init__
+ Parameters: APIS, a list of tuples of form (FUNCTIONNAME, LINK).
+
+ Creates functions of name FUNCTIONNAME that perform an API call to LINK
+ when called, giving back the response as JSON.
+
+ Returns: Nothing
+ """
def __init__(self, apis):
@@ -73,7 +106,7 @@ def __init__(self, apis):
# Creates a function on the current class.
def bind_closure(self, number):
- def generic_api_call(**kwargs):
+ def generic_api_call(*ordered_args, **kwargs):
self.api_objects[number].format_url(**kwargs)
return self.api_objects[number].request(**kwargs)
View
16 sba_businesses_api.py
@@ -0,0 +1,16 @@
+from generic_api import *
+
+# Simple Python wrapper around the Broadband API provided by the FCC.
+
+APIS = [ ("get_licenses", "http://api.sba.gov/license_permit/" )
+ ]
+
+class SBABusinessesApi(GenericAPI):
+ def __init__(self):
+ GenericAPI.__init__(self, APIS)
+
+
+# Sample use of BroadbandApi
+if __name__ == "__main__":
+ bb = BroadbandApi()
+ print bb.get_data(latitude=37, longitude=-122) # (Should be San Francisco)
Please sign in to comment.
Something went wrong with that request. Please try again.