Skip to content

Commit

Permalink
[guides on map] no need to send all catalog headers into guides on ma…
Browse files Browse the repository at this point in the history
…p requests.
  • Loading branch information
Arsentiy Milchakov authored and darina committed May 19, 2020
1 parent ef87e02 commit 43564cb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
8 changes: 8 additions & 0 deletions map/catalog_headers_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ platform::HttpClient::Headers CatalogHeadersProvider::GetHeaders()

return web_api::GetCatalogHeaders(params);
}

std::optional<platform::HttpClient::Header> CatalogHeadersProvider::GetPositionHeader()
{
if (!m_positionProvider.GetCurrentPosition())
return {};

return web_api::GetPositionHeader(*m_positionProvider.GetCurrentPosition());
}
3 changes: 3 additions & 0 deletions map/catalog_headers_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

#include "platform/http_client.hpp"

#include <optional>

class CatalogHeadersProvider
{
public:
CatalogHeadersProvider(PositionProvider const & positionProvider,
storage::Storage const & storage);

platform::HttpClient::Headers GetHeaders();
std::optional<platform::HttpClient::Header> GetPositionHeader();

private:
PositionProvider const & m_positionProvider;
Expand Down
11 changes: 10 additions & 1 deletion map/guides_on_map_delegate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "map/guides_on_map_delegate.hpp"

#include "web_api/request_headers.hpp"

GuidesOnMapDelegate::GuidesOnMapDelegate(
std::shared_ptr<CatalogHeadersProvider> const & headersProvider)
: m_headersProvider(headersProvider)
Expand All @@ -11,5 +13,12 @@ platform::HttpClient::Headers GuidesOnMapDelegate::GetHeaders()
if (!m_headersProvider)
return {};

return m_headersProvider->GetHeaders();
auto const position = m_headersProvider->GetPositionHeader();
if (!position)
return {};

auto headers = web_api::GetDefaultCatalogHeaders();
headers.emplace(position->m_name, position->m_value);

return headers;
}
6 changes: 6 additions & 0 deletions platform/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class HttpClient
public:
static auto constexpr kNoError = -1;

struct Header
{
std::string m_name;
std::string m_value;
};

using Headers = std::unordered_map<std::string, std::string>;

HttpClient() = default;
Expand Down
15 changes: 11 additions & 4 deletions web_api/request_headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ platform::HttpClient::Headers GetDefaultAuthHeaders()
return result;
}

platform::HttpClient::Header GetPositionHeader(m2::PointD const & pos)
{
std::ostringstream latLonStream;
auto const latLon = mercator::ToLatLon(pos);
latLonStream << std::fixed << std::setprecision(3) << latLon.m_lat << "," << latLon.m_lon;

return {kLatLonHeader, latLonStream.str()};
}

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);
latLonStream << std::fixed << std::setprecision(3) << latLon.m_lat << "," << latLon.m_lon;
result.emplace(kLatLonHeader, latLonStream.str());
auto const latLon = GetPositionHeader(*params.m_currentPosition);
result.emplace(latLon.m_name, latLon.m_value);
}

if (!params.m_cityGeoIds.empty())
Expand Down
1 change: 1 addition & 0 deletions web_api/request_headers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ class HeadersParams

platform::HttpClient::Headers GetDefaultCatalogHeaders();
platform::HttpClient::Headers GetDefaultAuthHeaders();
platform::HttpClient::Header GetPositionHeader(m2::PointD const & pos);
platform::HttpClient::Headers GetCatalogHeaders(HeadersParams const & params);
} // namespace web_api

0 comments on commit 43564cb

Please sign in to comment.