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

maxim_api.cpp « partners_api - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68488b0c2a33f09b7fb38a75d114f461318f5b10 (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
#include "partners_api/maxim_api.hpp"

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

#include "geometry/latlon.hpp"

#include "base/logging.hpp"

#include "std/target_os.hpp"

#include <iomanip>
#include <limits>
#include <sstream>

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

#include "private.h"

namespace
{
double const kSecInMinute = 60.;

bool RunSimpleHttpRequest(std::string const & url, std::string & result)
{
  platform::HttpClient request(url);
  return request.RunHttpRequest(result);
}
}  // namespace

namespace taxi
{
namespace maxim
{
std::string const kTaxiInfoUrl = "http://cabinet.taximaxim.ru/Services/Public.svc";

bool RawApi::GetTaxiInfo(ms::LatLon const & from, ms::LatLon const & to, std::string & result,
                         std::string const & baseUrl /* = kTaxiInfoUrl */)
{
  std::ostringstream url;
  url << std::fixed << std::setprecision(6) << baseUrl
      << "/CalculateByCoords?version=1.0&platform=WEB&RefOrgId="
      << MAXIM_CLIENT_ID << "&access-token=" << MAXIM_SERVER_TOKEN
      << "&startLatitude=" << from.lat << "&startLongitude=" << from.lon
      << "&endLatitude=" << to.lat << "&endLongitude=" << to.lon;

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

/// Requests list of available products from Maxim.
void Api::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to,
                               ProductsCallback const & successFn,
                               ErrorProviderCallback const & errorFn)
{
  ASSERT(successFn, ());
  ASSERT(errorFn, ());

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

  auto const baseUrl = m_baseUrl;

  GetPlatform().RunTask(Platform::Thread::Network, [from, to, baseUrl, successFn, errorFn]()
  {
    std::string result;
    if (!RawApi::GetTaxiInfo(from, to, result, baseUrl))
    {
      errorFn(ErrorCode::RemoteError);
      return;
    }

    std::vector<Product> products;
    try
    {
      MakeFromJson(result, products);
    }
    catch (base::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 Maxim app.
RideRequestLinks Api::GetRideRequestLinks(std::string const & productId, ms::LatLon const & from,
                                          ms::LatLon const & to) const
{
  std::ostringstream orderLink;
  std::ostringstream installLink;

  orderLink << "order?refOrgId=" << MAXIM_CLIENT_ID << "&startLatitude=" << from.lat
            << "&startLongitude=" << from.lon << "&endLatitude=" << to.lat
            << "&endLongitude=" << to.lon;

#if defined(OMIM_OS_IPHONE)
  installLink << "https://app.appsflyer.com/id579985456?pid=maps_me";
#elif defined(OMIM_OS_ANDROID)
  installLink << "https://app.appsflyer.com/com.taxsee.taxsee?pid=maps_me";
#endif

  return {"maximzakaz://" + orderLink.str(), installLink.str()};
}

void MakeFromJson(std::string const & src, std::vector<taxi::Product> & products)
{
  products.clear();

  base::Json root(src.c_str());
  if (!json_is_object(root.get()))
    return;

  bool success = false;
  FromJSONObject(root.get(), "Success", success);

  if (!success)
    return;

  auto const price = json_object_get(root.get(), "Price");
  if (price == nullptr || json_is_null(price))
    return;

  double p = 0.0;
  FromJSON(price, p);
  if (p == 0.0)
    return;

  taxi::Product product;

  FromJSONObject(root.get(), "PriceString", product.m_price);
  FromJSONObject(root.get(), "CurrencyCode", product.m_currency);

  double time = 0.0;
  FromJSONObject(root.get(), "FeedTime", time);
  product.m_time = strings::to_string(static_cast<int64_t>(time * kSecInMinute));

  products.push_back(move(product));
}
}  // namespace maxim
}  // namespace taxi