|
11 | 11 | mapping_api_endpoint = "https://api.sec-api.io/mapping" |
12 | 12 | exec_comp_api_endpoint = "https://api.sec-api.io/compensation" |
13 | 13 | insider_api_endpoint = "https://api.sec-api.io/insider-trading" |
| 14 | +form_nport_api_endpoint = "https://api.sec-api.io/form-nport" |
| 15 | +form_d_api_endpoint = "https://api.sec-api.io/form-d" |
| 16 | +form_adv_endpoint = "https://api.sec-api.io/form-adv" |
14 | 17 |
|
15 | 18 |
|
16 | 19 | def handle_api_error(response): |
@@ -298,3 +301,120 @@ def get_data(self, query): |
298 | 301 | handle_api_error(response) |
299 | 302 | else: |
300 | 303 | handle_api_error(response) |
| 304 | + |
| 305 | + |
| 306 | +class FormNportApi: |
| 307 | + """ |
| 308 | + Base class for Form NPORT API |
| 309 | + """ |
| 310 | + |
| 311 | + def __init__(self, api_key): |
| 312 | + self.api_key = api_key |
| 313 | + self.api_endpoint = form_nport_api_endpoint + "?token=" + api_key |
| 314 | + |
| 315 | + def get_data(self, query): |
| 316 | + response = {} |
| 317 | + |
| 318 | + # use backoff strategy to handle "too many requests" error. |
| 319 | + for x in range(3): |
| 320 | + response = requests.post(self.api_endpoint, json=query) |
| 321 | + if response.status_code == 200: |
| 322 | + return response.json() |
| 323 | + elif response.status_code == 429: |
| 324 | + # wait 500 * (x + 1) milliseconds and try again |
| 325 | + time.sleep(0.5 * (x + 1)) |
| 326 | + else: |
| 327 | + handle_api_error(response) |
| 328 | + else: |
| 329 | + handle_api_error(response) |
| 330 | + |
| 331 | + |
| 332 | +class FormDApi: |
| 333 | + """ |
| 334 | + Base class for Form D API |
| 335 | + """ |
| 336 | + |
| 337 | + def __init__(self, api_key): |
| 338 | + self.api_key = api_key |
| 339 | + self.api_endpoint = form_d_api_endpoint + "?token=" + api_key |
| 340 | + |
| 341 | + def get_data(self, query): |
| 342 | + response = {} |
| 343 | + |
| 344 | + # use backoff strategy to handle "too many requests" error. |
| 345 | + for x in range(3): |
| 346 | + response = requests.post(self.api_endpoint, json=query) |
| 347 | + if response.status_code == 200: |
| 348 | + return response.json() |
| 349 | + elif response.status_code == 429: |
| 350 | + # wait 500 * (x + 1) milliseconds and try again |
| 351 | + time.sleep(0.5 * (x + 1)) |
| 352 | + else: |
| 353 | + handle_api_error(response) |
| 354 | + else: |
| 355 | + handle_api_error(response) |
| 356 | + |
| 357 | + |
| 358 | +class FormAdvApi: |
| 359 | + """ |
| 360 | + Base class for Form ADV API |
| 361 | + """ |
| 362 | + |
| 363 | + def __init__(self, api_key): |
| 364 | + self.api_key = api_key |
| 365 | + self.api_endpoint_firm = form_adv_endpoint + "/firm" + "?token=" + api_key |
| 366 | + self.api_endpoint_individual = ( |
| 367 | + form_adv_endpoint + "/individual" + "?token=" + api_key |
| 368 | + ) |
| 369 | + self.api_endpoint_brochures = form_adv_endpoint + "/brochures?token=" + api_key |
| 370 | + |
| 371 | + def get_firms(self, query): |
| 372 | + response = {} |
| 373 | + |
| 374 | + # use backoff strategy to handle "too many requests" error. |
| 375 | + for x in range(3): |
| 376 | + response = requests.post(self.api_endpoint_firm, json=query) |
| 377 | + if response.status_code == 200: |
| 378 | + return response.json() |
| 379 | + elif response.status_code == 429: |
| 380 | + # wait 500 * (x + 1) milliseconds and try again |
| 381 | + time.sleep(0.5 * (x + 1)) |
| 382 | + else: |
| 383 | + handle_api_error(response) |
| 384 | + else: |
| 385 | + handle_api_error(response) |
| 386 | + |
| 387 | + def get_individuals(self, query): |
| 388 | + response = {} |
| 389 | + |
| 390 | + # use backoff strategy to handle "too many requests" error. |
| 391 | + for x in range(3): |
| 392 | + response = requests.post(self.api_endpoint_individual, json=query) |
| 393 | + if response.status_code == 200: |
| 394 | + return response.json() |
| 395 | + elif response.status_code == 429: |
| 396 | + # wait 500 * (x + 1) milliseconds and try again |
| 397 | + time.sleep(0.5 * (x + 1)) |
| 398 | + else: |
| 399 | + handle_api_error(response) |
| 400 | + else: |
| 401 | + handle_api_error(response) |
| 402 | + |
| 403 | + def get_brochures(self, crd): |
| 404 | + endpoint = ( |
| 405 | + form_adv_endpoint + "/brochures/" + str(crd) + "?token=" + self.api_key |
| 406 | + ) |
| 407 | + response = {} |
| 408 | + |
| 409 | + # use backoff strategy to handle "too many requests" error. |
| 410 | + for x in range(3): |
| 411 | + response = requests.get(endpoint) |
| 412 | + if response.status_code == 200: |
| 413 | + return response.json() |
| 414 | + elif response.status_code == 429: |
| 415 | + # wait 500 * (x + 1) milliseconds and try again |
| 416 | + time.sleep(0.5 * (x + 1)) |
| 417 | + else: |
| 418 | + handle_api_error(response) |
| 419 | + else: |
| 420 | + handle_api_error(response) |
0 commit comments