forked from organicmaps/organicmaps
[partners_api] cian error callback
This commit is contained in:
parent
264f5afb68
commit
9e2e8bb618
5 changed files with 103 additions and 40 deletions
|
@ -1,5 +1,4 @@
|
|||
#include "partners_api/cian_api.hpp"
|
||||
#include "partners_api/utils.hpp"
|
||||
|
||||
#include "platform/platform.hpp"
|
||||
|
||||
|
@ -13,6 +12,7 @@
|
|||
|
||||
#include "3party/jansson/myjansson.hpp"
|
||||
|
||||
using namespace partners_api;
|
||||
using namespace platform;
|
||||
|
||||
namespace
|
||||
|
@ -82,14 +82,14 @@ namespace cian
|
|||
std::string const kBaseUrl = "https://api.cian.ru/rent-nearby/v1";
|
||||
|
||||
// static
|
||||
bool RawApi::GetRentNearby(m2::RectD const & rect, std::string & result,
|
||||
std::string const & baseUrl /* = kBaseUrl */)
|
||||
http::Result RawApi::GetRentNearby(m2::RectD const & rect,
|
||||
std::string const & baseUrl /* = kBaseUrl */)
|
||||
{
|
||||
std::ostringstream url;
|
||||
url << baseUrl << "/get-offers-in-bbox/?bbox=" << rect.minX() << ',' << rect.maxY() << '~'
|
||||
<< rect.maxX() << ',' << rect.minY();
|
||||
|
||||
return partners_api_utils::RunSimpleHttpRequest(url.str(), result);
|
||||
return http::RunSimpleRequest(url.str());
|
||||
}
|
||||
|
||||
Api::Api(std::string const & baseUrl /* = kBaseUrl */) : m_baseUrl(baseUrl) {}
|
||||
|
@ -99,27 +99,29 @@ Api::~Api()
|
|||
m_worker.Shutdown(base::WorkerThread::Exit::SkipPending);
|
||||
}
|
||||
|
||||
uint64_t Api::GetRentNearby(ms::LatLon const & latlon, RentNearbyCallback const & cb)
|
||||
uint64_t Api::GetRentNearby(ms::LatLon const & latlon, RentNearbyCallback const & cb,
|
||||
ErrorCallback const & errCb)
|
||||
{
|
||||
auto const reqId = ++m_requestId;
|
||||
auto const baseUrl = m_baseUrl;
|
||||
auto const & baseUrl = m_baseUrl;
|
||||
|
||||
auto const point = MercatorBounds::FromLatLon(latlon);
|
||||
m2::RectD const rect = MercatorBounds::RectByCenterXYAndSizeInMeters(point, kSideLength);
|
||||
|
||||
m_worker.Push([reqId, rect, cb, baseUrl]() {
|
||||
std::string rawResult;
|
||||
m_worker.Push([reqId, rect, cb, errCb, baseUrl]() {
|
||||
std::vector<RentPlace> result;
|
||||
|
||||
if (!RawApi::GetRentNearby(rect, rawResult, baseUrl))
|
||||
auto const rawResult = RawApi::GetRentNearby(rect, baseUrl);
|
||||
if (!rawResult)
|
||||
{
|
||||
GetPlatform().RunOnGuiThread([cb, result, reqId]() { cb(result, reqId); });
|
||||
auto & code = rawResult.m_errorCode;
|
||||
GetPlatform().RunOnGuiThread([errCb, code, reqId]() { errCb(code, reqId); });
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
MakeResult(rawResult, result);
|
||||
MakeResult(rawResult.m_data, result);
|
||||
}
|
||||
catch (my::Json::Exception const & e)
|
||||
{
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "partners_api/utils.hpp"
|
||||
|
||||
#include "geometry/latlon.hpp"
|
||||
#include "geometry/rect2d.hpp"
|
||||
|
||||
|
@ -17,8 +19,8 @@ extern std::string const kBaseUrl;
|
|||
class RawApi
|
||||
{
|
||||
public:
|
||||
static bool GetRentNearby(m2::RectD const & rect, std::string & result,
|
||||
std::string const & url = kBaseUrl);
|
||||
static partners_api::http::Result GetRentNearby(m2::RectD const & rect,
|
||||
std::string const & url = kBaseUrl);
|
||||
};
|
||||
|
||||
struct RentOffer
|
||||
|
@ -44,10 +46,13 @@ public:
|
|||
using RentNearbyCallback =
|
||||
std::function<void(std::vector<RentPlace> const & places, uint64_t const requestId)>;
|
||||
|
||||
using ErrorCallback = std::function<void(int httpCode, uint64_t const requestId)>;
|
||||
|
||||
explicit Api(std::string const & baseUrl = kBaseUrl);
|
||||
virtual ~Api();
|
||||
|
||||
uint64_t GetRentNearby(ms::LatLon const & latlon, RentNearbyCallback const & cb);
|
||||
uint64_t GetRentNearby(ms::LatLon const & latlon, RentNearbyCallback const & cb,
|
||||
ErrorCallback const & errCb);
|
||||
|
||||
static bool IsCitySupported(std::string const & city);
|
||||
|
||||
|
|
|
@ -8,31 +8,59 @@ namespace
|
|||
{
|
||||
UNIT_TEST(Cian_GetRentNearbyRaw)
|
||||
{
|
||||
string result;
|
||||
cian::RawApi::GetRentNearby({37.402891, 55.656318, 37.840971, 55.859980}, result);
|
||||
auto const result = cian::RawApi::GetRentNearby({37.402891, 55.656318, 37.840971, 55.859980});
|
||||
|
||||
TEST(!result.empty(), ());
|
||||
TEST(!result.m_data.empty(), ());
|
||||
|
||||
my::Json root(result.c_str());
|
||||
my::Json root(result.m_data.c_str());
|
||||
TEST(json_is_object(root.get()), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(Cian_GetRentNearby)
|
||||
{
|
||||
cian::Api api("http://localhost:34568/partners");
|
||||
ms::LatLon latlon(37.501365, 55.805666);
|
||||
|
||||
std::vector<cian::RentPlace> result;
|
||||
uint64_t reqId = 0;
|
||||
reqId = api.GetRentNearby(latlon, [&reqId, &result](std::vector<cian::RentPlace> const & places,
|
||||
uint64_t const requestId) {
|
||||
TEST_EQUAL(reqId, requestId, ());
|
||||
result = places;
|
||||
testing::StopEventLoop();
|
||||
});
|
||||
|
||||
testing::RunEventLoop();
|
||||
{
|
||||
cian::Api api("http://localhost:34568/partners");
|
||||
std::vector<cian::RentPlace> result;
|
||||
|
||||
TEST(!result.empty(), ());
|
||||
reqId = api.GetRentNearby(
|
||||
latlon,
|
||||
[&reqId, &result](std::vector<cian::RentPlace> const & places, uint64_t const requestId) {
|
||||
TEST_EQUAL(reqId, requestId, ());
|
||||
result = places;
|
||||
testing::StopEventLoop();
|
||||
},
|
||||
[&reqId](int httpCode, uint64_t const requestId) {
|
||||
TEST_EQUAL(reqId, requestId, ());
|
||||
TEST(false, (httpCode));
|
||||
});
|
||||
|
||||
testing::RunEventLoop();
|
||||
|
||||
TEST(!result.empty(), ());
|
||||
}
|
||||
{
|
||||
cian::Api api("incorrect url");
|
||||
std::vector<cian::RentPlace> result;
|
||||
int httpCode = -1;
|
||||
|
||||
reqId = api.GetRentNearby(
|
||||
latlon,
|
||||
[&reqId, &result](std::vector<cian::RentPlace> const & places, uint64_t const requestId) {
|
||||
TEST_EQUAL(reqId, requestId, ());
|
||||
TEST(false, (requestId));
|
||||
},
|
||||
[&reqId, &httpCode](int code, uint64_t const requestId) {
|
||||
TEST_EQUAL(reqId, requestId, ());
|
||||
httpCode = code;
|
||||
testing::StopEventLoop();
|
||||
});
|
||||
|
||||
testing::RunEventLoop();
|
||||
|
||||
TEST_NOT_EQUAL(httpCode, -1, ());
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -17,6 +17,17 @@ using namespace platform;
|
|||
|
||||
namespace
|
||||
{
|
||||
bool RunSimpleHttpRequest(std::string const & url, std::string & result)
|
||||
{
|
||||
platform::HttpClient request(url);
|
||||
if (request.RunHttpRequest() && !request.WasRedirected() && request.ErrorCode() == 200)
|
||||
{
|
||||
result = request.ServerResponse();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckUberResponse(json_t const * answer)
|
||||
{
|
||||
if (answer == nullptr)
|
||||
|
@ -117,7 +128,7 @@ bool RawApi::GetProducts(ms::LatLon const & pos, string & result,
|
|||
url << fixed << setprecision(6) << baseUrl << "?server_token=" << UBER_SERVER_TOKEN
|
||||
<< "&latitude=" << pos.lat << "&longitude=" << pos.lon;
|
||||
|
||||
return partners_api_utils::RunSimpleHttpRequest(url.str(), result);
|
||||
return RunSimpleHttpRequest(url.str(), result);
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -128,7 +139,7 @@ bool RawApi::GetEstimatedTime(ms::LatLon const & pos, string & result,
|
|||
url << fixed << setprecision(6) << baseUrl << "/time?server_token=" << UBER_SERVER_TOKEN
|
||||
<< "&start_latitude=" << pos.lat << "&start_longitude=" << pos.lon;
|
||||
|
||||
return partners_api_utils::RunSimpleHttpRequest(url.str(), result);
|
||||
return RunSimpleHttpRequest(url.str(), result);
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -140,7 +151,7 @@ bool RawApi::GetEstimatedPrice(ms::LatLon const & from, ms::LatLon const & to, s
|
|||
<< "&start_latitude=" << from.lat << "&start_longitude=" << from.lon
|
||||
<< "&end_latitude=" << to.lat << "&end_longitude=" << to.lon;
|
||||
|
||||
return partners_api_utils::RunSimpleHttpRequest(url.str(), result);
|
||||
return RunSimpleHttpRequest(url.str(), result);
|
||||
}
|
||||
|
||||
void ProductMaker::Reset(uint64_t const requestId)
|
||||
|
|
|
@ -4,16 +4,33 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
namespace partners_api_utils
|
||||
namespace partners_api
|
||||
{
|
||||
inline bool RunSimpleHttpRequest(std::string const & url, std::string & result)
|
||||
namespace http
|
||||
{
|
||||
struct Result
|
||||
{
|
||||
Result(bool result, int errorCode, std::string const & data)
|
||||
: m_result(result), m_errorCode(errorCode), m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
operator bool() const { return m_result && m_errorCode == 200; }
|
||||
|
||||
bool m_result = false;
|
||||
int m_errorCode = platform::HttpClient::kNoError;
|
||||
std::string m_data;
|
||||
};
|
||||
|
||||
inline Result RunSimpleRequest(std::string const & url)
|
||||
{
|
||||
platform::HttpClient request(url);
|
||||
bool result = false;
|
||||
|
||||
if (request.RunHttpRequest() && !request.WasRedirected() && request.ErrorCode() == 200)
|
||||
{
|
||||
result = request.ServerResponse();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
result = true;
|
||||
|
||||
return {result, request.ErrorCode(), request.ServerResponse()};
|
||||
}
|
||||
} // namespace partners_api_utils
|
||||
} // namespace http
|
||||
} // namespace partners_api
|
||||
|
|
Loading…
Add table
Reference in a new issue