1
1
#!/usr/bin/env python3
2
2
# North American Numbering Plan Administration (NANPA) API Client - Developed by acidvegas in Python (https://git.acid.vegas/nanpa)
3
+ # nanpa/client.py
3
4
4
5
import json
5
6
import logging
9
10
10
11
11
12
class NanpaAPI :
12
- def __init__ (self ):
13
- '''Initialize the NANPA API client.'''
13
+ def __init__ (self ):
14
+ '''Initialize the NANPA API client.'''
14
15
15
- self .base_url = 'https://api.nanpa.com/reports/public'
16
+ self .base_url = 'https://api.nanpa.com/reports/public'
16
17
17
- def _make_request (self , endpoint : str , params : dict = None ) -> dict :
18
- '''
19
- Make a request to the NANPA API.
20
-
21
- :param endpoint: API endpoint to call
22
- :param params: Optional query parameters
23
- '''
24
18
25
- url = f'{ self .base_url } /{ endpoint } '
26
-
27
- if params :
28
- url += '?' + urllib .parse .urlencode (params )
29
-
30
- try :
31
- with urllib .request .urlopen (url ) as response :
32
- return json .loads (response .read ().decode ())
33
- except urllib .error .URLError as e :
34
- logging .error (f'Failed to make request to { url } : { e } ' )
35
- return None
19
+ def _make_request (self , endpoint : str , params : dict = None ) -> dict :
20
+ '''
21
+ Make a request to the NANPA API.
36
22
23
+ :param endpoint: API endpoint to call
24
+ :param params: Optional query parameters
25
+ '''
37
26
38
- def get_9yy_codes (self ) -> dict :
39
- '''Get 9YY specialty codes.'''
27
+ url = f'{ self .base_url } /{ endpoint } '
40
28
41
- return self ._make_request ('specialityResources/9yy/codes' )
29
+ if params :
30
+ url += '?' + urllib .parse .urlencode (params )
42
31
32
+ try :
33
+ with urllib .request .urlopen (url ) as response :
34
+ return json .loads (response .read ().decode ())
35
+ except urllib .error .URLError as e :
36
+ logging .error (f'Failed to make request to { url } : { e } ' )
37
+ return None
43
38
44
- def get_area_code_info (self , npa : str ) -> dict :
45
- '''
46
- Get detailed information about a specific area code.
47
-
48
- :param npa: Area code to lookup
49
- '''
50
39
51
- params = {'npa' : npa }
40
+ def get_9yy_codes (self ) -> dict :
41
+ '''Get 9YY specialty codes.'''
52
42
53
- return self ._make_request ('npa/areaCodeListing' , params )
43
+ return self ._make_request ('specialityResources/9yy/codes' )
54
44
55
45
56
- def get_area_codes_by_state (self , state : str ) -> dict :
57
- '''
58
- Get area codes for a specific state.
59
-
60
- :param state: Two-letter state code
61
- '''
46
+ def get_area_code_info (self , npa : str ) -> dict :
47
+ '''
48
+ Get detailed information about a specific area code.
62
49
63
- params = {'states' : state , 'isExternal' : 'false' }
50
+ :param npa: Area code to lookup
51
+ '''
64
52
65
- return self . _make_request ( 'code/getNpasForStates' , params )
53
+ params = { 'npa' : npa }
66
54
55
+ return self ._make_request ('npa/areaCodeListing' , params )
67
56
68
- def get_co_code_forecast (self , state : str , npa : str ) -> dict :
69
- '''
70
- Get CO code forecast information.
71
-
72
- :param state: Two-letter state code
73
- :param npa: Area code
74
57
75
- '''
58
+ def get_area_codes_by_state (self , state : str ) -> dict :
59
+ '''
60
+ Get area codes for a specific state.
76
61
77
- params = {'state' : state , 'npa' : npa }
62
+ :param state: Two-letter state code
63
+ '''
78
64
79
- return self . _make_request ( 'tbco/coCodeForecast' , params )
65
+ params = { 'states' : state , 'isExternal' : 'false' }
80
66
67
+ return self ._make_request ('code/getNpasForStates' , params )
81
68
82
- def get_current_pool_tracking (self , state : str , npa : str ) -> dict :
83
- '''
84
- Get current pool tracking information.
85
-
86
- :param state: Two-letter state code
87
- :param npa: Area code
88
- '''
89
69
90
- params = {'state' : state , 'npa' : npa }
70
+ def get_co_code_forecast (self , state : str , npa : str ) -> dict :
71
+ '''
72
+ Get CO code forecast information.
91
73
92
- return self ._make_request ('tbco/currentPoolTracking' , params )
74
+ :param state: Two-letter state code
75
+ :param npa: Area code
93
76
77
+ '''
94
78
95
- def get_lrn_forecast (self , state : str , npa : str ) -> dict :
96
- '''
97
- Get LRN forecast information.
98
-
99
- :param state: Two-letter state code
100
- :param npa: Area code
101
- '''
79
+ params = {'state' : state , 'npa' : npa }
102
80
103
- params = { 'state' : state , 'npa' : npa }
81
+ return self . _make_request ( 'tbco/coCodeForecast' , params )
104
82
105
- return self ._make_request ('tbco/lrnForecast' , params )
106
83
84
+ def get_current_pool_tracking (self , state : str , npa : str ) -> dict :
85
+ '''
86
+ Get current pool tracking information.
107
87
108
- def get_pooling_forecast (self , state : str , npa : str ) -> dict :
109
- '''
110
- Get pooling forecast information.
111
-
112
- :param state: Two-letter state code
113
- :param npa: Area code
114
- '''
88
+ :param state: Two-letter state code
89
+ :param npa: Area code
90
+ '''
115
91
116
- params = {'state' : state , 'npa' : npa }
92
+ params = {'state' : state , 'npa' : npa }
117
93
118
- return self ._make_request ('tbco/poolingForecast ' , params )
94
+ return self ._make_request ('tbco/currentPoolTracking ' , params )
119
95
120
96
121
- def get_pstn_activation (self , state : str ) -> dict :
122
- '''
123
- Get PSTN activation information.
124
-
125
- :param state: Two-letter state code
126
- '''
97
+ def get_lrn_forecast (self , state : str , npa : str ) -> dict :
98
+ '''
99
+ Get LRN forecast information.
127
100
128
- params = {'state' : state }
101
+ :param state: Two-letter state code
102
+ :param npa: Area code
103
+ '''
129
104
130
- return self . _make_request ( 'tbco/pstnActivation' , params )
105
+ params = { 'state' : state , 'npa' : npa }
131
106
107
+ return self ._make_request ('tbco/lrnForecast' , params )
132
108
133
- def get_rate_center_changes (self , start_date : str , end_date : str ) -> dict :
134
- '''
135
- Get rate center changes between two dates.
136
-
137
- :param start_date: Start date in format YYYY-MM-DDT00:00:00.000-05:00
138
- :param end_date: End date in format YYYY-MM-DDT23:59:59.999-05:00
139
- '''
140
109
141
- params = {'startDate' : start_date , 'endDate' : end_date }
110
+ def get_pooling_forecast (self , state : str , npa : str ) -> dict :
111
+ '''
112
+ Get pooling forecast information.
142
113
143
- return self ._make_request ('npa/rateCenterChanges' , params )
114
+ :param state: Two-letter state code
115
+ :param npa: Area code
116
+ '''
144
117
118
+ params = {'state' : state , 'npa' : npa }
145
119
146
- def get_rate_centers (self , state : str ) -> dict :
147
- '''
148
- Get rate centers for a specific state.
149
-
150
- :param state: Two-letter state code
151
- '''
120
+ return self ._make_request ('tbco/poolingForecast' , params )
152
121
153
- params = {'state' : state }
154
122
155
- return self ._make_request ('npa/rateCenters' , params )
123
+ def get_pstn_activation (self , state : str ) -> dict :
124
+ '''
125
+ Get PSTN activation information.
156
126
127
+ :param state: Two-letter state code
128
+ '''
157
129
158
- def get_specialty_codes_aging (self ) -> dict :
159
- '''Get aging 5XX specialty codes.'''
130
+ params = {'state' : state }
160
131
161
- return self ._make_request ('specialityResources/5xx/aging' )
132
+ return self ._make_request ('tbco/pstnActivation' , params )
162
133
163
134
164
- def get_specialty_codes_assigned (self ) -> dict :
165
- '''Get assigned 5XX specialty codes.'''
135
+ def get_rate_center_changes (self , start_date : str , end_date : str ) -> dict :
136
+ '''
137
+ Get rate center changes between two dates.
166
138
167
- return self ._make_request ('specialityResources/5xx/assigned' )
139
+ :param start_date: Start date in format YYYY-MM-DDT00:00:00.000-05:00
140
+ :param end_date: End date in format YYYY-MM-DDT23:59:59.999-05:00
141
+ '''
168
142
143
+ params = {'startDate' : start_date , 'endDate' : end_date }
169
144
170
- def get_specialty_codes_available (self , code_type : str = '5xx' ) -> dict :
171
- '''
172
- Get available specialty codes.
173
-
174
- :param code_type: Code type ('5xx' or '9yy')
175
- '''
145
+ return self ._make_request ('npa/rateCenterChanges' , params )
176
146
177
- return self ._make_request (f'specialityResources/{ code_type } /available' )
178
147
148
+ def get_rate_centers (self , state : str ) -> dict :
149
+ '''
150
+ Get rate centers for a specific state.
179
151
180
- def get_states ( self ) -> dict :
181
- '''Get all NANPA states and territories. '''
152
+ :param state: Two-letter state code
153
+ '''
182
154
183
- return self . _make_request ( 'nanpaStates' )
155
+ params = { 'state' : state }
184
156
157
+ return self ._make_request ('npa/rateCenters' , params )
185
158
186
- def get_thousands_blocks (self , state : str , npa : str , report_type : str = 'AS' ) -> dict :
187
- '''
188
- Get thousands blocks information.
189
-
190
- :param state: Two-letter state code
191
- :param npa: Area code
192
- :param report_type: Report type (default: AS)
193
- '''
194
159
195
- params = {'state' : state , 'npa' : npa , 'reportType' : report_type }
160
+ def get_specialty_codes_aging (self ) -> dict :
161
+ '''Get aging 5XX specialty codes.'''
196
162
197
- return self ._make_request ('tbco/thousandsBlocks' , params )
163
+ return self ._make_request ('specialityResources/5xx/aging' )
164
+
165
+
166
+ def get_specialty_codes_assigned (self ) -> dict :
167
+ '''Get assigned 5XX specialty codes.'''
168
+
169
+ return self ._make_request ('specialityResources/5xx/assigned' )
170
+
171
+
172
+ def get_specialty_codes_available (self , code_type : str = '5xx' ) -> dict :
173
+ '''
174
+ Get available specialty codes.
175
+
176
+ :param code_type: Code type ('5xx' or '9yy')
177
+ '''
178
+
179
+ return self ._make_request (f'specialityResources/{ code_type } /available' )
180
+
181
+
182
+ def get_states (self ) -> dict :
183
+ '''Get all NANPA states and territories.'''
184
+
185
+ return self ._make_request ('nanpaStates' )
186
+
187
+
188
+ def get_thousands_blocks (self , state : str , npa : str , report_type : str = 'AS' ) -> dict :
189
+ '''
190
+ Get thousands blocks information.
191
+
192
+ :param state: Two-letter state code
193
+ :param npa: Area code
194
+ :param report_type: Report type (default: AS)
195
+ '''
196
+
197
+ params = {'state' : state , 'npa' : npa , 'reportType' : report_type }
198
+
199
+ return self ._make_request ('tbco/thousandsBlocks' , params )
198
200
199
201
200
202
201
203
def main ():
202
- '''Example usage of the NANPA API client.'''
204
+ '''Example usage of the NANPA API client.'''
205
+
206
+ client = NanpaAPI ()
207
+
208
+ # Example API calls
209
+ states = client .get_states ()
210
+ logging .info (f'States: { json .dumps (states , indent = 2 )} ' )
211
+
212
+ fl_codes = client .get_area_codes_by_state ('FL' )
213
+ logging .info (f'Florida area codes: { json .dumps (fl_codes , indent = 2 )} ' )
203
214
204
- client = NanpaAPI ()
205
-
206
- # Example API calls
207
- states = client .get_states ()
208
- logging .info (f'States: { json .dumps (states , indent = 2 )} ' )
209
-
210
- fl_codes = client .get_area_codes_by_state ('FL' )
211
- logging .info (f'Florida area codes: { json .dumps (fl_codes , indent = 2 )} ' )
212
-
213
- area_info = client .get_area_code_info ('301' )
214
- logging .info (f'301 area code info: { json .dumps (area_info , indent = 2 )} ' )
215
+ area_info = client .get_area_code_info ('301' )
216
+ logging .info (f'301 area code info: { json .dumps (area_info , indent = 2 )} ' )
215
217
216
218
217
219
if __name__ == '__main__' :
218
- main ()
220
+ main ()
0 commit comments