如何在 Bravado 中设置自定义 http 客户端?

编程入门 行业动态 更新时间:2024-10-14 20:21:53
本文介绍了如何在 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 = 'myserver/api/path/to/swagger.json' client = Swaggerclient.from_url(URL, http_client=http_client)

更多推荐

如何在 Bravado 中设置自定义 http 客户端?

本文发布于:2023-11-25 23:36:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   客户端   如何在   Bravado   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!