Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change host in the swagger file? #235

Open
vaghar opened this issue Jul 13, 2016 · 8 comments
Open

How to change host in the swagger file? #235

vaghar opened this issue Jul 13, 2016 · 8 comments

Comments

@vaghar
Copy link

vaghar commented Jul 13, 2016

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

@prat0318
Copy link
Contributor

@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?

@analogue
Copy link
Contributor

analogue commented Jul 14, 2016

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>

@vaghar
Copy link
Author

vaghar commented Jul 14, 2016

Thank you so much for the suggestions!

@advance512
Copy link

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...

@sjaensch
Copy link
Contributor

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

@advance512
Copy link

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.

@sjaensch
Copy link
Contributor

sjaensch commented May 4, 2017

@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.

@advance512
Copy link

iirc, not putting a host/port caused bravado to complain and die.. but I'll check it out, just to be sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants