Skip to content

Commit c9f7f31

Browse files
authored
refactor(rest): seperate interface classes into own headers (#8444)
1 parent df6fa36 commit c9f7f31

18 files changed

+235
-118
lines changed

google/cloud/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,17 @@ if (GOOGLE_CLOUD_CPP_ENABLE_REST)
633633
internal/curl_handle.h
634634
internal/curl_handle_factory.cc
635635
internal/curl_handle_factory.h
636+
internal/curl_http_payload.cc
637+
internal/curl_http_payload.h
636638
internal/curl_impl.cc
637639
internal/curl_impl.h
638640
internal/curl_options.h
641+
internal/curl_rest_client.cc
642+
internal/curl_rest_client.h
643+
internal/curl_rest_response.cc
644+
internal/curl_rest_response.h
639645
internal/curl_wrappers.cc
640646
internal/curl_wrappers.h
641-
internal/http_payload.cc
642647
internal/http_payload.h
643648
internal/make_jwt_assertion.cc
644649
internal/make_jwt_assertion.h
@@ -669,12 +674,10 @@ if (GOOGLE_CLOUD_CPP_ENABLE_REST)
669674
internal/oauth2_service_account_credentials.h
670675
internal/openssl_util.cc
671676
internal/openssl_util.h
672-
internal/rest_client.cc
673677
internal/rest_client.h
674678
internal/rest_options.h
675679
internal/rest_request.cc
676680
internal/rest_request.h
677-
internal/rest_response.cc
678681
internal/rest_response.h
679682
internal/unified_rest_credentials.cc
680683
internal/unified_rest_credentials.h)
@@ -730,9 +733,11 @@ if (GOOGLE_CLOUD_CPP_ENABLE_REST)
730733
internal/binary_data_as_debug_string_test.cc
731734
internal/curl_handle_factory_test.cc
732735
internal/curl_handle_test.cc
736+
internal/curl_http_payload_test.cc
733737
internal/curl_impl_test.cc
738+
internal/curl_rest_client_test.cc
739+
internal/curl_rest_response_test.cc
734740
internal/curl_wrappers_test.cc
735-
internal/http_payload_test.cc
736741
internal/make_jwt_assertion_test.cc
737742
internal/oauth2_access_token_credentials_test.cc
738743
internal/oauth2_anonymous_credentials_test.cc
@@ -745,16 +750,14 @@ if (GOOGLE_CLOUD_CPP_ENABLE_REST)
745750
internal/oauth2_refreshing_credentials_wrapper_test.cc
746751
internal/oauth2_service_account_credentials_test.cc
747752
internal/openssl_util_test.cc
748-
internal/rest_client_test.cc
749753
internal/rest_request_test.cc
750-
internal/rest_response_test.cc
751754
internal/unified_rest_credentials_test.cc)
752755

753756
# List the emulator integration tests, then setup the targets and
754757
# dependencies.
755758
set(google_cloud_cpp_rest_internal_emulator_integration_tests
756759
# cmake-format: sort
757-
internal/rest_client_integration_test.cc)
760+
internal/curl_rest_client_integration_test.cc)
758761

759762
# List the production integration tests, then setup the targets and
760763
# dependencies.

google/cloud/google_cloud_cpp_rest_internal.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ google_cloud_cpp_rest_internal_hdrs = [
2020
"internal/binary_data_as_debug_string.h",
2121
"internal/curl_handle.h",
2222
"internal/curl_handle_factory.h",
23+
"internal/curl_http_payload.h",
2324
"internal/curl_impl.h",
2425
"internal/curl_options.h",
26+
"internal/curl_rest_client.h",
27+
"internal/curl_rest_response.h",
2528
"internal/curl_wrappers.h",
2629
"internal/http_payload.h",
2730
"internal/make_jwt_assertion.h",
@@ -50,9 +53,11 @@ google_cloud_cpp_rest_internal_srcs = [
5053
"internal/binary_data_as_debug_string.cc",
5154
"internal/curl_handle.cc",
5255
"internal/curl_handle_factory.cc",
56+
"internal/curl_http_payload.cc",
5357
"internal/curl_impl.cc",
58+
"internal/curl_rest_client.cc",
59+
"internal/curl_rest_response.cc",
5460
"internal/curl_wrappers.cc",
55-
"internal/http_payload.cc",
5661
"internal/make_jwt_assertion.cc",
5762
"internal/oauth2_access_token_credentials.cc",
5863
"internal/oauth2_anonymous_credentials.cc",
@@ -67,8 +72,6 @@ google_cloud_cpp_rest_internal_srcs = [
6772
"internal/oauth2_refreshing_credentials_wrapper.cc",
6873
"internal/oauth2_service_account_credentials.cc",
6974
"internal/openssl_util.cc",
70-
"internal/rest_client.cc",
7175
"internal/rest_request.cc",
72-
"internal/rest_response.cc",
7376
"internal/unified_rest_credentials.cc",
7477
]

google/cloud/google_cloud_cpp_rest_internal_emulator_integration_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"""Automatically generated unit tests list - DO NOT EDIT."""
1818

1919
google_cloud_cpp_rest_internal_emulator_integration_tests = [
20-
"internal/rest_client_integration_test.cc",
20+
"internal/curl_rest_client_integration_test.cc",
2121
]

google/cloud/google_cloud_cpp_rest_internal_unit_tests.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ google_cloud_cpp_rest_internal_unit_tests = [
2020
"internal/binary_data_as_debug_string_test.cc",
2121
"internal/curl_handle_factory_test.cc",
2222
"internal/curl_handle_test.cc",
23+
"internal/curl_http_payload_test.cc",
2324
"internal/curl_impl_test.cc",
25+
"internal/curl_rest_client_test.cc",
26+
"internal/curl_rest_response_test.cc",
2427
"internal/curl_wrappers_test.cc",
25-
"internal/http_payload_test.cc",
2628
"internal/make_jwt_assertion_test.cc",
2729
"internal/oauth2_access_token_credentials_test.cc",
2830
"internal/oauth2_anonymous_credentials_test.cc",
@@ -35,8 +37,6 @@ google_cloud_cpp_rest_internal_unit_tests = [
3537
"internal/oauth2_refreshing_credentials_wrapper_test.cc",
3638
"internal/oauth2_service_account_credentials_test.cc",
3739
"internal/openssl_util_test.cc",
38-
"internal/rest_client_test.cc",
3940
"internal/rest_request_test.cc",
40-
"internal/rest_response_test.cc",
4141
"internal/unified_rest_credentials_test.cc",
4242
]

google/cloud/internal/ci/run_integration_tests_emulator_bazel.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ emulator_args=(
5252
# We need to forward some environment variables suitable for running against
5353
# the emulator.
5454
"${BAZEL_BIN}" "${BAZEL_VERB}" "${bazel_test_args[@]}" "${emulator_args[@]}" \
55-
"//google/cloud:internal_rest_client_integration_test"
55+
"//google/cloud:internal_curl_rest_client_integration_test"
5656

5757
exit_status=$?
5858

google/cloud/internal/http_payload.cc renamed to google/cloud/internal/curl_http_payload.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "google/cloud/internal/http_payload.h"
15+
#include "google/cloud/internal/curl_http_payload.h"
1616
#include "google/cloud/internal/curl_impl.h"
1717
#include "google/cloud/log.h"
1818

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_HTTP_PAYLOAD_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_HTTP_PAYLOAD_H
17+
18+
#include "google/cloud/internal/http_payload.h"
19+
#include "google/cloud/options.h"
20+
#include "google/cloud/status_or.h"
21+
#include "google/cloud/version.h"
22+
#include "absl/types/span.h"
23+
#include <map>
24+
25+
namespace google {
26+
namespace cloud {
27+
namespace rest_internal {
28+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
29+
30+
class CurlImpl;
31+
32+
// Implementation using libcurl.
33+
class CurlHttpPayload : public HttpPayload {
34+
public:
35+
~CurlHttpPayload() override = default;
36+
37+
CurlHttpPayload(CurlHttpPayload const&) = delete;
38+
CurlHttpPayload(CurlHttpPayload&&) = default;
39+
CurlHttpPayload& operator=(CurlHttpPayload const&) = delete;
40+
CurlHttpPayload& operator=(CurlHttpPayload&&) = default;
41+
42+
StatusOr<std::size_t> Read(absl::Span<char> buffer) override;
43+
44+
private:
45+
friend class CurlRestResponse;
46+
CurlHttpPayload(std::unique_ptr<CurlImpl> impl, Options options);
47+
48+
std::unique_ptr<CurlImpl> impl_;
49+
Options options_;
50+
};
51+
52+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
53+
} // namespace rest_internal
54+
} // namespace cloud
55+
} // namespace google
56+
57+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_HTTP_PAYLOAD_H

google/cloud/internal/http_payload_test.cc renamed to google/cloud/internal/curl_http_payload_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "google/cloud/internal/http_payload.h"
15+
#include "google/cloud/internal/curl_http_payload.h"
1616
#include "google/cloud/testing_util/mock_http_payload.h"
1717
#include "google/cloud/testing_util/status_matchers.h"
1818
#include <gmock/gmock.h>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "google/cloud/internal/rest_client.h"
15+
#include "google/cloud/internal/curl_rest_client.h"
1616
#include "google/cloud/credentials.h"
1717
#include "google/cloud/internal/absl_str_cat_quiet.h"
1818
#include "google/cloud/internal/absl_str_join_quiet.h"
1919
#include "google/cloud/internal/api_client_header.h"
2020
#include "google/cloud/internal/curl_handle_factory.h"
2121
#include "google/cloud/internal/curl_impl.h"
2222
#include "google/cloud/internal/curl_options.h"
23+
#include "google/cloud/internal/curl_rest_response.h"
2324
#include "google/cloud/internal/oauth2_google_credentials.h"
2425
#include "google/cloud/internal/rest_options.h"
2526
#include "google/cloud/internal/unified_rest_credentials.h"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_REST_CLIENT_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_REST_CLIENT_H
17+
18+
#include "google/cloud/internal/rest_client.h"
19+
#include "google/cloud/internal/rest_options.h"
20+
#include "google/cloud/internal/rest_request.h"
21+
#include "google/cloud/internal/rest_response.h"
22+
#include "google/cloud/options.h"
23+
#include "google/cloud/status_or.h"
24+
#include "google/cloud/version.h"
25+
#include <string>
26+
#include <utility>
27+
#include <vector>
28+
29+
namespace google {
30+
namespace cloud {
31+
namespace rest_internal {
32+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
33+
34+
class CurlHandleFactory;
35+
class CurlImpl;
36+
37+
// RestClient implementation using libcurl.
38+
class CurlRestClient : public RestClient {
39+
public:
40+
static std::string HostHeader(Options const& options,
41+
std::string const& default_endpoint);
42+
~CurlRestClient() override = default;
43+
44+
CurlRestClient(CurlRestClient const&) = delete;
45+
CurlRestClient(CurlRestClient&&) = default;
46+
CurlRestClient& operator=(CurlRestClient const&) = delete;
47+
CurlRestClient& operator=(CurlRestClient&&) = default;
48+
49+
StatusOr<std::unique_ptr<RestResponse>> Delete(
50+
RestRequest const& request) override;
51+
StatusOr<std::unique_ptr<RestResponse>> Get(
52+
RestRequest const& request) override;
53+
StatusOr<std::unique_ptr<RestResponse>> Patch(
54+
RestRequest const& request,
55+
std::vector<absl::Span<char const>> const& payload) override;
56+
StatusOr<std::unique_ptr<RestResponse>> Post(
57+
RestRequest const& request,
58+
std::vector<absl::Span<char const>> const& payload) override;
59+
StatusOr<std::unique_ptr<RestResponse>> Post(
60+
RestRequest request,
61+
std::vector<std::pair<std::string, std::string>> const& form_data)
62+
override;
63+
StatusOr<std::unique_ptr<RestResponse>> Put(
64+
RestRequest const& request,
65+
std::vector<absl::Span<char const>> const& payload) override;
66+
67+
private:
68+
friend std::unique_ptr<RestClient> GetDefaultRestClient(
69+
std::string endpoint_address, Options options);
70+
71+
friend class std::unique_ptr<RestClient> GetPooledRestClient(
72+
std::string endpoint_address, Options options);
73+
74+
CurlRestClient(std::string endpoint_address,
75+
std::shared_ptr<CurlHandleFactory> factory, Options options);
76+
StatusOr<std::unique_ptr<CurlImpl>> CreateCurlImpl(
77+
RestRequest const& request);
78+
79+
std::string endpoint_address_;
80+
std::shared_ptr<CurlHandleFactory> handle_factory_;
81+
std::string x_goog_api_client_header_;
82+
Options options_;
83+
};
84+
85+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
86+
} // namespace rest_internal
87+
} // namespace cloud
88+
} // namespace google
89+
90+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_REST_CLIENT_H

0 commit comments

Comments
 (0)