Allow to use the library without requests installed

pull/2/head
Raphaël Vinot 2016-07-28 16:01:27 +02:00
parent 7bad7cc38d
commit 52fea63106
2 changed files with 16 additions and 1 deletions

View File

@ -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):

View File

@ -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()