首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在客户端中保存响应的json转储?

如何在客户端中保存响应的json转储?
EN

Stack Overflow用户
提问于 2022-02-13 19:37:03
回答 1查看 189关注 0票数 0

在有关嘲弄的python文档中指出:

在您开发和测试应用程序时,最好将实际的API响应保存在文件中,如books good y.json或books android.json,以便在测试中使用。

我从哪弄到这些json的?特别是模拟build()命令的响应。

编辑:

我想测试的类,calendar.py:

代码语言:javascript
运行
复制
from google.oauth2 import service_account
from googleapiclient.discovery import build


class Calendar:
    def __init__(self, credentials_file, calendar_id) -> None:
        credentials = service_account.Credentials.from_service_account_file(
            credentials_file, scopes=["https://www.googleapis.com/auth/calendar"]
        )
        self.service = build("calendar", "v3", credentials=credentials)
        self._id = calendar_id

如何检索构建的json响应以保存在calendar-discovery.json

修改类以简化模拟

代码语言:javascript
运行
复制
import google_auth_httplib2
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import build_http

class Calendar:
    def __init__(self, credentials_file, calendar_id) -> None:
        credentials = service_account.Credentials.from_service_account_file(
            credentials_file, scopes=["https://www.googleapis.com/auth/calendar"]
        )
        http = google_auth_httplib2.AuthorizedHttp(credentials, http=build_http())
        self.service = build("calendar", "v3", http=http)
        self._id = calendar_id

测试:

代码语言:javascript
运行
复制
def test_calendar_initialization(mocker):
    mock = mocker.patch("meal_planner.calendar.build_http")
    mock.return_value = HttpMock("calendar-discovery.json", {"status": 200})

    calendar = Calendar(credentials_file="credential.json", calendar_id="id")
EN

回答 1

Stack Overflow用户

发布于 2022-03-02 18:49:06

  1. 列出所有的发现API:https://discovery.googleapis.com/discovery/v1/apis
  2. 搜索"name": "calendar"
  3. 寻找"discoveryRestUrl": "https://calendar-json.googleapis.com/$discovery/rest?version=v3"
  4. 获取日历发现文档curl https://calendar-json.googleapis.com/$discovery/rest?version=v3 >日历-curl y.json‘

参考资料:https://developers.google.com/discovery

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71104201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档