diff --git a/pytaxonomies/api.py b/pytaxonomies/api.py index 4f3c77c..d4cc0c9 100644 --- a/pytaxonomies/api.py +++ b/pytaxonomies/api.py @@ -2,11 +2,16 @@ # -*- coding: utf-8 -*- import json -import requests import os import collections import re +try: + import requests + HAS_REQUESTS = True +except ImportError: + HAS_REQUESTS = False + class Entry(): @@ -132,6 +137,8 @@ class Taxonomies(collections.Mapping): return json.load(f) def __load_url(self, url): + if not HAS_REQUESTS: + raise Exception("Python module 'requests' isn't installed, unable to fetch the taxonomies.") return requests.get(url).json() def __make_uri(self, taxonomy_name): diff --git a/tests/tests.py b/tests/tests.py index 3785fbf..ad96932 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -3,6 +3,7 @@ import unittest from pytaxonomies import Taxonomies +import pytaxonomies.api class TestPyTaxonomies(unittest.TestCase): @@ -44,6 +45,13 @@ class TestPyTaxonomies(unittest.TestCase): def test_amountEntries(self): list(self.taxonomies.values())[0].amount_entries() + def test_missingDependency(self): + pytaxonomies.api.HAS_REQUESTS = False + with self.assertRaises(Exception): + Taxonomies() + Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json") + pytaxonomies.api.HAS_REQUESTS = True + if __name__ == "__main__": unittest.main()