Skip to content

Commit

Permalink
Merge branch 'master' of github.com:codeforamerica/FCC-Python-Egg
Browse files Browse the repository at this point in the history
  • Loading branch information
quadrismegistus committed Apr 17, 2011
2 parents 8a1730d + 9159254 commit da648bc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
7 changes: 3 additions & 4 deletions block_conversion_api.py
Expand Up @@ -2,13 +2,12 @@

# Simple Python wrapper around the Block Conversion API provided by the FCC.

class BlockConversionAPI(GenericLatLongAPI):
class BlockConversionAPI(GenericAPI):
def __init__(self):
GenericLatLongAPI.__init__(self, "http://data.fcc.gov/api/block/find")

GenericAPI.__init__(self, [("get_block", "http://data.fcc.gov/api/block/find")])


# Sample use of BlockConversionAPI
if __name__ == "__main__":
bc = BlockConversionAPI()
print bc.request(lat=41, long=-87) # (Should be San Francisco)
print bc.get_block(lat=41, long=-87) # (Should be San Francisco)
7 changes: 3 additions & 4 deletions broadband_api.py
Expand Up @@ -2,13 +2,12 @@

# Simple Python wrapper around the Broadband API provided by the FCC.

class BroadbandApi(GenericLatLongAPI):
class BroadbandApi(GenericAPI):
def __init__(self):
GenericLatLongAPI.__init__(self, "http://data.fcc.gov/api/speedtest/find")

GenericAPI.__init__(self, [("get_data", "http://data.fcc.gov/api/speedtest/find")])


# Sample use of BroadbandApi
if __name__ == "__main__":
bb = BroadbandApi()
print bb.request(latitude=37, longitude=-122) # (Should be San Francisco)
print bb.get_data(latitude=37, longitude=-122) # (Should be San Francisco)
43 changes: 17 additions & 26 deletions generic_api.py
Expand Up @@ -10,13 +10,13 @@ def __str__(self):
return "The returned JSON was invalid."


# Inheritable class to perform requests to generic APIs.
# Perform requests to generic APIs.

class BaseAPIRequest:
def __init__(self, url):
self.url = url

#Inherit this method to describe how to format URLs.
# Formats a URL with the provided keyword arguments.
def format_url(self, **args):
if args is None: raise NoArgumentsException

Expand All @@ -36,54 +36,45 @@ def request(self, **args):
if t.startswith("callback("):
t=t[t.index("(")+1:-1]

return json.loads(t)

"""
try:
return json.loads(t)

except:
raise BadJSONException
return None
"""

#object = json.loads("".join([l for l in urllib.urlopen(self.formatted_url)]))


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

def __init__(self, apis):

self.api_objects = []
self.api_functions = []
self.functions = []

#Who writes normal functions when you can use CLOSURES?!?
# Who writes normal functions when you can use CLOSURES?!?

number = 0

# Bind each function to the class.
for api in apis:
self.api_objects.append(BaseAPIRequest(api[1]))
self.bind_closure(number)
setattr(self, api[0], self.api_functions[number])
number += 1

# Creates a function on the current class.
def bind_closure(self, number):
def generic_api_call(**kwargs):
self.api_objects[number].format_url(**kwargs)
return self.api_objects[number].request(**kwargs)

self.api_functions.append(generic_api_call)






# Wraps any API that just uses Latitude and Longitude.

#TODO: This is no longer necessary...
class GenericLatLongAPI(BaseAPIRequest):
def __init__(self, url):
self.url = url

def request(self, **args):
return BaseAPIRequest.request(self, **args)
self.api_functions.append(generic_api_call)
15 changes: 12 additions & 3 deletions license_view_api.py
Expand Up @@ -2,12 +2,21 @@

# Simple Python wrapper around the License View API provided by the FCC.


apis = [ ("get_licenses", "http://data.fcc.gov/api/license-view/basicSearch/getLicenses" )
APIS = [ ("get_licenses", "http://data.fcc.gov/api/license-view/basicSearch/getLicenses" )
, ("get_common_names", "http://data.fcc.gov/api/license-view/licenses/getCommonNames")
, ("get_statuses", "http://data.fcc.gov/api/license-view/licenses/getStatuses")
, ("get_categories", "http://data.fcc.gov/api/license-view/licenses/getCategories")
, ("get_entities", "http://data.fcc.gov/api/license-view/licenses/getEntities")
, ("get_renewals", "http://data.fcc.gov/api/license-view/licenses/getRenewals")
, ("get_issued", "http://data.fcc.gov/api/license-view/licenses/getIssued")
]


class LicenseViewAPI(GenericAPI):
def __init__(self):
GenericAPI.__init__(self, APIS)

# Sample use of BlockConversionAPI
if __name__ == "__main__":
bc = GenericAPI(apis)
bc = LicenseViewAPI()
print bc.get_licenses(searchValue = "Verizon Wireless")
14 changes: 7 additions & 7 deletions tests/testcases.py
Expand Up @@ -26,27 +26,27 @@ def test_Sweep(self):
results = [ True , True , False , False , False , False , False , False , False , False , ]

for x in range(10):
self.assertTrue((self.bb.request(latitude=41, longitude=-86 + x * 10)['status'] == 'OK') == results[x])
self.assertTrue((self.bb.get_data(latitude=41, longitude=-86 + x * 10)['status'] == 'OK') == results[x])


# Does SF exist?
def test_SF(self):
result = self.bb.request(latitude=37, longitude=-122)
result = self.bb.get_data(latitude=37, longitude=-122)

self.assertTrue(result['status'] == 'OK')
self.assertTrue('SpeedTestCounty' in result)

# Does Chicago exist?
def test_Chicago(self):
result = self.bb.request(latitude=41, longitude=-87)
result = self.bb.get_data(latitude=41, longitude=-87)

self.assertTrue(result['status'] == 'OK')


# Test the middle of nowhere (Disclaimer: I have no idea where
# this is, so it may not be in the middle of nowhere)
def test_Nowhere(self):
result = self.bb.request(latitude=35, longitude=35)
result = self.bb.get_data(latitude=35, longitude=35)

self.assertTrue(result['status'] == 'Fail')

Expand Down Expand Up @@ -80,7 +80,7 @@ def setUp(self):

# Does SF exist?
def test_SF(self):
result = self.bb.request(latitude=37, longitude=-122)
result = self.bb.get_block(latitude=37, longitude=-122)

self.assertTrue(result['status'] == 'OK')
self.assertTrue(result['State']['code'] == 'CA')
Expand All @@ -89,15 +89,15 @@ def test_SF(self):

# Does (somewhere near) Chicago exist?
def test_Chicago(self):
result = self.bb.request(latitude=41, longitude=-87)
result = self.bb.get_block(latitude=41, longitude=-87)

self.assertTrue(result['Block']['FIPS'] == '180739908004112')


# Test the middle of nowhere (Disclaimer: I have no idea where
# this is, so it may not be in the middle of nowhere)
def test_Nowhere(self):
result = self.bb.request(latitude=35, longitude=35)
result = self.bb.get_block(latitude=35, longitude=35)

self.assertTrue(result['status'] == 'Fail')

Expand Down

0 comments on commit da648bc

Please sign in to comment.