Refactor RpcClient interfaces.

PiperOrigin-RevId: 600912194
This commit is contained in:
Francis Tsui
2024-01-23 14:44:37 -08:00
committed by Copybara-Service
parent 5fb7f69830
commit e2aa37ab04
12 changed files with 155 additions and 30 deletions
+3
View File
@@ -26,6 +26,8 @@ cc_library(
"private_certificate_data.h",
"public_certificate_database.h",
"sharing_platform.h",
"sharing_rpc_client.h",
"sharing_rpc_notifier.h",
"shell.h",
"system_info.h",
"wifi_adapter.h",
@@ -42,6 +44,7 @@ cc_library(
"//sharing/proto:share_cc_proto",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
+72
View File
@@ -0,0 +1,72 @@
// Copyright 2023 Google LLC
//
// 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
//
// https://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 THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_
#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_
#include <memory>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "sharing/internal/api/sharing_rpc_notifier.h"
#include "sharing/proto/certificate_rpc.pb.h"
#include "sharing/proto/contact_rpc.pb.h"
#include "sharing/proto/device_rpc.pb.h"
namespace nearby::sharing::api {
// SharingRpcClient is used to access Nearby Share backend APIs.
class SharingRpcClient {
public:
SharingRpcClient() = default;
virtual ~SharingRpcClient() = default;
// Updates device data.
virtual void UpdateDevice(
const proto::UpdateDeviceRequest& request,
absl::AnyInvocable<
void(const absl::StatusOr<proto::UpdateDeviceResponse>& response) &&>
callback) = 0;
// NearbyShareService v1: ListContactPeople
virtual void ListContactPeople(
const proto::ListContactPeopleRequest& request,
absl::AnyInvocable<void(const absl::StatusOr<
proto::ListContactPeopleResponse>& response) &&>
callback) = 0;
// NearbyShareService v1: ListPublicCertificates
virtual void ListPublicCertificates(
const proto::ListPublicCertificatesRequest& request,
absl::AnyInvocable<
void(const absl::StatusOr<proto::ListPublicCertificatesResponse>&
response) &&>
callback) = 0;
};
// Interface for creating SharingRpcClient instances. Because each
// SharingRpcClient instance can only be used for one API call, a factory
// makes it easier to make multiple requests in sequence or in parallel.
class SharingRpcClientFactory {
public:
SharingRpcClientFactory() = default;
virtual ~SharingRpcClientFactory() = default;
virtual std::unique_ptr<SharingRpcClient> CreateInstance() = 0;
virtual api::SharingRpcNotifier* GetRpcNotifier() const = 0;
};
} // namespace nearby::sharing::api
#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_
@@ -0,0 +1,60 @@
// Copyright 2021 Google LLC
//
// 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
//
// https://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 THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_
#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_
#include "sharing/proto/certificate_rpc.pb.h"
#include "sharing/proto/contact_rpc.pb.h"
#include "sharing/proto/device_rpc.pb.h"
namespace nearby::sharing::api {
// Interface for passing RPC Responses/Requests to observers, by passing
// instances of this class to each RPC Client.
class SharingRpcNotifier {
public:
class Observer {
public:
virtual ~Observer() = default;
// Called when HTTP RPC is made for request and responses.
virtual void OnUpdateDeviceRequest(
const nearby::sharing::proto::UpdateDeviceRequest& request) = 0;
virtual void OnUpdateDeviceResponse(
const nearby::sharing::proto::UpdateDeviceResponse& response) = 0;
virtual void OnListContactPeopleRequest(
const nearby::sharing::proto::ListContactPeopleRequest& request) = 0;
virtual void OnListContactPeopleResponse(
const nearby::sharing::proto::ListContactPeopleResponse& response) = 0;
virtual void OnListPublicCertificatesRequest(
const nearby::sharing::proto::ListPublicCertificatesRequest&
request) = 0;
virtual void OnListPublicCertificatesResponse(
const nearby::sharing::proto::ListPublicCertificatesResponse&
response) = 0;
};
virtual ~SharingRpcNotifier() = default;
virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;
protected:
SharingRpcNotifier() = default;
};
} // namespace nearby::sharing::api
#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_