Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rutaxi_api.cpp « partners_api - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc617c73490043d912c7e13e35f7b6108a463523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "partners_api/rutaxi_api.hpp"

#include "platform/http_client.hpp"
#include "platform/platform.hpp"

#include "geometry/latlon.hpp"
#include "geometry/mercator.hpp"

#include "coding/url_encode.hpp"

#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include "std/target_os.hpp"

#include <iomanip>
#include <memory>
#include <sstream>
#include <utility>

#include "3party/jansson/myjansson.hpp"

#include "private.h"

namespace
{
std::string const kMappingFilepath = "taxi_places/osm_to_rutaxi.json";
std::string const kArrivalTimeSeconds = "300";

bool RunSimpleHttpRequest(std::string const & url, std::string const & data, std::string & result)
{
  platform::HttpClient request(url);

  request.SetTimeout(10.0);

  if (!data.empty())
    request.SetBodyData(data, "application/json");

  request.SetRawHeader("Accept", "application/json");
  request.SetRawHeader("X-Parse-Application-Id", std::string("App ") + RUTAXI_APP_TOKEN);

  return request.RunHttpRequest(result);
}
}  // namespace

namespace taxi
{
namespace rutaxi
{
std::string const kTaxiInfoUrl = "https://api.rutaxi.ru/api/1.0.0/";

// static
bool RawApi::GetNearObject(ms::LatLon const & pos, std::string const & city, std::string & result,
                           std::string const & baseUrl /* = kTaxiInfoUrl */)
{
  std::ostringstream data;
  data << R"({"latitude": )" << pos.lat << R"(, "longitude": )" << pos.lon << R"(, "city": ")"
       << city << R"("})";

  return RunSimpleHttpRequest(baseUrl + "near/", data.str(), result);
}

// static
bool RawApi::GetCost(Object const & from, Object const & to, std::string const & city,
                     std::string & result, std::string const & baseUrl /* = kTaxiInfoUrl */)
{
  std::ostringstream data;
  data << R"({"city": ")" << city << R"(", "Order": {"points": [{"object_id": )" << from.m_id
       << R"(, "house": ")" << from.m_house << R"("}, {"object_id": )" << to.m_id
       << R"(, "house": ")" << to.m_house << R"("}]}})";

  return RunSimpleHttpRequest(baseUrl + "cost/", data.str(), result);
}

Api::Api(std::string const & baseUrl /* = kTaxiInfoUrl */)
  : ApiBase(baseUrl)
  , m_cityMapping(LoadCityMapping())
{
}

void Api::SetDelegate(Delegate * delegate)
{
  m_delegate = delegate;
}

void Api::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to,
                               ProductsCallback const & successFn,
                               ErrorProviderCallback const & errorFn)
{
  ASSERT(successFn, ());
  ASSERT(errorFn, ());
  ASSERT(m_delegate, ());

  auto const fromCity = m_delegate->GetCityName(MercatorBounds::FromLatLon(from));
  auto const toCity = m_delegate->GetCityName(MercatorBounds::FromLatLon(to));
  auto const cityIdIt = m_cityMapping.find(toCity);

  // TODO(a): Add ErrorCode::FarDistance and provide this error code.
  if (fromCity != toCity || cityIdIt == m_cityMapping.cend() || !IsDistanceSupported(from, to))
  {
    errorFn(ErrorCode::NoProducts);
    return;
  }

  auto const baseUrl = m_baseUrl;
  auto const & city = cityIdIt->second;

  GetPlatform().RunTask(Platform::Thread::Network, [from, to, city, baseUrl, successFn, errorFn]()
  {
    auto const getNearObject = [&city, &baseUrl, &errorFn](ms::LatLon const & pos, Object & dst)
    {
      std::string httpResult;
      if (!RawApi::GetNearObject(pos, city.m_id, httpResult, baseUrl))
      {
        errorFn(ErrorCode::RemoteError);
        return false;
      }

      try
      {
        MakeNearObject(httpResult, dst);
      }
      catch (my::Json::Exception const & e)
      {
        errorFn(ErrorCode::NoProducts);
        LOG(LERROR, (e.what(), httpResult));
        return false;
      }

      return true;
    };

    Object fromObj;
    Object toObj;

    if (!getNearObject(from, fromObj) || !getNearObject(to, toObj))
      return;

    std::string result;
    if (!RawApi::GetCost(fromObj, toObj, city.m_id, result, baseUrl))
    {
      errorFn(ErrorCode::RemoteError);
      return;
    }

    std::vector<Product> products;
    try
    {
      MakeProducts(result, fromObj, toObj, city, products);
    }
    catch (my::Json::Exception const & e)
    {
      LOG(LERROR, (e.what(), result));
      products.clear();
    }

    if (products.empty())
      errorFn(ErrorCode::NoProducts);
    else
      successFn(products);
  });
}

/// Returns link which allows you to launch the RuTaxi app.
RideRequestLinks Api::GetRideRequestLinks(std::string const & productId, ms::LatLon const & from,
                                          ms::LatLon const & to) const
{
  return {"rto://order.rutaxi.ru/a.php?" + productId, "https://go.onelink.me/2944814706/mapsme1"};
}

void MakeNearObject(std::string const & src, Object & dst)
{
  my::Json root(src.c_str());

  auto const data = json_object_get(root.get(), "data");
  auto const objects = json_object_get(data, "objects");
  auto const item = json_array_get(objects, 0);

  FromJSONObject(item, "id", dst.m_id);
  FromJSONObject(item, "house", dst.m_house);
  FromJSONObject(item, "name", dst.m_title);
}

void MakeProducts(std::string const & src, Object const & from, Object const & to,
                  City const & city, std::vector<taxi::Product> & products)
{
  products.clear();

  my::Json root(src.c_str());

  std::ostringstream productStream;
  productStream << "city=" << city.m_id << "&title1=" << UrlEncode(from.m_title)
                << "&ob1=" << from.m_id << "&h1=" << UrlEncode(from.m_house)
                << "&title2=" << UrlEncode(to.m_title) << "&ob2=" << to.m_id
                << "&h2=" << UrlEncode(to.m_house);

  taxi::Product product;
  product.m_productId = productStream.str();
  product.m_currency = city.m_currency;
  product.m_time = kArrivalTimeSeconds;

  auto const data = json_object_get(root.get(), "data");

  FromJSONObject(data, "cost", product.m_price);

  products.emplace_back(std::move(product));
}

CityMapping LoadCityMapping()
{
  std::string fileData;
  try
  {
    auto const fileReader = GetPlatform().GetReader(kMappingFilepath);
    fileReader->ReadAsString(fileData);
  }
  catch (FileAbsentException const & ex)
  {
    LOG(LERROR, ("Exception while get reader for file:", kMappingFilepath, "reason:", ex.what()));
    return {};
  }
  catch (FileReader::Exception const & ex)
  {
    LOG(LERROR, ("Exception while reading file:", kMappingFilepath, "reason:", ex.what()));
    return {};
  }

  ASSERT(!fileData.empty(), ());

  CityMapping result;

  try
  {
    my::Json root(fileData.c_str());

    auto const count = json_array_size(root.get());
    std::string osmName;
    City city;

    for (size_t i = 0; i < count; ++i)
    {
      auto const item = json_array_get(root.get(), i);

      FromJSONObject(item, "osm", osmName);
      FromJSONObject(item, "rutaxi", city.m_id);
      FromJSONObject(item, "currency", city.m_currency);

      result.emplace(osmName, city);
    }
  }
  catch (my::Json::Exception const & ex)
  {
    LOG(LWARNING, ("Exception while parsing file:", kMappingFilepath, "reason:", ex.what(),
                   "json:", fileData));
    return {};
  }

  return result;
}
}  // namespace rutaxi
}  // namespace taxi