Skip to content

Commit

Permalink
FRN updated; testcases also changed?
Browse files Browse the repository at this point in the history
  • Loading branch information
quadrismegistus committed Apr 17, 2011
1 parent 7c9f2f9 commit cef35d0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
48 changes: 30 additions & 18 deletions frn_api.py
Expand Up @@ -5,33 +5,38 @@
# Simple Python wrapper around the Broadband API provided by the FCC.

class FrnApi(BaseAPIRequest):
"""
Provides a pythonic abstraction to the API @ http://reboot.fcc.gov/developer/frn-conversions-api
Two kinds of requests possible:
1. request(stateCode='IL') or request(stateCode=17) or request(stateCode='IL',multi='No')
returns a dictionary of FRNs in this state (keyed to 'Frns');
if multi==Yes, includes FRNs also operating in other states.
2. request(frn=0017855545)
returns a dictionary of information about this FRN, keyed to "Info"
"""

def __init__(self):
self.url_getList="http://data.fcc.gov/api/frn/getList?stateCode=%s&multi=%s"
self.url_getInfo="http://data.fcc.gov/api/frn/getInfo?frn=%s"


def format_url(self, **args):
if 'stateCode' in args:
if not 'multi' in args: args['multi']='Yes'
self.url=self.url_getList % (args['stateCode'],args['multi'])
self.url=self.url_getList
#if not 'multi' in args: args['multi']='Yes'
#self.formatted_url=self.url_getList % (args['stateCode'],args['multi'])
elif 'frn' in args:
self.url = self.url_getInfo % (args['frn'])
self.url=self.url_getInfo
#self.formatted_url = self.url_getInfo % (args['frn'])
else:
self.url = None
#print self.url

def request_state(self, stateCode, multi='Yes'):
#return BaseAPIRequest.request(self, **args)
url = self.url_getList % (stateCode, multi)
f = urllib.urlopen(url)
#return [l for l in f]
t=f.read().strip()

# jsonp to json
t=t[t.index('(')+1:-1]

return json.loads(t)
self.url=self.url_getList
#print self.url

def request(self, **args):
self.format_url(**args)
BaseAPIRequest.request(self,**args)


# Demonstration of how to use BroadbandApi
Expand All @@ -40,7 +45,14 @@ def request_state(self, stateCode, multi='Yes'):
#print bb.request(stateCode='IL')

#exit()
x=bb.request_state('IL')
#x=bb.request_state('IL')
x=bb.request(stateCode='IL')
print type(x)
print len(x)
print x

print
x=bb.request(frn='0017855545')
print type(x)
print len(x)
print x
23 changes: 23 additions & 0 deletions tests/testcases.py
Expand Up @@ -3,11 +3,13 @@

import unittest
from broadband_api import *
from frn_api import *

class TestBroadbandAPI (unittest.TestCase):

def setUp(self):
self.bb = BroadbandApi()
self.frnapi = FrnApi()


#Sweep across the US and compare to precomputed values.
Expand Down Expand Up @@ -47,6 +49,27 @@ def test_Nowhere(self):

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

def test_FRN(self):
result = self.frnapi.request(frn='0017855545')
self.assertTrue(result['Info']['frn'] == '0017855545')

def test_companyName(self):
result = self.frnapi.request(frn='0017855545')
self.assertTrue(result['Info']['companyName']=='Cygnus Telecommunications Corporation')

def test_FRNapiIsDict(self):
result1 = self.frnapi.request(stateCode='IL')
result2 = self.frnapi.request(frn='0017855545')
self.assertTrue(type(result1)==type({}) and type(result2)==type({}))
def test_CygnusInIL(self):
result = self.frnapi.request(stateCode='IL')
#print result['Frns']
#Cygnus Telecommunications Corporation
self.assertTrue('Cygnus Telecommunications Corporation' in [x['companyName'] for x in result['Frns']['Frn']])





if __name__ == '__main__':
unittest.main()

0 comments on commit cef35d0

Please sign in to comment.