问题描述
我试图通过猴子补丁ApiConfigGenerator.get_descriptor_defaults
来使图标部分包含我自己的图标,而不是Google搜索图标.不幸的是,在最终确定发现文档时,将忽略/丢弃这些信息.
I've tried to get the icons section to contain my own icons, rather than google search ones by trying to monkey patch ApiConfigGenerator.get_descriptor_defaults
. Unfortunately these are ignored/discarded when the discovery document is finalized.
{
"kind": "discovery#restDescription",
"etag": "...",
"discoveryVersion": "v1",
"id": "acme:v1",
"name": "acme",
"version": "v1",
"description": "Acme API",
"ownerDomain": "google.com",
"ownerName": "Google",
"icons": {
"x16": "http://www.google.com/images/icons/product/search-16.gif", # <-- acme icon url here!
"x32": "http://www.google.com/images/icons/product/search-32.gif" # <--
},
"protocol": "rest",
"baseUrl": "http://acme.appspot.com/_ah/api/acme/v1/",
"basePath": "/_ah/api/acme/v1/",
"rootUrl": "http://acme.appspot.com/_ah/api/",
"servicePath": "acme/v1/",
"batchPath": "batch",
"parameters": {
如果有的话,我该如何解决?
How do I work around this, if at all?
推荐答案
google.appengine.api.endpoints
库中尚不支持此功能,但是如果在API配置中指定了icon16
和icon32
,则可以实现.为此,您可以在ApiConfigGenerator
类上对pretty_print_config_to_json
方法进行猴子补丁,或对其进行子类化,然后在google.appengine.ext.endpoints.api_config
模块中对猴子进行类补丁.
This is not supported yet in the google.appengine.api.endpoints
library but is possible if you specify icon16
and icon32
in the API config. To do this, you can monkey patch the pretty_print_config_to_json
method on the ApiConfigGenerator
class, or subclass it and monkey patch the class in the google.appengine.ext.endpoints.api_config
module.
例如,我们导入模块并在猴子修补模块之前保存原始类:
For example, we import the module and save the original class before monkey patching the module:
import json
from google.appengine.ext.endpoints import api_config
OriginalConfigGenerator = api_config.ApiConfigGenerator
然后我们定义一个子类来替换它:
Then we define a subclass to replace it:
class NewConfigGenerator(OriginalConfigGenerator):
,然后覆盖产生API配置的方法:
and then override the method which produces the API config:
def pretty_print_config_to_json(self, services, hostname=None):
descriptor = super(NewConfigGenerator, self).pretty_print_config_to_json(
services, hostname=hostname)
descriptor = json.loads(descriptor)
一旦有了配置字典,就可以添加自己的密钥并返回它:
once you have the config dictionary, you can add your own keys and return it:
descriptor['icon16'] = 'YOUR-16x16-ICON-LINK'
descriptor['icon32'] = 'YOUR-32x32-ICON-LINK'
# This will be slower than overwriting __api_descriptor
# due to the json.parse/json.loads having to occur twice
# but hacking around private methods is a pain.
return json.dumps(descriptor, sort_keys=True, indent=2)
最后,确保在实际创建API之前先对模块进行猴子补丁:
and finally, be sure to monkey patch the module before actually creating an API:
api_config.ApiConfigGenerator = NewConfigGenerator
例如,可以通过将该猴子补丁放入monkey_patch.py
这样的文件中,然后使用
This could be done, for example, by putting this monkey patch in a file like monkey_patch.py
and then using
import monkey_patch
在定义您的API类的文件中.希望这会有所帮助.
in the file where your API classes are defined. Hope this helps.
这篇关于如何指定我自己的图标,以便它们显示在Google Endpoints API发现文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!