Skip to content

Commit

Permalink
🔧 optimizing result value by std::move()-ing the result vectors from …
Browse files Browse the repository at this point in the history
…the paginating calls instead of copying into temporary
  • Loading branch information
pysco68 committed Feb 29, 2024
1 parent 5bfeffa commit 1a9dcec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
9 changes: 6 additions & 3 deletions gh/branches.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ BOOST_FUSION_ADAPT_STRUCT(gh::repos::branch_t, name, commit, protection_url, is_

namespace gh {

using namespace std::string_literals;

/**
* \brief list github branches, passing them to the result_handler as std::vector<branch_t>.
* See https://developer.github.com/v3/repos/branches/#list-branches
Expand All @@ -54,7 +56,8 @@ namespace gh {
* \param result_handler Callable with signature : `func(std::vector<branch_t>)`
*/
inline void list_branches(std::string owner, std::string repos, std::function<void(repos::branches&&)>&& result_handler,
std::optional<auth> auth = std::nullopt) {
std::optional<auth> auth = std::nullopt,
const std::string& api_endpoint = "https://api.github.com"s) {

using namespace xxhr;
std::function<void(xxhr::Response&&)> response_handler;
Expand Down Expand Up @@ -94,14 +97,14 @@ namespace gh {
do_request(next_page.value());
}
else {
result_handler({all_branches.begin(), all_branches.end()});
result_handler(std::move(all_branches));
}
} else {
throw std::runtime_error(resp.url + " failed with error: " + std::string(resp.error) + " - " + resp.text);
}
};

auto url = "https://api.github.com/repos/"s + owner + "/" + repos + "/branches?" + gh::detail::pagination::get_per_page_query_string();
auto url = api_endpoint + "/repos/" + owner + "/" + repos + "/branches?" + gh::detail::pagination::get_per_page_query_string();
do_request(url);
}
}
2 changes: 1 addition & 1 deletion gh/issues.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace gh {
do_request(next_page.value());
}
else {
result_handler({ all_issues.begin(), all_issues.end()});
result_handler(std::move(all_issues));
}
} else {
throw std::runtime_error( "err : "s + std::string(resp.error) + "status: "s
Expand Down
3 changes: 1 addition & 2 deletions gh/releases.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,8 @@ namespace gh {
do_request(next_page.value());
}
else {
result_handler({all_releases.begin(), all_releases.end()});
result_handler(std::move(all_releases));
}


} else {
throw std::runtime_error( "err : "s + std::string(resp.error) + "status: "s
Expand Down
2 changes: 1 addition & 1 deletion gh/search.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace gh {
do_request(next_page.value(), xxhr::Parameters{});
}
else {
result_handler({all_results.begin(), all_results.end()});
result_handler(std::move(all_results));
}
} else {
throw std::runtime_error(resp.url + " failed with error: " + std::string(resp.error));
Expand Down

0 comments on commit 1a9dcec

Please sign in to comment.