Skip to content

Commit fd0337d

Browse files
authored
chore(paths): extract query pattern [arduino] (#184)
1 parent 083a8a8 commit fd0337d

File tree

1 file changed

+21
-60
lines changed

1 file changed

+21
-60
lines changed

src/api/paths.cpp

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "api/paths.h"
1111

12+
#include <numeric>
1213
#include <string>
1314

1415
namespace Ark {
@@ -22,6 +23,20 @@ namespace {
2223
constexpr const uint8_t URL_MAX_LEN = 128U;
2324
} //namespace
2425

26+
////////////////////////////////////////////////////////////////////////////////
27+
////////////////////////////////////////////////////////////////////////////////
28+
29+
static std::string
30+
joinQueryBody(const std::map<std::string, std::string>& bodyMap) {
31+
return std::accumulate(bodyMap.begin(), bodyMap.end(),
32+
std::string(),
33+
[](const std::string& result,
34+
const std::pair<const std::string, std::string>& p) {
35+
return result + (result.empty() ? "" : "&") + p.first + "=" + p.second;
36+
});
37+
}
38+
39+
////////////////////////////////////////////////////////////////////////////////
2540
////////////////////////////////////////////////////////////////////////////////
2641

2742
/**
@@ -116,16 +131,7 @@ std::pair<std::string, std::string> Blocks::search(
116131
url += "/search";
117132
url += query;
118133

119-
std::string parameterBuffer;
120-
auto count = 0UL;
121-
for (const auto& p : bodyParameters) {
122-
++count;
123-
parameterBuffer += p.first + '=' + p.second;
124-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
125-
parameterBuffer += '&';
126-
};
127-
};
128-
return { url, parameterBuffer };
134+
return { url, joinQueryBody(bodyParameters) };
129135
}
130136

131137
////////////////////////////////////////////////////////////////////////////////
@@ -185,16 +191,7 @@ std::pair<std::string, std::string> Businesses::search(
185191
url += "/search";
186192
url += query;
187193

188-
std::string parameterBuffer;
189-
auto count = 0UL;
190-
for (const auto& p : bodyParameters) {
191-
++count;
192-
parameterBuffer += p.first + '=' + p.second;
193-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
194-
parameterBuffer += '&';
195-
};
196-
};
197-
return { url, parameterBuffer };
194+
return { url, joinQueryBody(bodyParameters) };
198195
}
199196

200197
////////////////////////////////////////////////////////////////////////////////
@@ -241,16 +238,7 @@ std::pair<std::string, std::string> Bridgechains::search(
241238
url += "/search";
242239
url += query;
243240

244-
std::string parameterBuffer;
245-
auto count = 0UL;
246-
for (const auto& p : bodyParameters) {
247-
++count;
248-
parameterBuffer += p.first + '=' + p.second;
249-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
250-
parameterBuffer += '&';
251-
};
252-
};
253-
return { url, parameterBuffer };
241+
return { url, joinQueryBody(bodyParameters) };
254242
}
255243

256244
////////////////////////////////////////////////////////////////////////////////
@@ -360,16 +348,7 @@ std::pair<std::string, std::string> Locks::search(
360348
url += "/search";
361349
url += query;
362350

363-
std::string parameterBuffer;
364-
auto count = 0UL;
365-
for (const auto& p : bodyParameters) {
366-
++count;
367-
parameterBuffer += p.first + '=' + p.second;
368-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
369-
parameterBuffer += '&';
370-
};
371-
};
372-
return { url, parameterBuffer };
351+
return { url, joinQueryBody(bodyParameters) };
373352
}
374353

375354
////////////////////////////////////////////////////////////////////////////////
@@ -596,16 +575,7 @@ std::pair<std::string, std::string> Transactions::search(
596575
url += "/search";
597576
url += query;
598577

599-
std::string parameterBuffer;
600-
auto count = 0UL;
601-
for (const auto& p : bodyParameters) {
602-
++count;
603-
parameterBuffer += p.first + '=' + p.second;
604-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
605-
parameterBuffer += '&';
606-
};
607-
};
608-
return { url, parameterBuffer.c_str() };
578+
return { url, joinQueryBody(bodyParameters) };
609579
}
610580

611581
////////////////////////////////////////////////////////////////////////////////
@@ -788,16 +758,7 @@ std::pair<std::string, std::string> Wallets::search(
788758
url += "/search";
789759
url += query;
790760

791-
std::string parameterBuffer;
792-
auto count = 0UL;
793-
for (const auto& p : bodyParameters) {
794-
++count;
795-
parameterBuffer += p.first + '=' + p.second;
796-
if (bodyParameters.size() > 1 && count < bodyParameters.size()) {
797-
parameterBuffer += '&';
798-
};
799-
};
800-
return { url.c_str(), parameterBuffer.c_str() };
761+
return { url, joinQueryBody(bodyParameters) };
801762
}
802763

803764
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)