forked from organicmaps/organicmaps
Merge pull request #6359 from milchakov/viator_fix
[partners_api] viator error handling fix
This commit is contained in:
commit
91b89fb62f
2 changed files with 33 additions and 24 deletions
|
@ -28,17 +28,22 @@ UNIT_TEST(Viator_GetTop5Products)
|
|||
{
|
||||
viator::Api api;
|
||||
std::string const kSofia = "5630";
|
||||
std::string resultId;
|
||||
std::vector<viator::Product> resultProducts;
|
||||
|
||||
api.GetTop5Products(
|
||||
kSofia, "",
|
||||
[kSofia](std::string const & destId, std::vector<viator::Product> const & products) {
|
||||
TEST_EQUAL(destId, kSofia, ());
|
||||
TEST(!products.empty(), ());
|
||||
api.GetTop5Products(kSofia, "",
|
||||
[&resultId, &resultProducts](std::string const & destId,
|
||||
std::vector<viator::Product> const & products) {
|
||||
resultId = destId;
|
||||
resultProducts = products;
|
||||
|
||||
testing::StopEventLoop();
|
||||
});
|
||||
testing::StopEventLoop();
|
||||
});
|
||||
|
||||
testing::RunEventLoop();
|
||||
|
||||
TEST_EQUAL(resultId, kSofia, ());
|
||||
TEST(!resultProducts.empty(), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(Viator_SortProducts)
|
||||
|
|
|
@ -115,22 +115,7 @@ std::string MakeUrl(std::string const & apiMethod)
|
|||
return os.str();
|
||||
}
|
||||
|
||||
bool CheckAnswer(my::Json const & root)
|
||||
{
|
||||
bool success;
|
||||
FromJSONObjectOptionalField(root.get(), "success", success, false);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
std::string errorMessage;
|
||||
FromJSONObject(root.get(), "errorMessageText", errorMessage);
|
||||
LOG(LWARNING, ("Viator retrieved unsuccessfull status, error message:", errorMessage));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool CheckDataArray(json_t const * data)
|
||||
bool CheckJsonArray(json_t const * data)
|
||||
{
|
||||
if (data == nullptr)
|
||||
return false;
|
||||
|
@ -144,13 +129,32 @@ bool CheckDataArray(json_t const * data)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool CheckAnswer(my::Json const & root)
|
||||
{
|
||||
bool success;
|
||||
FromJSONObjectOptionalField(root.get(), "success", success, false);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
std::string errorMessage = "Unknown error.";
|
||||
auto const errorMessageArray = json_object_get(root.get(), "errorMessageText");
|
||||
|
||||
if (CheckJsonArray(errorMessageArray))
|
||||
FromJSON(json_array_get(errorMessageArray, 0), errorMessage);
|
||||
|
||||
LOG(LWARNING, ("Viator retrieved unsuccessfull status, error message:", errorMessage));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void MakeProducts(std::string const & src, std::vector<Product> & products)
|
||||
{
|
||||
products.clear();
|
||||
|
||||
my::Json root(src.c_str());
|
||||
auto const data = json_object_get(root.get(), "data");
|
||||
if (!CheckAnswer(root) || !CheckDataArray(data))
|
||||
if (!CheckAnswer(root) || !CheckJsonArray(data))
|
||||
return;
|
||||
|
||||
auto const dataSize = json_array_size(data);
|
||||
|
|
Loading…
Add table
Reference in a new issue