我是Google api的新手,目前正在尝试为我已经构建的一些功能构建一些单元测试覆盖。Google的HttpMock看起来非常适合我想要做的事情,但是无论我做什么,我都无法使示例正常工作。从Google复制的完整示例代码是:
from apiclient.discovery import build
from apiclient.http import HttpMock
import pprint
http = HttpMock('books-discovery.json', {'status': '200'})
api_key = 'your_api_key'
service = build('books', 'v1', http=http, developerKey=api_key)
request = service.volumes().list(source='public', q='android')
http = HttpMock('books-android.json', {'status': '200'})
response = request.execute(http=http)
pprint.pprint(response)
我尝试将books-discovery.json
设置为有效的json,但仍然得到这个错误:
Traceback (most recent call last):
File "testthing.py", line 7, in <module>
service = build('books', 'v1', http=http, developerKey=api_key)
File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/apiclient/discovery.py", line 207, in build
developerKey=developerKey, model=model, requestBuilder=requestBuilder)
File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/apiclient/discovery.py", line 249, in build_from_document
base = urlparse.urljoin(service['rootUrl'], service['servicePath'])
KeyError: 'rootUrl'
我做错了什么?books-discovery.json
是否需要一些其他地方没有提到的特殊形式?
books-discovery。json需要作为构建图书API的发现服务文件和保存的响应
查看文档中的注释
在开发和测试应用程序时,保存是一个好主意像books-discovery这样的文件中的实际API响应。json或books-android。Json用于测试。
books-discovery.json
https://gist.github.com/Bachmann1234/19fddfb983022218204abooks-android.json
https://gist.github.com/Bachmann1234/a2b4207caa29dbc23e29使用这两个文件,示例运行
所以你所做的是说"与其进行这个HTTP调用,不如返回这个内容和状态"
您仍然需要提供适当的内容,以便代码能够运行。您不需要依赖HTTP调用来完成它。
对于任何其他来寻找答案的人,这里是我对Github问题的评论:
问题似乎是
build()
将使用提供的http
对象获取发现文档,即使使用HttpMock
。与缓存禁用,或者当您没有文档的缓存版本(在我的案例是在Travis CI上,而不是在我的本地机器上),这将导致它失败——取决于状态的不确定性失败你的缓存,这是了不起的。复制:
#!/usr/bin/env python from apiclient.discovery import build from apiclient.http import HttpMockSequence http = HttpMockSequence([({'status': '200'}, "{}")]) build('calendar', 'v3', cache_discovery=False, http=http)
回溯:
Traceback (most recent call last):
File "reproduce-bug.py", line 7, in <module>
build('calendar', 'v3', cache_discovery=False, http=http)
File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py",line
credentials=credentials)
File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py", line 318, in build_from_document
base = urljoin(service['rootUrl'], service['servicePath'])
KeyError: 'rootUrl'
在绞尽脑汁之后,我发现了一个解决方法:简单地调用build('calendar', 'v3')
(或者,在您的情况下,build('books', 'v1')
),在使用mock之前没有其他参数