def test_has_methods(self): json_file = os.path.join(self.data_path, 'api.json') client = spyre(json_file, base_url='http://my_base.url/') with open(json_file, 'r') as api_description: content = json.loads(api_description.read()) for method in content['methods']: self.assertEqual(getattr(client, method).name, method)
def get_client(name, description, base_url=None, middlewares=None, reset=False): """ Build a complete spore client and store it :param name: name of the client :param description: the REST API description as a file or URL :param base_url: the base URL of the REST API :param middlewares: middlewares to enable :type middlewares: ordered list of 2-elements tuples -> (middleware_class, { 'predicate': ..., 'named_arg1': ..., 'named_arg2': ..., ...}) :param reset: regenerate or not the client Example : import britney_utils from britney.middleware.format import Json from britney.middleware.auth import Basic is_json = lambda environ: environ['spore.format'] == 'json' client = britney_utils.get_client('MyRestApi', 'http://my-rest-api.org/description.json', base_url='http://rest-api.org/v2/', middlewares=( (Json, {'predicate': is_json}), (Basic, {'username': '******', 'password': '******'}) )) """ if name in __clients and not reset: return __clients[name] middlewares = middlewares if middlewares is not None else [] try: client = britney.spyre(description, base_url=base_url) except (SporeClientBuildError, SporeMethodBuildError) as build_errors: logging.getLogger('britney').error(str(build_errors)) else: for middleware in middlewares: kwargs = {} if len(middleware) == 2: kwargs = middleware[1] predicate = kwargs.pop('predicate', None) if predicate: client.enable_if(predicate, middleware[0], **kwargs) else: client.enable(middleware[0], **kwargs) __clients[name] = client return client
import britney from britney.middleware import auth, format from yaml import load import re from .util import load_conf_file from .middleware import Group conf = load(load_conf_file()) try: client = britney.spyre(conf['general']['spec_uri'], base_url=conf['general']['redmine_url']) except Exception as e: print(e) else: client.enable(format.Json) client.enable_if(lambda environ: re.search(r'group', environ['PATH_INFO']), Group) client.enable(auth.ApiKey, key_name='X-Redmine-API-Key', key_value=conf['general']['redmine_key'])
def test_multiple_clients_with_same_method_names(self): client1 = spyre(os.path.join(self.data_path, 'api.json')) client2 = spyre(os.path.join(self.data_path, 'api2.json')) self.assertFalse(client1.test_requires == client2.test_requires)
def test_with_base_url(self): json_file = os.path.join(self.data_path, 'api.json') client = spyre(json_file, base_url='http://my_base.url/') self.assertEqual(client.base_url, 'http://my_base.url/')
def test_without_base_url(self): json_file = os.path.join(self.data_path, 'api.json') client = spyre(json_file) with open(json_file, 'r') as api_description: content = json.loads(api_description.read()) self.assertEqual(client.base_url, content['base_url'])
def test_no_valid_json_document(self): with self.assertRaises(ValueError) as json_error: json_file = os.path.join(self.data_path, 'no_api.json') spyre(json_file)
def test_no_description_url(self): with self.assertRaises(IOError) as http_error: spyre('http://description.not.fou.nd/api.json')
def test_no_description_file(self): with self.assertRaises(IOError) as io_error: spyre('/path/dont/exist/api.json')