如何在 Bravado 中设置自定义 http 客户端? [英] How do you set a custom http client in Bravado?

查看:34
本文介绍了如何在 Bravado 中设置自定义 http 客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Bravado 3.0.0.我想提出一个将使用我自己的自定义 CA 捆绑包的请求.底层请求客户端没有利用我设置的 REQUESTS_CA_BUNDLE 环境变量.

I'm using Bravado 3.0.0. I want to make a request that will use my own custom CA Bundle. The underlying Requests client isn't taking advantage of the REQUESTS_CA_BUNDLE env var I've set.

如何传入使用我的 CA Bundle 的自定义客户端?

How do I pass in a custom client that uses my CA Bundle?

推荐答案

(此答案基于当前的 Bravado 8.1.0 版本)

(This answer is based on the current-as-of-this-writing 8.1.0 version of Bravado)

因为我在学习 Bravado 时甚至花了一段时间才找到这个答案,主要是因为我认为其他人在开始时可能会受益,这里是如何建立连接的最新研究:

Since it took me a while to even find this answer while learning about Bravado, and mostly because I think others might benefit when starting out, here's an updated look into how to establish a connection:

为了利用 HTTP 客户端中的非默认设置,(以请求为例)必须使用自己的设置创建一个新的 HTTP 客户端实例,然后将其传递给 SwaggerClient.from_url() 调用:

In order to leverage non-default settings in the HTTP client, (using Requests as an example,) one has to create a new HTTP client instance with it's own settings, then pass this to the SwaggerClient.from_url() call:

""" Required to create a new Requests 'http_client' instance: """
from bravado.requests_client import Requestsclient
""" Required to create a Bravado SwaggerClient instance: """
from bravado.client import SwaggerClient

""" Create a new Requests client instance: """
http_client = RequestsClient()

从这里你可以做请求允许你做的所有有趣的事情,比如设置基本的 HTTP 身份验证:

From here you can do all the fun stuff that Requests allows you to do, like set basic HTTP authentication:

http_client.set_basic_auth(SERVER, USER, PASS)

或禁用 SSL 证书验证(不推荐在测试环境之外使用):

Or disable SSL certificate verification (Not recommended outside of a test environment):

http_client.session.verify = False

或者如您的问答所指出的那样,提供一个本地证书存储以进行验证:

Or as your question and answer point out, supply a local certificate store to verify against:

http_client.session.cert = os.environ.get('REQUESTS_CA_BUNDLE')

从那里开始,创建一个 SwaggerClient 实例,将其指向您的 swagger.json 路径,并引用请求http_client"实例(具有预定义的设置),就像这样:

From there, it's a simple matter of creating a SwaggerClient instance, pointing it to your swagger.json path, and referencing the Requests 'http_client' instance, (with pre-defined settings,) like so:

URL = 'https://myserver/api/path/to/swagger.json'
client = Swaggerclient.from_url(URL, http_client=http_client)

这篇关于如何在 Bravado 中设置自定义 http 客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆