Adding factory for creating FastPairMetadataRepository instances to increase the flexibility. The unit test for the FastPairMetadataRepositoryFactory will be involved when it is called.

PiperOrigin-RevId: 501682263
This commit is contained in:
hai007
2023-01-12 15:28:42 -08:00
committed by Copybara-Service
parent 7eeea7a283
commit 454d22b19a
3 changed files with 49 additions and 0 deletions
@@ -16,6 +16,7 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_H_
#include <functional>
#include <memory>
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
@@ -43,6 +44,16 @@ class FastPairMetadataRepository {
ErrorCallback error_callback) = 0;
};
// Interface for creating FastPairMetadataRepository instances to increase the
// flexibility
class FastPairMetadataRepositoryFactory {
public:
FastPairMetadataRepositoryFactory() = default;
virtual ~FastPairMetadataRepositoryFactory() = default;
virtual std::unique_ptr<FastPairMetadataRepository> CreateInstance() = 0;
};
} // namespace fastpair
} // namespace nearby
@@ -23,11 +23,15 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/internal/api/device_info.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/repository/fast_pair_metadata_fetcher_impl.h"
#include "internal/network/http_client_factory.h"
#include "internal/network/url.h"
#include "internal/platform/logging.h"
#include "internal/network/http_client.h"
namespace nearby {
namespace fastpair {
@@ -116,5 +120,20 @@ void FastPairMetadataRepositoryImpl::OnFetcherFailed(FastPairHttpError error) {
error_callback_(error);
}
FastPairMetadataRepositoryFactoryImpl::FastPairMetadataRepositoryFactoryImpl(
network::HttpClientFactory* http_client_factory)
: http_client_factory_(http_client_factory) {}
FastPairMetadataRepositoryFactoryImpl::
~FastPairMetadataRepositoryFactoryImpl() = default;
std::unique_ptr<FastPairMetadataRepository>
FastPairMetadataRepositoryFactoryImpl::CreateInstance() {
return std::make_unique<FastPairMetadataRepositoryImpl>(
std::make_unique<FastPairMetadataFetcherImpl>(
api::DeviceInfo::OsType::kWindows),
http_client_factory_->CreateInstance());
}
} // namespace fastpair
} // namespace nearby
@@ -25,6 +25,7 @@
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "internal/network/http_client.h"
#include "internal/network/http_client_factory.h"
namespace nearby {
namespace fastpair {
@@ -64,6 +65,24 @@ class FastPairMetadataRepositoryImpl : public FastPairMetadataRepository {
ErrorCallback error_callback_;
};
class FastPairMetadataRepositoryFactoryImpl
: public FastPairMetadataRepositoryFactory {
public:
explicit FastPairMetadataRepositoryFactoryImpl(
network::HttpClientFactory* http_client_factory);
~FastPairMetadataRepositoryFactoryImpl() override;
FastPairMetadataRepositoryFactoryImpl(
FastPairMetadataRepositoryFactoryImpl&) = delete;
FastPairMetadataRepositoryFactoryImpl& operator=(
FastPairMetadataRepositoryFactoryImpl&) = delete;
std::unique_ptr<FastPairMetadataRepository> CreateInstance() override;
private:
network::HttpClientFactory* http_client_factory_;
};
} // namespace fastpair
} // namespace nearby