mirror of https://github.com/MISP/PyTaxonomies
Allow to use the library without requests installed
parent
7bad7cc38d
commit
52fea63106
|
@ -2,11 +2,16 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import requests
|
|
||||||
import os
|
import os
|
||||||
import collections
|
import collections
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
try:
|
||||||
|
import requests
|
||||||
|
HAS_REQUESTS = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_REQUESTS = False
|
||||||
|
|
||||||
|
|
||||||
class Entry():
|
class Entry():
|
||||||
|
|
||||||
|
@ -132,6 +137,8 @@ class Taxonomies(collections.Mapping):
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
def __load_url(self, url):
|
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()
|
return requests.get(url).json()
|
||||||
|
|
||||||
def __make_uri(self, taxonomy_name):
|
def __make_uri(self, taxonomy_name):
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from pytaxonomies import Taxonomies
|
from pytaxonomies import Taxonomies
|
||||||
|
import pytaxonomies.api
|
||||||
|
|
||||||
|
|
||||||
class TestPyTaxonomies(unittest.TestCase):
|
class TestPyTaxonomies(unittest.TestCase):
|
||||||
|
@ -44,6 +45,13 @@ class TestPyTaxonomies(unittest.TestCase):
|
||||||
def test_amountEntries(self):
|
def test_amountEntries(self):
|
||||||
list(self.taxonomies.values())[0].amount_entries()
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue