理解类继承来干涸一些鳕鱼

2024-05-17 09:02:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用cloudantpython库连接到我的cloudant帐户。你知道吗

以下是我目前掌握的代码:

import cloudant


class WorkflowsCloudant(cloudant.Account):
    def __init__(self):
        super(WorkflowsCloudant, self).__init__(settings.COUCH_DB_ACCOUNT_NAME,
                                                auth=(settings.COUCH_PUBLIC_KEY,
                                                      settings.COUCH_PRIVATE_KEY))

    @blueprint.route('/<workflow_id>')
    def get_single_workflow(account_id, workflow_id):
        account = WorkflowsCloudant()
        db = account.database(settings.COUCH_DB_NAME)
        doc = db.document(workflow_id)
        resp = doc.get().json()

        if resp['account_id'] != account_id:
            return error_helpers.forbidden('Invalid Account')

        return jsonify(resp)

这个Flask控制器内部将有CRUD操作,但是对于当前的实现,在对要查看/操作的文档执行操作之前,我必须在每个方法中设置accountdb变量。如何清理(或干涸)代码,以便只需调用主WorkflowsCloudant类?你知道吗


Tags: 代码selfiddbsettingsinitdefaccount
2条回答

不能将RestTemplate用于异步请求,这就是AsyncRestTemplate的用途。您需要实现自己的AsyncClientHttpRequestFactory。我简单地查看了你在帖子中提供的链接,看起来你可以包装一个AsyncRestClient并从AsyncClientHttpRequestFactory.createAsyncRequest返回BoundRequestBuilder。接下来,基本上需要将调用从特定于Spring的接口委托给特定于AsyncRestClient的类。这不应该太难

也就是说,Spring5Web附带了一个WebClient来实现异步等功能。我建议在构建自己的异步库之前认真考虑一下,尽管是在另一个库的基础上

这是spring RestTemplate的官方java文档

Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.

编辑: 好了,给你一个填鸭式的回答:

AsyncRestTemplate template = new AsyncRestTemplate(
                new HttpComponentsAsyncClientHttpRequestFactory());

HttpComponentsAsyncClientHttpRequestFactory自4.0以来就是spring的一部分

相关问题 更多 >