Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/endpoints/include/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions contrib/endpoints/include/api_manager/env_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <functional>
#include <memory>

#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"
Expand Down Expand Up @@ -58,6 +59,8 @@ class ApiManagerEnvInterface {
// of the request
// (possibly before returning).
virtual void RunHTTPRequest(std::unique_ptr<HTTPRequest> request) = 0;

virtual void RunGRPCRequest(std::unique_ptr<GRPCRequest> request) = 0;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


} // namespace api_manager
Expand Down
97 changes: 97 additions & 0 deletions contrib/endpoints/include/api_manager/grpc_request.h
Original file line number Diff line number Diff line change
@@ -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 <functional>
#include <string>

#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<void(utils::Status, std::string&&)> 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_; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment
// the gRPC method to call

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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_; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// The gRPC service name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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_; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// The request body serialized as string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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<void(utils::Status, std::string&&)> callback_;
std::string method_;
std::string server_;
std::string service_;
std::string body_;
};

} // namespace api_manager
} // namespace google

#endif // API_MANAGER_GRPC_REQUEST_H_
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ class MockApiManagerEnvironment : public ApiManagerEnvInterface {
std::unique_ptr<PeriodicTimer>(std::chrono::milliseconds,
std::function<void()>));
MOCK_METHOD1(DoRunHTTPRequest, void(HTTPRequest *));
MOCK_METHOD1(DoRunGRPCRequest, void(GRPCRequest *));
virtual void RunHTTPRequest(std::unique_ptr<HTTPRequest> req) {
DoRunHTTPRequest(req.get());
}
virtual void RunGRPCRequest(std::unique_ptr<GRPCRequest> req) {
DoRunGRPCRequest(req.get());
}
};

// A useful mock class to log to stdout to debug Config loading failure.
Expand All @@ -53,6 +57,10 @@ class MockApiManagerEnvironmentWithLog : public ApiManagerEnvInterface {
std::map<std::string, std::string> headers;
request->OnComplete(utils::Status::OK, std::move(headers), "");
}
void RunGRPCRequest(std::unique_ptr<GRPCRequest> request) {
std::map<std::string, std::string> headers;
request->OnComplete(utils::Status::OK, "");
}
};

} // namespace api_manager
Expand Down
5 changes: 5 additions & 0 deletions src/envoy/prototype/api_manager_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,10 @@ void Env::RunHTTPRequest(
std::move(message), *callbacks,
Optional<std::chrono::milliseconds>(std::chrono::milliseconds(10000)));
}

void Env::RunGRPCRequest(
std::unique_ptr<google::api_manager::GRPCRequest> request) {
// TODO: send grpc request.
}
}
}
2 changes: 2 additions & 0 deletions src/envoy/prototype/api_manager_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Env : public google::api_manager::ApiManagerEnvInterface,
std::function<void()> continuation) override;
virtual void RunHTTPRequest(
std::unique_ptr<google::api_manager::HTTPRequest> request) override;
virtual void RunGRPCRequest(
std::unique_ptr<google::api_manager::GRPCRequest> request) override;
};
}
}