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

uber_api.cpp « partners_api - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4896ed3acd8cabc9867330a4bd2848cff43b60ec (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "partners_api/uber_api.hpp"
#include "partners_api/utils.hpp"

#include "geometry/latlon.hpp"

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

#include "std/iomanip.hpp"
#include "std/sstream.hpp"

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

#include "private.h"

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)
    return false;

  // Uber products are not available at this point.
  if (!json_is_array(answer))
    return false;

  if (json_array_size(answer) <= 0)
    return false;

  return true;
}

bool IsIncomplete(taxi::Product const & p)
{
  return p.m_name.empty() || p.m_productId.empty() || p.m_time.empty() || p.m_price.empty();
}

void FillProducts(json_t const * time, json_t const * price, vector<taxi::Product> & products)
{
  // Fill data from time.
  auto const timeSize = json_array_size(time);
  for (size_t i = 0; i < timeSize; ++i)
  {
    taxi::Product product;
    int64_t estimatedTime = 0;
    auto const item = json_array_get(time, i);
    FromJSONObject(item, "display_name", product.m_name);
    FromJSONObject(item, "estimate", estimatedTime);
    product.m_time = strings::to_string(estimatedTime);
    products.push_back(move(product));
  }

  // Fill data from price.
  auto const priceSize = json_array_size(price);
  for (size_t i = 0; i < priceSize; ++i)
  {
    string name;
    auto const item = json_array_get(price, i);

    FromJSONObject(item, "display_name", name);
    auto const it = find_if(products.begin(), products.end(), [&name](taxi::Product const & product)
    {
      return product.m_name == name;
    });

    if (it == products.end())
      continue;

    FromJSONObject(item, "product_id", it->m_productId);
    FromJSONObject(item, "estimate", it->m_price);

    // The field currency_code can contain null in case when price equal to Metered.
    auto const currency = json_object_get(item, "currency_code");
    if (currency != nullptr && !json_is_null(currency))
      it->m_currency = json_string_value(currency);
  }

  products.erase(remove_if(products.begin(), products.end(), IsIncomplete), products.end());
}

void MakeFromJson(char const * times, char const * prices, vector<taxi::Product> & products)
{
  products.clear();
  try
  {
    my::Json timesRoot(times);
    my::Json pricesRoot(prices);
    auto const timesArray = json_object_get(timesRoot.get(), "times");
    auto const pricesArray = json_object_get(pricesRoot.get(), "prices");
    if (CheckUberResponse(timesArray) && CheckUberResponse(pricesArray))
    {
      FillProducts(timesArray, pricesArray, products);
    }
  }
  catch (my::Json::Exception const & e)
  {
    LOG(LERROR, (e.Msg()));
    products.clear();
  }
}
}  // namespace

namespace taxi
{
namespace uber
{
string const kEstimatesUrl = "https://api.uber.com/v1/estimates";
string const kProductsUrl = "https://api.uber.com/v1/products";

// static
bool RawApi::GetProducts(ms::LatLon const & pos, string & result,
                         std::string const & baseUrl /* = kProductsUrl */)
{
  ostringstream url;
  url << fixed << setprecision(6) << baseUrl << "?server_token=" << UBER_SERVER_TOKEN
      << "&latitude=" << pos.lat << "&longitude=" << pos.lon;

  return RunSimpleHttpRequest(url.str(), result);
}

// static
bool RawApi::GetEstimatedTime(ms::LatLon const & pos, string & result,
                              std::string const & baseUrl /* = kEstimatesUrl */)
{
  ostringstream url;
  url << fixed << setprecision(6) << baseUrl << "/time?server_token=" << UBER_SERVER_TOKEN
      << "&start_latitude=" << pos.lat << "&start_longitude=" << pos.lon;

  return RunSimpleHttpRequest(url.str(), result);
}

// static
bool RawApi::GetEstimatedPrice(ms::LatLon const & from, ms::LatLon const & to, string & result,
                               std::string const & baseUrl /* = kEstimatesUrl */)
{
  ostringstream url;
  url << fixed << setprecision(6) << baseUrl << "/price?server_token=" << UBER_SERVER_TOKEN
      << "&start_latitude=" << from.lat << "&start_longitude=" << from.lon
      << "&end_latitude=" << to.lat << "&end_longitude=" << to.lon;

  return RunSimpleHttpRequest(url.str(), result);
}

void ProductMaker::Reset(uint64_t const requestId)
{
  lock_guard<mutex> lock(m_mutex);

  m_requestId = requestId;
  m_times.reset();
  m_prices.reset();
}

void ProductMaker::SetTimes(uint64_t const requestId, string const & times)
{
  lock_guard<mutex> lock(m_mutex);

  if (requestId != m_requestId)
    return;

  m_times = make_unique<string>(times);
}

void ProductMaker::SetPrices(uint64_t const requestId, string const & prices)
{
  lock_guard<mutex> lock(m_mutex);

  if (requestId != m_requestId)
    return;

  m_prices = make_unique<string>(prices);
}

void ProductMaker::SetError(uint64_t const requestId, taxi::ErrorCode code)
{
  lock_guard<mutex> lock(m_mutex);

  if (requestId != m_requestId)
    return;

  m_error = make_unique<taxi::ErrorCode>(code);
}

void ProductMaker::MakeProducts(uint64_t const requestId, ProductsCallback const & successFn,
                                ErrorProviderCallback const & errorFn)
{
  ASSERT(successFn, ());
  ASSERT(errorFn, ());

  vector<Product> products;
  unique_ptr<taxi::ErrorCode> error;
  {
    lock_guard<mutex> lock(m_mutex);

    if (requestId != m_requestId || !m_times || !m_prices)
      return;

    if (!m_error)
    {
      if (!m_times->empty() && !m_prices->empty())
        MakeFromJson(m_times->c_str(), m_prices->c_str(), products);

      if (products.empty())
        m_error = my::make_unique<taxi::ErrorCode>(ErrorCode::NoProducts);
    }

    if (m_error)
      error = my::make_unique<taxi::ErrorCode>(*m_error);

    // Reset m_times and m_prices because we need to call callback only once.
    m_times.reset();
    m_prices.reset();
  }

  if (error)
    errorFn(*error);
  else
    successFn(products);
}

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

  if (!IsDistanceSupported(from, to))
  {
    // TODO(a): Add ErrorCode::FarDistance and provide this error code.
    errorFn(ErrorCode::NoProducts);
    return;
  }

  auto const reqId = ++m_requestId;
  auto const maker = m_maker;
  auto const baseUrl = m_baseUrl;

  maker->Reset(reqId);

  threads::SimpleThread([maker, from, reqId, baseUrl, successFn, errorFn]()
  {
    string result;
    if (!RawApi::GetEstimatedTime(from, result, baseUrl))
      maker->SetError(reqId, ErrorCode::RemoteError);

    maker->SetTimes(reqId, result);
    maker->MakeProducts(reqId, successFn, errorFn);
  }).detach();

  threads::SimpleThread([maker, from, to, reqId, baseUrl, successFn, errorFn]()
  {
    string result;
    if (!RawApi::GetEstimatedPrice(from, to, result, baseUrl))
      maker->SetError(reqId, ErrorCode::RemoteError);

    maker->SetPrices(reqId, result);
    maker->MakeProducts(reqId, successFn, errorFn);
  }).detach();
}

RideRequestLinks Api::GetRideRequestLinks(string const & productId, ms::LatLon const & from,
                                          ms::LatLon const & to) const
{
  stringstream url;
  url << fixed << setprecision(6)
      << "?client_id=" << UBER_CLIENT_ID << "&action=setPickup&product_id=" << productId
      << "&pickup[latitude]=" << from.lat << "&pickup[longitude]=" << from.lon
      << "&dropoff[latitude]=" << to.lat << "&dropoff[longitude]=" << to.lon;

  return {"uber://" + url.str(), "https://m.uber.com/ul" + url.str()};
}
}  // namespace uber
}  // namespace taxi