-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
61 lines (42 loc) · 1.61 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Define clients for other services we interact with.
Structure copied from sfl2/gateway//gateway/client.py
For more usage information, see the Bravado docs:
http://bravado.readthedocs.io/en/latest/quickstart.html
"""
import logging
import os.path
from urllib.parse import urlparse
from bravado.client import SwaggerClient
from bravado.swagger_model import load_file
logger = logging.getLogger(__name__)
class Clients(object):
def __init__(self):
"""
Initialize bravado client.
"""
self.spec_dir_absolute_path = os.path.dirname(__file__)
self.bravado_config = dict(
validate_swagger_spec=True,
validate_requests=True,
validate_responses=True,
use_models=True,
)
self._add_swagger('gateway', 'https://gateway.vcf-test.vzw.dev.llabs.io/')
def _add_swagger(self, service_name, service_url):
# create client
# Fix bravado URL. By default Bravado ignores non-host parts of the input
# URL if the spec base path is defined. We want the path from the URL plus
# the spec's base path.
spec_dict = load_file(os.path.join(self.spec_dir_absolute_path, service_name + '.json'))
base_path = (
urlparse(service_url).path.rstrip('/') +
spec_dict.get('basePath', ''))
if base_path:
spec_dict['basePath'] = base_path
swagger_client = SwaggerClient.from_spec(
spec_dict,
origin_url=service_url,
config=self.bravado_config)
setattr(self, service_name, swagger_client)
clients = Clients()