You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:param str url: url of path of Swagger API definition
However, these are cases where we need more configurability. For example, I've just encountered an OpenAPI endpoint that rejects the urllib UserAgent, so I would like to be able to provide a customised UserAgent via a Request instance, rather than simply a string.
The text was updated successfully, but these errors were encountered:
This would be a great help. There are providers such as CloudFlare which explicitly block Python urllib requests; without the ability to pick a new UserAgent pyswagger cannot fetch the swagger file from these.
I had the same problem. What I did as a workaround was subclassing App (which in turn would load a subclassed UrlGetter) and subclass Client.
import six
from pyswagger import App, Security
from pyswagger.getter import UrlGetter
from pyswagger.contrib.client.requests import Client
header_dict = {"User-agent": "Best Alpha Agent/0.0.1a"}
def _url_load(path):
# original _url_load method is here:
# https://github.com/pyopenapi/pyswagger/blob/333c4ca08e758cd2194943d9904a3eda3fe43977/pyswagger/getter.py#L141
ret = f = None
try:
# This is new. create a Request object instead of path
req = six.moves.urllib.request.Request(path, headers=header_dict)
f = six.moves.urllib.request.urlopen(req)
ret = f.read()
finally:
if f:
f.close()
return ret
class CustomUrlGetter(UrlGetter):
__simple_getter_callback__ = _url_load
class CustomApp(App):
def load_obj(self, jref, getter=None, parser=None):
return super(CustomApp, self).load_obj(jref, getter=CustomUrlGetter, parser=parser)
class CustomClient(Client):
def request(self, req_and_resp, opt=None, headers=None):
return super(CustomClient, self).request(req_and_resp, opt, headers=header_dict)
App.load()
assumes thaturl
is a string, for requesting the OpenAPI.json:pyswagger/pyswagger/core.py
Line 264 in 333c4ca
However, these are cases where we need more configurability. For example, I've just encountered an OpenAPI endpoint that rejects the urllib UserAgent, so I would like to be able to provide a customised UserAgent via a
Request
instance, rather than simply a string.The text was updated successfully, but these errors were encountered: