[core] bookmarks catalog headers are provided into corresponding requests

This commit is contained in:
Arsentiy Milchakov 2019-11-14 20:25:35 +03:00 committed by Maksim Andrianov
parent c124c9032e
commit 29261796d4
19 changed files with 188 additions and 28 deletions

View file

@ -32,6 +32,8 @@ set(
bookmark_manager.hpp
bookmarks_search_params.hpp
caching_address_getter.hpp
catalog_headers_provider.cpp
catalog_headers_provider.hpp
chart_generator.cpp
chart_generator.hpp
cloud.cpp
@ -95,6 +97,7 @@ set(
onboarding.hpp
place_page_info.cpp
place_page_info.hpp
position_provider.hpp
power_management/power_manager.cpp
power_management/power_manager.hpp
power_management/power_management_schemas.cpp

View file

@ -5,7 +5,6 @@
#include "web_api/request_headers.hpp"
#include "web_api/utils.hpp"
#include "platform/http_client.hpp"
#include "platform/http_uploader.hpp"
#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
@ -729,3 +728,13 @@ void BookmarkCatalog::SetInvalidTokenHandler(InvalidTokenHandler && onInvalidTok
{
m_onInvalidToken = std::move(onInvalidToken);
}
void BookmarkCatalog::SetHeadersProvider(HeadersProvider const & provider)
{
m_headersProvider = provider;
}
platform::HttpClient::Headers BookmarkCatalog::GetHeaders() const
{
return m_headersProvider();
}

View file

@ -2,8 +2,11 @@
#include "partners_api/utm.hpp"
#include "map/catalog_headers_provider.hpp"
#include "kml/types.hpp"
#include "platform/http_client.hpp"
#include "platform/remote_file.hpp"
#include "platform/safe_callback.hpp"
@ -53,6 +56,8 @@ public:
using CustomPropertiesCallback = platform::SafeCallback<void(bool success, CustomProperties const &)>;
using InvalidTokenHandler = std::function<void()>;
using HeadersProvider = std::function<platform::HttpClient::Headers()>;
void RegisterByServerId(std::string const & id);
void UnregisterByServerId(std::string const & id);
@ -119,16 +124,20 @@ public:
using PingCallback = platform::SafeCallback<void(bool isSuccessful)>;
void Ping(PingCallback && callback) const;
// Handler can be called from non-UI thread.
void SetInvalidTokenHandler(InvalidTokenHandler && onInvalidToken);
using BookmarksToDeleteCallback = platform::SafeCallback<void(std::vector<std::string> const & serverIds)>;
void RequestBookmarksToDelete(std::string const & accessToken, std::string const & userId,
std::vector<std::string> const & serverIds,
BookmarksToDeleteCallback && callback) const;
// Handler can be called from non-UI thread.
void SetInvalidTokenHandler(InvalidTokenHandler && onInvalidToken);
void SetHeadersProvider(HeadersProvider const & provider);
platform::HttpClient::Headers GetHeaders() const;
private:
std::set<std::string> m_downloadingIds;
std::set<std::string> m_registeredInCatalog;
InvalidTokenHandler m_onInvalidToken;
HeadersProvider m_headersProvider;
};

View file

@ -551,6 +551,8 @@ BookmarkManager::BookmarkManager(User & user, Callbacks && callbacks)
m_bookmarkCloud.SetInvalidTokenHandler([this] { m_user.ResetAccessToken(); });
m_bookmarkCatalog.SetInvalidTokenHandler([this] { m_user.ResetAccessToken(); });
m_bookmarkCatalog.SetHeadersProvider(m_callbacks.m_catalogHeadersProvider);
}
BookmarkManager::EditSession BookmarkManager::GetEditSession()

View file

@ -82,14 +82,16 @@ public:
using DetachedBookmarksCallback = std::function<void(std::vector<BookmarkGroupInfo> const &)>;
template <typename StringsBundleProvider, typename SearchAPIProvider,
typename CreateListener, typename UpdateListener,
typename CatalogHeadersProvider, typename CreateListener, typename UpdateListener,
typename DeleteListener, typename AttachListener, typename DetachListener>
Callbacks(StringsBundleProvider && stringsBundleProvider,
SearchAPIProvider && searchAPIProvider, CreateListener && createListener,
UpdateListener && updateListener, DeleteListener && deleteListener,
AttachListener && attachListener, DetachListener && detachListener)
SearchAPIProvider && searchAPIProvider, CatalogHeadersProvider && catalogHeaders,
CreateListener && createListener, UpdateListener && updateListener,
DeleteListener && deleteListener, AttachListener && attachListener,
DetachListener && detachListener)
: m_getStringsBundle(std::forward<StringsBundleProvider>(stringsBundleProvider))
, m_getSearchAPI(std::forward<SearchAPIProvider>(searchAPIProvider))
, m_catalogHeadersProvider(std::forward<BookmarkCatalog::HeadersProvider>(catalogHeaders))
, m_createdBookmarksCallback(std::forward<CreateListener>(createListener))
, m_updatedBookmarksCallback(std::forward<UpdateListener>(updateListener))
, m_deletedBookmarksCallback(std::forward<DeleteListener>(deleteListener))
@ -99,6 +101,7 @@ public:
GetStringsBundleFn m_getStringsBundle;
GetSeacrhAPIFn m_getSearchAPI;
BookmarkCatalog::HeadersProvider m_catalogHeadersProvider;
CreatedBookmarksCallback m_createdBookmarksCallback;
UpdatedBookmarksCallback m_updatedBookmarksCallback;
DeletedBookmarksCallback m_deletedBookmarksCallback;

View file

@ -0,0 +1,31 @@
#include "map/catalog_headers_provider.hpp"
CatalogHeadersProvider::CatalogHeadersProvider(PositionProvider const & positionProvider,
storage::Storage const & storage)
: m_positionProvider(positionProvider)
, m_storage(storage)
{
}
platform::HttpClient::Headers CatalogHeadersProvider::GetHeaders()
{
web_api::HeadersParams params;
params.m_currentPosition = m_positionProvider.GetCurrentPosition();
storage::CountriesVec localMaps;
m_storage.GetLocalRealMaps(localMaps);
auto const & countryToCity = m_storage.GetMwmTopCityGeoIds();
auto & countries = params.m_countryGeoids;
auto & cities = params.m_cityGeoids;
for (auto const id : localMaps)
{
auto const countryIds = m_storage.GetTopCountryGeoIds(id);
countries.insert(countries.end(), countryIds.cbegin(), countryIds.cend());
auto const cityIt = countryToCity.find(id);
if (cityIt != countryToCity.cend())
cities.push_back(cityIt->second);
}
return web_api::GetCatalogHeaders(params);
}

View file

@ -0,0 +1,22 @@
#pragma once
#include "map/position_provider.hpp"
#include "storage/storage.hpp"
#include "web_api/request_headers.hpp"
#include "platform/http_client.hpp"
class CatalogHeadersProvider
{
public:
CatalogHeadersProvider(PositionProvider const & positionProvider,
storage::Storage const & storage);
platform::HttpClient::Headers GetHeaders();
private:
PositionProvider const & m_positionProvider;
storage::Storage const & m_storage;
};

View file

@ -1,5 +1,6 @@
#include "map/framework.hpp"
#include "map/benchmark_tools.hpp"
#include "map/catalog_headers_provider.hpp"
#include "map/chart_generator.hpp"
#include "map/displayed_categories_modifiers.hpp"
#include "map/everywhere_search_params.hpp"
@ -421,10 +422,13 @@ Framework::Framework(FrameworkParams const & params)
InitSearchAPI();
LOG(LDEBUG, ("Search API initialized"));
auto const catalogHeadersProvider = make_shared<CatalogHeadersProvider>(*this, m_storage);
m_bmManager = make_unique<BookmarkManager>(m_user, BookmarkManager::Callbacks(
[this]() -> StringsBundle const & { return m_stringsBundle; },
[this]() -> SearchAPI & { return GetSearchAPI(); },
[catalogHeadersProvider]() { return catalogHeadersProvider->GetHeaders(); },
[this](vector<BookmarkInfo> const & marks) { GetSearchAPI().OnBookmarksCreated(marks); },
[this](vector<BookmarkInfo> const & marks) { GetSearchAPI().OnBookmarksUpdated(marks); },
[this](vector<kml::MarkId> const & marks) { GetSearchAPI().OnBookmarksDeleted(marks); },
@ -514,7 +518,8 @@ Framework::Framework(FrameworkParams const & params)
GetPowerManager().Subscribe(this);
GetPowerManager().Load();
m_promoApi->SetDelegate(make_unique<PromoDelegate>(m_featuresFetcher.GetDataSource(), *m_cityFinder));
m_promoApi->SetDelegate(make_unique<PromoDelegate>(m_featuresFetcher.GetDataSource(),
*m_cityFinder, catalogHeadersProvider));
eye::Eye::Instance().Subscribe(m_promoApi.get());
// Clean the no longer used key from old devices.

View file

@ -13,6 +13,7 @@
#include "map/mwm_url.hpp"
#include "map/notifications/notification_manager.hpp"
#include "map/place_page_info.hpp"
#include "map/position_provider.hpp"
#include "map/power_management/power_management_schemas.hpp"
#include "map/power_management/power_manager.hpp"
#include "map/purchase.hpp"
@ -151,7 +152,8 @@ struct FrameworkParams
{}
};
class Framework : public SearchAPI::Delegate,
class Framework : public PositionProvider,
public SearchAPI::Delegate,
public RoutingManager::Delegate,
public TipsApi::Delegate,
private power_management::PowerManager::Subscriber
@ -378,7 +380,7 @@ public:
search::Results::ConstIter end, bool clear,
booking::filter::Types types) override;
void ClearViewportSearchResults() override;
// SearchApi::Delegate and TipsApi::Delegate override.
// PositionProvider, SearchApi::Delegate and TipsApi::Delegate override.
boost::optional<m2::PointD> GetCurrentPosition() const override;
bool ParseSearchQueryCommand(search::SearchParams const & params) override;
search::ProductInfo GetProductInfo(search::Result const & result) const override;

View file

@ -143,6 +143,7 @@ BookmarkManager::Callbacks const bmCallbacks(
return dummyBundle;
},
static_cast<BookmarkManager::Callbacks::GetSeacrhAPIFn>(nullptr),
static_cast<BookmarkCatalog::HeadersProvider>(nullptr),
static_cast<BookmarkManager::Callbacks::CreatedBookmarksCallback>(nullptr),
static_cast<BookmarkManager::Callbacks::UpdatedBookmarksCallback>(nullptr),
static_cast<BookmarkManager::Callbacks::DeletedBookmarksCallback>(nullptr),
@ -1221,6 +1222,7 @@ UNIT_CLASS_TEST(Runner, Bookmarks_Listeners)
return dummyBundle;
},
static_cast<BookmarkManager::Callbacks::GetSeacrhAPIFn>(nullptr),
static_cast<BookmarkCatalog::HeadersProvider>(nullptr),
onCreate, onUpdate, onDelete, onAttach, onDetach);
User user;

13
map/position_provider.hpp Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include "geometry/point2d.hpp"
#include <boost/optional.hpp>
class PositionProvider
{
public:
virtual ~PositionProvider() = default;
virtual boost::optional<m2::PointD> GetCurrentPosition() const = 0;
};

View file

@ -7,8 +7,9 @@
#include "base/string_utils.hpp"
PromoDelegate::PromoDelegate(DataSource const & dataSource, search::CityFinder & cityFinder)
: m_dataSource(dataSource), m_cityFinder(cityFinder)
PromoDelegate::PromoDelegate(DataSource const & dataSource, search::CityFinder & cityFinder,
std::shared_ptr<CatalogHeadersProvider> const & headersProvider)
: m_dataSource(dataSource), m_cityFinder(cityFinder), m_headersProvider(headersProvider)
{
}
@ -39,3 +40,11 @@ std::string PromoDelegate::GetCityId(m2::PointD const & point)
return {};
}
platform::HttpClient::Headers PromoDelegate::GetHeaders()
{
if (!m_headersProvider)
return {};
return m_headersProvider->GetHeaders();
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "map/catalog_headers_provider.hpp"
#include "partners_api/promo_api.hpp"
#include "indexer/feature_to_osm.hpp"
@ -19,13 +21,16 @@ class CityFinder;
class PromoDelegate : public promo::Api::Delegate
{
public:
PromoDelegate(DataSource const & dataSource, search::CityFinder & cityFinder);
PromoDelegate(DataSource const & dataSource, search::CityFinder & cityFinder,
std::shared_ptr<CatalogHeadersProvider> const & headersProvider);
std::string GetCityId(m2::PointD const & point) override;
platform::HttpClient::Headers GetHeaders() override;
private:
DataSource const & m_dataSource;
search::CityFinder & m_cityFinder;
std::shared_ptr<CatalogHeadersProvider> m_headersProvider;
// todo(@a, @m) Drop the unique_ptr and add an IsLoaded() method?
std::unique_ptr<indexer::FeatureIdToGeoObjectIdOneWay> m_cities;
};

View file

@ -8,7 +8,6 @@
#include "platform/platform_tests_support/async_gui_thread.hpp"
#include <algorithm>
#include <memory>
using namespace eye;
@ -38,6 +37,7 @@ class DelegateForTesting : public promo::Api::Delegate
{
public:
std::string GetCityId(m2::PointD const &) override { return kTestId; }
platform::HttpClient::Headers GetHeaders() override { return {}; }
};
} // namespace

View file

@ -1,7 +1,5 @@
#include "partners_api/promo_api.hpp"
#include "web_api/request_headers.hpp"
#include "platform/http_client.hpp"
#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
@ -165,9 +163,8 @@ std::string GetCityCatalogueUrl(std::string const & baseUrl, std::string const &
return baseUrl + "v2/mobilefront/city/" + ToSignedId(id);
}
void GetPromoGalleryImpl(std::string const & url, UTM utm,
CityGalleryCallback const & onSuccess, OnError const & onError)
void GetPromoGalleryImpl(std::string const & url, platform::HttpClient::Headers const & headers,
UTM utm, CityGalleryCallback const & onSuccess, OnError const & onError)
{
if (url.empty())
{
@ -175,13 +172,13 @@ void GetPromoGalleryImpl(std::string const & url, UTM utm,
return;
}
GetPlatform().RunTask(Platform::Thread::Network, [url, utm, onSuccess, onError]()
GetPlatform().RunTask(Platform::Thread::Network, [url, headers, utm, onSuccess, onError]()
{
CityGallery result;
std::string httpResult;
platform::HttpClient request(url);
request.SetTimeout(5 /* timeoutSec */);
request.SetRawHeaders(web_api::GetDefaultCatalogHeaders());
request.SetRawHeaders(headers);
if (!request.RunHttpRequest(httpResult))
{
onError();
@ -270,7 +267,8 @@ void Api::GetCityGallery(m2::PointD const & point, std::string const & lang, UTM
{
CHECK(m_delegate, ());
auto const url = MakeCityGalleryUrl(m_baseUrl, m_delegate->GetCityId(point), lang);
GetPromoGalleryImpl(url, utm, onSuccess, onError);
auto const headers = m_delegate->GetHeaders();
GetPromoGalleryImpl(url, headers, utm, onSuccess, onError);
}
void Api::GetPoiGallery(m2::PointD const & point, std::string const & lang, Tags const & tags,
@ -278,9 +276,11 @@ void Api::GetPoiGallery(m2::PointD const & point, std::string const & lang, Tags
OnError const & onError) const
{
CHECK(m_delegate, ());
auto const url =
MakePoiGalleryUrl(m_baseUrl, m_delegate->GetCityId(point), point, lang, tags, useCoordinates);
GetPromoGalleryImpl(url, utm, onSuccess, onError);
auto const headers = m_delegate->GetHeaders();
GetPromoGalleryImpl(url, headers, utm, onSuccess, onError);
}
void Api::OnTransitionToBooking(m2::PointD const & hotelPos)

View file

@ -4,6 +4,7 @@
#include "metrics/eye.hpp"
#include "platform/http_client.hpp"
#include "platform/safe_callback.hpp"
#include "geometry/point2d.hpp"
@ -85,6 +86,7 @@ public:
virtual ~Delegate() = default;
virtual std::string GetCityId(m2::PointD const & point) = 0;
virtual platform::HttpClient::Headers GetHeaders() = 0;
};
Api(std::string const & baseUrl = BOOKMARKS_CATALOG_FRONT_URL,

View file

@ -53,8 +53,6 @@ public:
// Check by default: ErrorCode() == 200
bool RunHttpRequest(std::string & response, SuccessChecker checker = nullptr);
// Shared methods for all platforms, implemented at http_client.cpp
HttpClient & SetDebugMode(bool debug_mode);
HttpClient & SetUrlRequested(std::string const & url);
HttpClient & SetHttpMethod(std::string const & method);
// This method is mutually exclusive with set_body_data().

View file

@ -4,14 +4,45 @@
#include "platform/platform.hpp"
#include "geometry/mercator.hpp"
#include "base/string_utils.hpp"
namespace web_api
{
namespace
{
char const * const kDeviceIdHeader = "X-Mapsme-Device-Id";
char const * const kUserAgentHeader = "User-Agent";
char const * const kCitiesHeader = "X-Mapsme-City-Ids";
char const * const kCountriesHeader = "X-Mapsme-Country-Ids";
char const * const kLatLonHeader = "X-Mapsme-Lat-Lon";
} // namespace
platform::HttpClient::Headers GetDefaultCatalogHeaders()
{
return {{kUserAgentHeader, GetPlatform().GetAppUserAgent()},
{kDeviceIdHeader, DeviceId()}};
}
platform::HttpClient::Headers GetCatalogHeaders(HeadersParams const & params)
{
platform::HttpClient::Headers result = GetDefaultCatalogHeaders();
if (params.m_currentPosition)
{
std::ostringstream latLonStream;
auto const latLon = mercator::ToLatLon(params.m_currentPosition.get());
latLonStream << std::fixed << std::setprecision(3) << latLon.m_lat << "," << latLon.m_lon;
result.emplace(kLatLonHeader, latLonStream.str());
}
if (!params.m_cityGeoids.empty())
result.emplace(kCitiesHeader, strings::JoinAny(params.m_cityGeoids));
if (!params.m_countryGeoids.empty())
result.emplace(kCountriesHeader, strings::JoinAny(params.m_countryGeoids));
return result;
}
} // namespace web_api

View file

@ -2,10 +2,24 @@
#include "platform/http_client.hpp"
#include "geometry/point2d.hpp"
#include "base/geo_object_id.hpp"
#include <vector>
#include <boost/optional.hpp>
namespace web_api
{
extern char const * const kDeviceIdHeader;
extern char const * const kUserAgentHeader;
class HeadersParams
{
public:
boost::optional<m2::PointD> m_currentPosition;
std::vector<base::GeoObjectId> m_countryGeoids;
std::vector<base::GeoObjectId> m_cityGeoids;
};
platform::HttpClient::Headers GetDefaultCatalogHeaders();
platform::HttpClient::Headers GetCatalogHeaders(HeadersParams const & params);
} // namespace web_api