-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bgpkit/feature-roas-sdk
Add support for BGPKIT ROAS lookup API
- Loading branch information
Showing
6 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .bgpkit_parser import Parser | ||
from .bgpkit_broker import Broker | ||
from .bgpkit_roas import Roas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, List | ||
|
||
import requests as requests | ||
|
||
|
||
def check_type(value: any, ty: type) -> bool: | ||
try: | ||
ty(value) | ||
return True | ||
except ValueError: | ||
raise ValueError("invalid option input") | ||
|
||
|
||
@dataclass | ||
class RoasItem: | ||
prefix: str | ||
asn: int | ||
tal: str | ||
date_ranges: List[List[str]] | ||
|
||
|
||
@dataclass | ||
class RoasRes: | ||
limit: int | ||
count: int | ||
data: List[RoasItem] | ||
next_page_num: Optional[int] | ||
next_page: Optional[str] | ||
error: Optional[str] | ||
|
||
|
||
class Roas: | ||
|
||
def __init__(self, api_url: str = "https://api.roas.bgpkit.com"): | ||
self.base_url = api_url.strip() | ||
|
||
def query(self, | ||
prefix: str = None, | ||
asn: int = None, | ||
tal: str = None, | ||
date: str = None, | ||
max_len: int = None, | ||
debug: bool = False, | ||
) -> [RoasItem]: | ||
|
||
if not (prefix or asn or tal or date or max_len): | ||
print("ERROR: must specify at least one query parameter: prefix, asn, tal, date, max_len") | ||
return [] | ||
|
||
params = [] | ||
if prefix: | ||
check_type(prefix, str) | ||
params.append(f"prefix={prefix}") | ||
if asn: | ||
check_type(asn, int) | ||
params.append(f"asn={asn}") | ||
if tal: | ||
check_type(tal, str) | ||
params.append(f"tal={tal}") | ||
if date: | ||
check_type(date, str) | ||
params.append(f"date={date}") | ||
if max_len: | ||
check_type(max_len, int) | ||
params.append(f"max_len={max_len}") | ||
|
||
api_url = f"{self.base_url}/lookup?" + "&".join(params) | ||
data_items = [] | ||
if debug: | ||
print(api_url) | ||
res = RoasRes(**requests.get(api_url).json()) | ||
while res.data: | ||
data_items.extend(res.data) | ||
if res.next_page: | ||
res = RoasRes(**requests.get(res.next_page).json()) | ||
else: | ||
break | ||
|
||
return data_items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import setuptools | ||
|
||
# read the contents of your README file | ||
from pathlib import Path | ||
this_directory = Path(__file__).parent | ||
long_description = (this_directory / "README.md").read_text() | ||
|
||
setuptools.setup( | ||
name='pybgpkit', | ||
version='0.0.1', | ||
version='0.0.2.post1', | ||
description='BGPKIT tools Python bindings', | ||
url='https://github.com/bgpkit/pybgpkit', | ||
author='Mingwei Zhang', | ||
author_email='[email protected]', | ||
packages=setuptools.find_packages(), | ||
include_package_data=True, | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
install_requires=[ | ||
# available on pip | ||
'pybgpkit-parser==0.0.1', | ||
'requests', | ||
], | ||
entry_points={'console_scripts': [ | ||
]} | ||
|