Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: make HttpResponse fluent #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 21, 2021
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloWorld(HttpRequest) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload("Hello World\n");
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
}
```

Expand Down
7 changes: 3 additions & 4 deletions examples/hello_from_namespace/hello_from_namespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloWorld(HttpRequest) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload("Hello from a C++ namespace!\n");
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a C++ namespace!\n");
}

} // namespace hello_from_namespace
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloWorld(HttpRequest) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload("Hello from a nested C++ namespace!\n");
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a nested C++ namespace!\n");
}

} // namespace hello_from_nested_namespace::ns0::ns1
11 changes: 4 additions & 7 deletions examples/hello_gcs/hello_gcs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ namespace gcs = ::google::cloud::storage;

HttpResponse HelloGcs(HttpRequest request) { // NOLINT
auto error = [] {
HttpResponse response;
response.set_result(HttpResponse::kBadRequest);
return response;
return HttpResponse{}.set_result(HttpResponse::kBadRequest);
};

std::vector<std::string> components;
Expand All @@ -42,8 +40,7 @@ HttpResponse HelloGcs(HttpRequest request) { // NOLINT
std::istreambuf_iterator<char>{});
if (!reader.status().ok()) return error();

HttpResponse response;
response.set_header("Content-Type", "application/octet-stream");
response.set_payload(std::move(contents));
return response;
return HttpResponse{}
.set_header("Content-Type", "application/octet-stream")
.set_payload(std::move(contents));
}
7 changes: 3 additions & 4 deletions examples/hello_multiple_sources/hello_multiple_sources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloMultipleSources(HttpRequest) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload(Greeting());
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(Greeting());
}
7 changes: 3 additions & 4 deletions examples/hello_with_third_party/hello_with_third_party.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloWithThirdParty(HttpRequest request) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload(fmt::format("Hello at {}\n", request.target()));
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(fmt::format("Hello at {}\n", request.target()));
}
7 changes: 3 additions & 4 deletions examples/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;

HttpResponse HelloWorld(HttpRequest) { // NOLINT
HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload("Hello World\n");
return response;
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ gcf::HttpResponse concepts_after_response(
for (int i = 0; i != kIterations; ++i) sum += i;
return sum;
});
gcf::HttpResponse response;
response.set_payload("Hello World!");
return response;
return gcf::HttpResponse{}.set_payload("Hello World!");
}
// [END functions_concepts_after_response]
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ gcf::HttpResponse concepts_after_timeout(gcf::HttpRequest request) { // NOLINT
using std::chrono::minutes;
std::cout << "Function running..." << std::endl;
if (request.verb() == "GET") std::this_thread::sleep_for(minutes(2));
gcf::HttpResponse response;
response.set_payload("Function completed!");
std::cout << "Function completed!" << std::endl;
return response;
return gcf::HttpResponse{}.set_payload("Function completed!");
}
// [END functions_concepts_after_timeout]
7 changes: 3 additions & 4 deletions examples/site/concepts_filesystem/concepts_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ gcf::HttpResponse concepts_filesystem(gcf::HttpRequest /*request*/) { // NOLINT
payload += p.path().generic_string();
payload += "\n";
}
gcf::HttpResponse response;
response.set_header("content-type", "text/plain");
response.set_payload(payload);
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload(payload);
}
// [END functions_concepts_after_timeout]
6 changes: 2 additions & 4 deletions examples/site/concepts_request/concepts_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ unsigned int make_http_request(std::string const& host);
gcf::HttpResponse concepts_request(gcf::HttpRequest /*request*/) { // NOLINT
std::string const host = "example.com";
auto const code = make_http_request(host);
gcf::HttpResponse response;
response.set_payload("Received code " + std::to_string(code) + " from " +
host);
return response;
return gcf::HttpResponse{}.set_payload(
"Received code " + std::to_string(code) + " from " + host);
}
// [END functions_concepts_requests]

Expand Down
5 changes: 2 additions & 3 deletions examples/site/concepts_stateless/concepts_stateless.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ std::atomic<int> count{0};
} // namespace

gcf::HttpResponse concepts_stateless(gcf::HttpRequest /*request*/) { // NOLINT
gcf::HttpResponse response;
response.set_payload("Instance execution count: " + std::to_string(++count));
return response;
return gcf::HttpResponse{}.set_payload("Instance execution count: " +
std::to_string(++count));
}
// [END functions_concepts_stateless]
4 changes: 1 addition & 3 deletions examples/site/env_vars/env_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace gcf = ::google::cloud::functions;
gcf::HttpResponse env_vars(gcf::HttpRequest /*request*/) { // NOLINT
char const* value = std::getenv("FOO");
if (value == nullptr) value = "FOO environment variable is not set";
gcf::HttpResponse response;
response.set_payload(value);
return response;
return gcf::HttpResponse{}.set_payload(value);
}
// [END functions_env_vars]
12 changes: 5 additions & 7 deletions examples/site/hello_world_error/hello_world_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ namespace gcf = ::google::cloud::functions;
gcf::HttpResponse hello_world_error(gcf::HttpRequest request) { // NOLINT
if (request.target() == "/return500") {
// An error response code does NOT create entries in Error Reporting
gcf::HttpResponse response;
response.set_result(gcf::HttpResponse::kInternalServerError);
return response;
return gcf::HttpResponse{}.set_result(
gcf::HttpResponse::kInternalServerError);
}
// Unstructured logs to stdout and/or stderr do NOT create entries in Error
// Reporting
Expand All @@ -50,9 +49,8 @@ gcf::HttpResponse hello_world_error(gcf::HttpRequest request) { // NOLINT
.dump()
<< std::endl;

gcf::HttpResponse response;
response.set_payload("Hello World!");
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
}
// [END functions_helloworld_error]
7 changes: 3 additions & 4 deletions examples/site/hello_world_get/hello_world_get.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
namespace gcf = ::google::cloud::functions;

gcf::HttpResponse hello_world_get(gcf::HttpRequest) { // NOLINT
gcf::HttpResponse response;
response.set_payload("Hello World!");
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
}
// [END functions_helloworld_get]
7 changes: 3 additions & 4 deletions examples/site/hello_world_http/hello_world_http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
return std::string("Hello World!");
};

gcf::HttpResponse response;
response.set_payload(greeting());
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload(greeting());
}
// [END functions_helloworld_http]
7 changes: 3 additions & 4 deletions examples/site/howto_create_container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
return std::string("Hello World!");
};

gcf::HttpResponse response;
response.set_payload(greeting());
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload(greeting());
}
```

Expand Down
7 changes: 3 additions & 4 deletions examples/site/howto_deploy_to_cloud_run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
return std::string("Hello World!");
};

gcf::HttpResponse response;
response.set_payload(greeting());
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload(greeting());
}
```

Expand Down
7 changes: 3 additions & 4 deletions examples/site/howto_local_development/local_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ namespace gcf = ::google::cloud::functions;
namespace {

gcf::HttpResponse HelloWithShutdown(gcf::HttpRequest const& /*request*/) {
gcf::HttpResponse response;
response.set_header("Content-Type", "text/plain");
response.set_payload("Hello World\n");
return response;
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
}

} // namespace
Expand Down
7 changes: 3 additions & 4 deletions examples/site/howto_offload_builder_creation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
return std::string("Hello World!");
};

gcf::HttpResponse response;
response.set_payload(greeting());
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload(greeting());
}
```

Expand Down
7 changes: 3 additions & 4 deletions examples/site/http_content/http_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ gcf::HttpResponse http_content(gcf::HttpRequest request) { // NOLINT
}
}

gcf::HttpResponse response;
response.set_payload("Hello " + name);
response.set_header("content-type", "text/plain");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello " + name);
}
// [END functions_http_content]

Expand Down
22 changes: 10 additions & 12 deletions examples/site/http_cors/http_cors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@ gcf::HttpResponse http_cors(gcf::HttpRequest request) { // NOLINT
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
gcf::HttpResponse response;
response.set_result(gcf::HttpResponse::kNoContent);
response.set_header("Access-Control-Allow-Origin", "*");
response.set_header("Access-Control-Allow-Methods", "GET");
response.set_header("Access-Control-Allow-Headers", "Content-Type");
response.set_header("Access-Control-Max-Age", "3600");
return response;
return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "*")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Content-Type")
.set_header("Access-Control-Max-Age", "3600");
}

gcf::HttpResponse response;
response.set_header("content-type", "text/plain");
response.set_payload("Hello World!");
response.set_header("Access-Control-Allow-Origin", "*");
return response;
return gcf::HttpResponse{}
.set_header("Access-Control-Allow-Origin", "*")
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
}
// [END functions_http_cors]
26 changes: 12 additions & 14 deletions examples/site/http_cors_auth/http_cors_auth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ gcf::HttpResponse http_cors_auth(gcf::HttpRequest request) { // NOLINT
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
gcf::HttpResponse response;
response.set_result(gcf::HttpResponse::kNoContent);
response.set_header("Access-Control-Allow-Origin", "https://mydomain.com");
response.set_header("Access-Control-Allow-Methods", "GET");
response.set_header("Access-Control-Allow-Headers", "Authorization");
response.set_header("Access-Control-Max-Age", "3600");
response.set_header("Access-Control-Allow-Credentials", "true");
return response;
return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "https://mydomain.com")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Authorization")
.set_header("Access-Control-Max-Age", "3600")
.set_header("Access-Control-Allow-Credentials", "true");
}

gcf::HttpResponse response;
response.set_header("content-type", "text/plain");
response.set_payload("Hello World!");
response.set_header("Access-Control-Allow-Origin", "https://mydomain.com");
response.set_header("Access-Control-Allow-Credentials", "true");
return response;
return gcf::HttpResponse{}
.set_header("Access-Control-Allow-Origin", "https://mydomain.com")
.set_header("Access-Control-Allow-Credentials", "true")
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
}
// [END functions_http_cors_auth]
15 changes: 6 additions & 9 deletions examples/site/http_method/http_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ namespace gcf = ::google::cloud::functions;

gcf::HttpResponse http_method(gcf::HttpRequest request) { // NOLINT
if (request.verb() == "GET") {
gcf::HttpResponse response;
response.set_header("content-type", "text/plain");
response.set_payload("Hello World!");
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
}

gcf::HttpResponse response;
response.set_result(request.verb() == "POST"
? gcf::HttpResponse::kForbidden
: gcf::HttpResponse::kMethodNotAllowed);
return response;
return gcf::HttpResponse{}.set_result(
request.verb() == "POST" ? gcf::HttpResponse::kForbidden
: gcf::HttpResponse::kMethodNotAllowed);
}
// [END functions_http_method]
7 changes: 3 additions & 4 deletions examples/site/http_xml/http_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ gcf::HttpResponse http_xml(gcf::HttpRequest request) { // NOLINT
boost::property_tree::read_xml(is, data);

auto name = data.get<std::string>("name", "World");
gcf::HttpResponse response;
response.set_header("content-type", "text/plain");
response.set_payload("Hello " + name);
return response;
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello " + name);
}
// [END functions_http_xml]
Loading