def _get_rpc_or_rest(self, api_format, request, start_response): """Sends back HTTP response with API directory. This calls start_response and returns the response body. It will return the discovery doc for the requested api/version. Args: api_format: A string containing either 'rest' or 'rpc'. request: An ApiRequest, the transformed request sent to the Discovery API. start_response: A function with semantics defined in PEP-333. Returns: A string, the response body. """ api = request.body_json['api'] version = request.body_json['version'] # Create a lookup key including the root of the request lookup_key = (api, version, self._get_actual_root(request)) config = (self._config_manager.configs.get(lookup_key) or self._generate_api_config_with_root(request)) if not config: logging.warn('No discovery doc for version %s of api %s', version, api) return util.send_wsgi_not_found_response(start_response) doc = self._discovery_proxy.generate_discovery_doc(config, api_format) if not doc: error_msg = ('Failed to convert .api to discovery doc for ' 'version %s of api %s') % (version, api) logging.error('%s', error_msg) return util.send_wsgi_error_response(error_msg, start_response) return self._send_success_response(doc, start_response)
def handle_discovery_request(self, path, request, start_response): """Returns the result of a discovery service request. This calls start_response and returns the response body. Args: path: A string containing the API path (the portion of the path after /_ah/api/). request: An ApiRequest, the transformed request sent to the Discovery API. start_response: A function with semantics defined in PEP-333. Returns: The response body. Or returns False if the request wasn't handled by DiscoveryService. """ if path == self._GET_REST_API: return self._get_rest_doc(request, start_response) elif path == self._GET_RPC_API: error_msg = ('RPC format documents are no longer supported with the ' 'Endpoints Framework for Python. Please use the REST ' 'format.') logging.error('%s', error_msg) return util.send_wsgi_error_response(error_msg, start_response) elif path == self._LIST_API: return self._list(request, start_response) return False
def _get_rest_doc(self, request, start_response): """Sends back HTTP response with API directory. This calls start_response and returns the response body. It will return the discovery doc for the requested api/version. Args: request: An ApiRequest, the transformed request sent to the Discovery API. start_response: A function with semantics defined in PEP-333. Returns: A string, the response body. """ api = request.body_json['api'] version = request.body_json['version'] generator = discovery_generator.DiscoveryGenerator(request=request) services = [ s for s in self._backend.api_services if s.api_info.name == api and s.api_info.version == version ] doc = generator.pretty_print_config_to_json(services) if not doc: error_msg = ('Failed to convert .api to discovery doc for ' 'version %s of api %s') % (version, api) logging.error('%s', error_msg) return util.send_wsgi_error_response(error_msg, start_response) return self._send_success_response(doc, start_response)
def fail_request(self, orig_request, message, start_response): """Write an immediate failure response to outfile, no redirect. This calls start_response and returns the error body. Args: orig_request: An ApiRequest, the original request from the user. message: A string containing the error message to be displayed to user. start_response: A function with semantics defined in PEP-333. Returns: A string containing the body of the error response. """ cors_handler = self._create_cors_handler(orig_request) return util.send_wsgi_error_response( message, start_response, cors_handler=cors_handler)
def fail_request(self, orig_request, message, start_response): """Write an immediate failure response to outfile, no redirect. This calls start_response and returns the error body. Args: orig_request: An ApiRequest, the original request from the user. message: A string containing the error message to be displayed to user. start_response: A function with semantics defined in PEP-333. Returns: A string containing the body of the error response. """ cors_handler = self._create_cors_handler(orig_request) return util.send_wsgi_error_response( message, start_response, cors_handler=cors_handler)
def _get_rest_doc(self, request, start_response): """Sends back HTTP response with API directory. This calls start_response and returns the response body. It will return the discovery doc for the requested api/version. Args: request: An ApiRequest, the transformed request sent to the Discovery API. start_response: A function with semantics defined in PEP-333. Returns: A string, the response body. """ api = request.body_json['api'] version = request.body_json['version'] generator = discovery_generator.DiscoveryGenerator(request=request) doc = generator.pretty_print_config_to_json(self._backend.api_services) if not doc: error_msg = ('Failed to convert .api to discovery doc for ' 'version %s of api %s') % (version, api) logging.error('%s', error_msg) return util.send_wsgi_error_response(error_msg, start_response) return self._send_success_response(doc, start_response)