bravado
bravado copied to clipboard
How to change host in the swagger file?
I just started using the library (thanks). A question on API usage:
My yml file looks like this:
swagger: '2.0'
info:
title: FogHorn Edge API
description: Manages FogHorn edge components
version: "1.0.0"
host: localhost
schemes:
- http
...........
I can successfully load the swagger file and create a client:
from bravado.client import SwaggerClient
from bravado.swagger_model import load_file
def create_client(self):
swagger_file = self.get_yml_file()
a_file = load_file(swagger_file)
return SwaggerClient.from_spec(a_file)
My question is how do I at runtime replace the localhost in the swagger file with the address of the server (e.g. 129.100:90.6:9000) using bravado api? My swagger file is stored locally and not a server.
For know I am replacing the text in the swagger file before calling load_file. I tried to use RequestsClient.separate_params to set the 'url', but no success. I have also tried setting via origin_url or config, using the from_spec method (no success):
def from_spec(cls, spec_dict, origin_url=None, http_client=None, config=None)
Thanks for your help, Shahriar
@analogue I don't recollect a way of providing the host during request call if schema file is read via file:// mode. Any ideas if bravado supports this?
There doesn't look to be a publicly documented non-invasive way to do this. A PR to address this would be awesome. In the meantime, you might be able to get away with something like this:
from bravado_core.spec import build_api_serving_url
client = SwaggerClient.from_url(...)
# suggested
client.swagger_spec.api_url = build_api_serving_url(...)
# or even
client.swagger_spec.api_url = <whatever you want it to be>
Thank you so much for the suggestions!
This is a very basic requirement for using microservices. I don't know what the host and port will be, but I get them dynamically and load them into Bravado, which then creates a client that bravely attempt to connect to the statically defined host/port endpoint as denoted in the swagger.json file...
@advance512 if you load a spec from a file, this will do the trick (this is how we do it internally):
from bravado.client import SwaggerClient
from bravado.swagger_model import load_file
spec_dict = load_file(spec_path)
spec_dict['host'] = '{}:{}'.format(host, port)
But I'm not loading it from a file. I have an endpoint from which I construct a URL:
http://host:port/service/swagger.json
I give this URL to Bravado. It creates a client that instead of using:
http://host:port/service/
uses:
http://staticHost:staticPort/service/
I can create an HTTP client and download the file, and use load_file instead - it seems overkill.
I'd much rather do:
client = SwaggerClient.from_url(...,
host=endpointHost,
port=endpointPort,
)
I follow @analogue's example instead.
@advance512 your issue is similar but different from the one the original poster discussed. You can just remove the host entry from your swagger spec file, bravado should use the host and port from your URL.
Ideally the host that you use in the URL should be a load balancer (or something similar) that will then route the request to one of your microservice instances. At Yelp we use SmartStack for this purpose.
iirc, not putting a host/port caused bravado to complain and die.. but I'll check it out, just to be sure.