问题
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?
回答1:
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):
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)
and finally, be sure to monkey patch the module before actually creating an API:
api_config.ApiConfigGenerator = NewConfigGenerator
This could be done, for example, by putting this monkey patch in a file like monkey_patch.py
and then using
import monkey_patch
in the file where your API classes are defined. Hope this helps.
来源:https://stackoverflow.com/questions/16532502/how-do-i-specify-my-own-icons-so-they-show-up-in-a-google-endpoints-api-discover