Skip to content

Commit

Permalink
Merge pull request #9 from bgpkit/feature/8-verify-flag
Browse files Browse the repository at this point in the history
Add `verify` flag to broker to allow disabling SSL verification.
  • Loading branch information
digizeph authored Apr 17, 2023
2 parents ab9b0b8 + cae4bc1 commit 4272205
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
11 changes: 8 additions & 3 deletions bgpkit/bgpkit_broker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass

import requests as requests
import urllib3


def check_type(value: any, ty: type) -> bool:
Expand All @@ -24,9 +25,13 @@ class BrokerItem:

class Broker:

def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v2", page_size: int = 100):
def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v2", page_size: int = 100, verify=True):
self.base_url = api_url.strip()
self.page_size = int(page_size)
self.verify = verify
if not verify:
# if a user disable SSL verification on-purpose, do not warn the user
urllib3.disable_warnings()

def query(self,
ts_start: str = None,
Expand Down Expand Up @@ -58,7 +63,7 @@ def query(self,
data_items = []
if print_url:
print(api_url)
res = requests.get(api_url).json()
res = requests.get(api_url, verify=self.verify).json()
while res:
if res["count"] > 0:
data_items.extend([BrokerItem(**i) for i in res["data"]])
Expand All @@ -70,7 +75,7 @@ def query(self,
query_url = f"{api_url}&page={page}"
if print_url:
print(query_url)
res = requests.get(query_url).json()
res = requests.get(query_url, verify=self.verify).json()
else:
break

Expand Down
8 changes: 8 additions & 0 deletions bgpkit/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def test_broker_2(self):
print(len(items))
assert len(items) == 7

def test_broker_no_verify(self):
broker = bgpkit.Broker(verify=False)
items = broker.query(ts_start="1643760000", ts_end="2022-02-02T00:20:00", collector_id="rrc00")
for item in items:
print(json.dumps(item.__dict__))
print(len(items))
assert len(items) == 7

def test_roas(self):
roas = bgpkit.Roas()
data = roas.query(debug=True, asn=3333, date="2018-01-01")
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='pybgpkit',
version='0.4.0',
version='0.4.3',
description='BGPKIT tools Python bindings',
url='https://github.com/bgpkit/pybgpkit',
author='Mingwei Zhang',
Expand All @@ -19,7 +19,7 @@
install_requires=[
# available on pip
'dataclasses_json',
'pybgpkit-parser==0.4.0',
'pybgpkit-parser==0.4.2',
'requests',
]
)
4 changes: 3 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import bgpkit
parser = bgpkit.Parser(url="https://spaces.bgpkit.org/parser/update-example",
filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"})
filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"},
cache_dir="cache"
)
elems = parser.parse_all()
assert len(elems) == 4227
import json
Expand Down

0 comments on commit 4272205

Please sign in to comment.