diff --git a/contrib/endpoints/include/BUILD b/contrib/endpoints/include/BUILD index c0b31a061b2..a252eb6e3ad 100644 --- a/contrib/endpoints/include/BUILD +++ b/contrib/endpoints/include/BUILD @@ -42,6 +42,7 @@ filegroup( "api_manager/api_manager.h", "api_manager/compute_platform.h", "api_manager/env_interface.h", + "api_manager/grpc_request.h", "api_manager/http_request.h", "api_manager/method.h", "api_manager/method_call_info.h", diff --git a/contrib/endpoints/include/api_manager/env_interface.h b/contrib/endpoints/include/api_manager/env_interface.h index 6308acc6d44..d68ebb3fe6e 100644 --- a/contrib/endpoints/include/api_manager/env_interface.h +++ b/contrib/endpoints/include/api_manager/env_interface.h @@ -21,6 +21,7 @@ #include #include +#include "contrib/endpoints/include/api_manager/grpc_request.h" #include "contrib/endpoints/include/api_manager/http_request.h" #include "contrib/endpoints/include/api_manager/periodic_timer.h" #include "contrib/endpoints/include/api_manager/utils/status.h" @@ -58,6 +59,8 @@ class ApiManagerEnvInterface { // of the request // (possibly before returning). virtual void RunHTTPRequest(std::unique_ptr request) = 0; + + virtual void RunGRPCRequest(std::unique_ptr request) = 0; }; } // namespace api_manager diff --git a/contrib/endpoints/include/api_manager/grpc_request.h b/contrib/endpoints/include/api_manager/grpc_request.h new file mode 100644 index 00000000000..0eabccc09d2 --- /dev/null +++ b/contrib/endpoints/include/api_manager/grpc_request.h @@ -0,0 +1,97 @@ +/* Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef API_MANAGER_GRPC_REQUEST_H_ +#define API_MANAGER_GRPC_REQUEST_H_ + +#include +#include + +#include "contrib/endpoints/include/api_manager/utils/status.h" + +namespace google { +namespace api_manager { + +// Represents a Grpc request issued by the API Manager and +// processed by the environment. +class GRPCRequest { + public: + // GRPCRequest constructor without headers in the callback function. + GRPCRequest(std::function callback) + : callback_(callback) {} + + // A callback for the environment to invoke when the request is + // complete. This will be invoked by the environment exactly once, + // and then the environment will drop the std::unique_ptr + // referencing the GRPCRequest. + void OnComplete(utils::Status status, std::string&& body) { + callback_(status, std::move(body)); + } + + // the gRPC method to call + const std::string& method() const { return method_; } + GRPCRequest& set_method(const std::string& value) { + method_ = value; + return *this; + } + GRPCRequest& set_method(std::string&& value) { + method_ = std::move(value); + return *this; + } + + // DNS or IP address of the gRPC server. + const std::string& server() const { return server_; } + GRPCRequest& set_server(const std::string& value) { + server_ = value; + return *this; + } + GRPCRequest& set_server(std::string&& value) { + server_ = std::move(value); + return *this; + } + + // The gRPC service name. + const std::string& service() const { return service_; } + GRPCRequest& set_service(const std::string& value) { + service_ = value; + return *this; + } + GRPCRequest& set_service(std::string&& value) { + service_ = std::move(value); + return *this; + } + + // The request body serialized as string. + const std::string& body() const { return body_; } + GRPCRequest& set_body(const std::string& value) { + body_ = value; + return *this; + } + GRPCRequest& set_body(std::string&& value) { + body_ = std::move(value); + return *this; + } + + private: + std::function callback_; + std::string method_; + std::string server_; + std::string service_; + std::string body_; +}; + +} // namespace api_manager +} // namespace google + +#endif // API_MANAGER_GRPC_REQUEST_H_ diff --git a/contrib/endpoints/src/api_manager/mock_api_manager_environment.h b/contrib/endpoints/src/api_manager/mock_api_manager_environment.h index ae03eaa6f45..041dbc23ac7 100644 --- a/contrib/endpoints/src/api_manager/mock_api_manager_environment.h +++ b/contrib/endpoints/src/api_manager/mock_api_manager_environment.h @@ -29,9 +29,13 @@ class MockApiManagerEnvironment : public ApiManagerEnvInterface { std::unique_ptr(std::chrono::milliseconds, std::function)); MOCK_METHOD1(DoRunHTTPRequest, void(HTTPRequest *)); + MOCK_METHOD1(DoRunGRPCRequest, void(GRPCRequest *)); virtual void RunHTTPRequest(std::unique_ptr req) { DoRunHTTPRequest(req.get()); } + virtual void RunGRPCRequest(std::unique_ptr req) { + DoRunGRPCRequest(req.get()); + } }; // A useful mock class to log to stdout to debug Config loading failure. @@ -53,6 +57,10 @@ class MockApiManagerEnvironmentWithLog : public ApiManagerEnvInterface { std::map headers; request->OnComplete(utils::Status::OK, std::move(headers), ""); } + void RunGRPCRequest(std::unique_ptr request) { + std::map headers; + request->OnComplete(utils::Status::OK, ""); + } }; } // namespace api_manager diff --git a/src/envoy/prototype/api_manager_env.cc b/src/envoy/prototype/api_manager_env.cc index 0ef8cb004d5..b058142597e 100644 --- a/src/envoy/prototype/api_manager_env.cc +++ b/src/envoy/prototype/api_manager_env.cc @@ -127,5 +127,10 @@ void Env::RunHTTPRequest( std::move(message), *callbacks, Optional(std::chrono::milliseconds(10000))); } + +void Env::RunGRPCRequest( + std::unique_ptr request) { + // TODO: send grpc request. +} } } diff --git a/src/envoy/prototype/api_manager_env.h b/src/envoy/prototype/api_manager_env.h index 6e5e63676aa..0ae8136f5be 100644 --- a/src/envoy/prototype/api_manager_env.h +++ b/src/envoy/prototype/api_manager_env.h @@ -26,6 +26,8 @@ class Env : public google::api_manager::ApiManagerEnvInterface, std::function continuation) override; virtual void RunHTTPRequest( std::unique_ptr request) override; + virtual void RunGRPCRequest( + std::unique_ptr request) override; }; } }