Skip to content

Commit a9bc4c4

Browse files
authored
Merge pull request #2 from bgpkit/v2-broker
support broker api version 2
2 parents 2b80c0d + 9478bcd commit a9bc4c4

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Example:
6262
```python
6363
import bgpkit
6464
broker = bgpkit.Broker()
65-
items = broker.query(start_ts=1634693400, end_ts=1634693400)
65+
items = broker.query(ts_start="1634693400", ts_end="2021-10-20T01:30:00")
6666
for item in items:
6767
print(item)
6868
print(len(items))
@@ -72,14 +72,13 @@ assert len(items) == 58
7272
Available fields:
7373

7474
- `Broker()`
75-
- `api_url`: the base URL for the BGPKIT Broker instance. Default: `https://api.broker.bgpkit.com/v1`
75+
- `api_url`: the base URL for the BGPKIT Broker instance. Default: `https://api.broker.bgpkit.com/v2`
7676
- `page_size`: the number of items per API call (no need to change it). Default: 100.
7777
- `query()`
78-
- `start_ts`: start timestamp for MRT file, UNIX timestamp format
79-
- `end_ts`: end timestamp for MRT file, UNIX timestamp format
78+
- `ts_start`: start timestamp for MRT file, UNIX timestamp format
79+
- `ts_end`: end timestamp for MRT file, UNIX timestamp format
8080
- `collector`: collector name, e.g. `rrc00` or `route-views2`
8181
- `data_type`: `rib` or `update`
82-
- `order`: order by timestamp, `asc` or `desc`, default
8382

8483
### BGPKIT ROAS Lookup
8584

bgpkit/bgpkit_broker.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,43 @@ def check_type(value: any, ty: type) -> bool:
1313

1414
@dataclass
1515
class BrokerItem:
16+
ts_start: str
17+
ts_end: str
1618
collector_id: str
1719
data_type: str
18-
timestamp: int
1920
url: str
21+
rough_size: int
22+
exact_size: int
2023

2124

2225
class Broker:
2326

24-
def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v1", page_size: int = 100):
27+
def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v2", page_size: int = 100):
2528
self.base_url = api_url.strip()
2629
self.page_size = int(page_size)
2730

2831
def query(self,
29-
start_ts: int = None,
30-
end_ts: int = None,
31-
collector: str = None,
32+
ts_start: str = None,
33+
ts_end: str = None,
34+
collector_id: str = None,
3235
project: str = None,
3336
data_type: str = None,
34-
order: str = None,
3537
print_url: bool = False,
3638
) -> [BrokerItem]:
3739
params = []
38-
if start_ts:
39-
check_type(start_ts, int)
40-
params.append(f"start_ts={start_ts}")
41-
if end_ts:
42-
check_type(end_ts, int)
43-
params.append(f"end_ts={end_ts}")
44-
if collector:
45-
check_type(collector, str)
46-
params.append(f"collector={collector}")
40+
if ts_start:
41+
params.append(f"ts_start={ts_start}")
42+
if ts_end:
43+
params.append(f"ts_end={ts_end}")
44+
if collector_id:
45+
check_type(collector_id, str)
46+
params.append(f"collector={collector_id}")
4747
if project:
4848
check_type(project, str)
4949
params.append(f"project={project}")
5050
if data_type:
5151
check_type(data_type, str)
5252
params.append(f"data_type={data_type}")
53-
if order:
54-
check_type(order, str)
55-
params.append(f"order={order}")
5653
params.append(f"page_size={self.page_size}")
5754

5855
api_url = f"{self.base_url}/search?" + "&".join(params)
@@ -61,10 +58,10 @@ def query(self,
6158
data_items = []
6259
if print_url:
6360
print(api_url)
64-
res = requests.get(api_url).json()["data"]
61+
res = requests.get(api_url).json()
6562
while res:
6663
if res["count"] > 0:
67-
data_items.extend([BrokerItem(**i) for i in res["items"]])
64+
data_items.extend([BrokerItem(**i) for i in res["data"]])
6865

6966
if res["count"] < res["page_size"]:
7067
break
@@ -73,7 +70,7 @@ def query(self,
7370
query_url = f"{api_url}&page={page}"
7471
if print_url:
7572
print(query_url)
76-
res = requests.get(query_url).json()["data"]
73+
res = requests.get(query_url).json()
7774
else:
7875
break
7976

bgpkit/bgpkit_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from pybgpkit_parser import Parser
1+
from pybgpkit_parser import Parser

bgpkit/test_integration.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import unittest
23

34
import bgpkit
@@ -13,11 +14,11 @@ def test_parser(self):
1314

1415
def test_broker(self):
1516
broker = bgpkit.Broker()
16-
items = broker.query(start_ts=1643760000, end_ts=1643761200)
17+
items = broker.query(ts_start="1643760000", ts_end="2022-02-02T00:20:00")
1718
for item in items:
18-
print(item)
19+
print(json.dumps(item.__dict__))
1920
print(len(items))
20-
assert len(items) == 58
21+
assert len(items) == 290
2122

2223
# elems = bgpkit.Parser(items[0].url).parse_all()
2324
# assert len(elems) == 222467

setup.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name='pybgpkit',
10-
version='0.0.2.post1',
10+
version='0.0.4',
1111
description='BGPKIT tools Python bindings',
1212
url='https://github.com/bgpkit/pybgpkit',
1313
author='Mingwei Zhang',
@@ -18,9 +18,13 @@
1818
long_description_content_type='text/markdown',
1919
install_requires=[
2020
# available on pip
21-
'pybgpkit-parser==0.0.1',
21+
'dataclasses_json',
22+
'pybgpkit-parser==0.1.0',
2223
'requests',
2324
],
24-
entry_points={'console_scripts': [
25-
]}
25+
entry_points={
26+
'console_scripts': [
27+
"pybgpkit=bgpkit.cli:main"
28+
]
29+
}
2630
)

0 commit comments

Comments
 (0)