From b0ba3c6d20ab6886789f630eba691b2a7770c10b Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 2 Aug 2018 18:08:15 -0400 Subject: [PATCH] Rename request to network, move to its own namespace --- include/{util/request.hpp => network.hpp} | 10 ++++++---- src/app/RackScene.cpp | 1 - src/app/app.cpp | 4 ++-- src/{util/request.cpp => network.cpp} | 14 +++++++++----- src/plugin.cpp | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 22 deletions(-) rename include/{util/request.hpp => network.hpp} (69%) rename src/{util/request.cpp => network.cpp} (95%) diff --git a/include/util/request.hpp b/include/network.hpp similarity index 69% rename from include/util/request.hpp rename to include/network.hpp index fad102d2..bd52208d 100644 --- a/include/util/request.hpp +++ b/include/network.hpp @@ -4,9 +4,10 @@ namespace rack { +namespace network { -enum RequestMethod { +enum Method { METHOD_GET, METHOD_POST, METHOD_PUT, @@ -16,13 +17,14 @@ enum RequestMethod { /** Requests a JSON API URL over HTTP(S), using the data as the query (GET) or the body (POST, etc) Caller must json_decref(). */ -json_t *requestJson(RequestMethod method, std::string url, json_t *dataJ); +json_t *requestJson(Method method, std::string url, json_t *dataJ); /** Returns true if downloaded successfully */ bool requestDownload(std::string url, std::string filename, float *progress); /** URL-encodes `s` */ -std::string requestEscape(std::string s); +std::string encodeUrl(std::string s); /** Computes the SHA256 of the file at `filename` */ -std::string requestSHA256File(std::string filename); +std::string computeSHA256File(std::string filename); +} // namespace network } // namespace rack diff --git a/src/app/RackScene.cpp b/src/app/RackScene.cpp index dadfcbbe..aafea19c 100644 --- a/src/app/RackScene.cpp +++ b/src/app/RackScene.cpp @@ -1,6 +1,5 @@ #include "app.hpp" #include "window.hpp" -#include "util/request.hpp" #include "osdialog.h" #include #include diff --git a/src/app/app.cpp b/src/app/app.cpp index 5fec6af6..9e609c6c 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -1,5 +1,5 @@ #include "app.hpp" -#include "util/request.hpp" +#include "network.hpp" #include @@ -20,7 +20,7 @@ RackScene *gRackScene = NULL; static void checkVersion() { - json_t *resJ = requestJson(METHOD_GET, gApiHost + "/version", NULL); + json_t *resJ = network::requestJson(network::METHOD_GET, gApiHost + "/version", NULL); if (resJ) { json_t *versionJ = json_object_get(resJ, "version"); diff --git a/src/util/request.cpp b/src/network.cpp similarity index 95% rename from src/util/request.cpp rename to src/network.cpp index 1e2324e6..2943ec15 100644 --- a/src/util/request.cpp +++ b/src/network.cpp @@ -1,11 +1,14 @@ -#include "util/common.hpp" -#include "util/request.hpp" #define CURL_STATICLIB #include #include +#include "util/common.hpp" +#include "network.hpp" + namespace rack { +namespace network { + static size_t writeStringCallback(char *ptr, size_t size, size_t nmemb, void *userdata) { std::string *str = (std::string*) userdata; @@ -15,7 +18,7 @@ static size_t writeStringCallback(char *ptr, size_t size, size_t nmemb, void *us } -json_t *requestJson(RequestMethod method, std::string url, json_t *dataJ) { +json_t *requestJson(Method method, std::string url, json_t *dataJ) { CURL *curl = curl_easy_init(); assert(curl); @@ -149,7 +152,7 @@ bool requestDownload(std::string url, std::string filename, float *progress) { return res == CURLE_OK; } -std::string requestEscape(std::string s) { +std::string encodeUrl(std::string s) { CURL *curl = curl_easy_init(); assert(curl); char *escaped = curl_easy_escape(curl, s.c_str(), s.size()); @@ -159,7 +162,7 @@ std::string requestEscape(std::string s) { return ret; } -std::string requestSHA256File(std::string filename) { +std::string computeSHA256File(std::string filename) { FILE *f = fopen(filename.c_str(), "rb"); if (!f) return ""; @@ -191,4 +194,5 @@ std::string requestSHA256File(std::string filename) { } +} // namespace network } // namespace rack diff --git a/src/plugin.cpp b/src/plugin.cpp index 75e1f337..20978b10 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -1,7 +1,7 @@ #include "plugin.hpp" #include "app.hpp" #include "asset.hpp" -#include "util/request.hpp" +#include "network.hpp" #include "osdialog.h" #include @@ -175,14 +175,14 @@ static bool syncPlugin(std::string slug, json_t *manifestJ, bool dryRun) { if (dryRun) { downloadUrl += "/available"; } - downloadUrl += "?token=" + requestEscape(gToken); - downloadUrl += "&slug=" + requestEscape(slug); - downloadUrl += "&version=" + requestEscape(latestVersion); - downloadUrl += "&arch=" + requestEscape(arch); + downloadUrl += "?token=" + network::encodeUrl(gToken); + downloadUrl += "&slug=" + network::encodeUrl(slug); + downloadUrl += "&version=" + network::encodeUrl(latestVersion); + downloadUrl += "&arch=" + network::encodeUrl(arch); if (dryRun) { // Check if available - json_t *availableResJ = requestJson(METHOD_GET, downloadUrl, NULL); + json_t *availableResJ = network::requestJson(network::METHOD_GET, downloadUrl, NULL); if (!availableResJ) { warn("Could not check whether download is available"); return false; @@ -200,7 +200,7 @@ static bool syncPlugin(std::string slug, json_t *manifestJ, bool dryRun) { // Download zip std::string pluginDest = asset::local("plugins/" + slug + ".zip"); - if (!requestDownload(downloadUrl, pluginDest, &downloadProgress)) { + if (!network::requestDownload(downloadUrl, pluginDest, &downloadProgress)) { warn("Plugin %s download was unsuccessful", slug.c_str()); return false; } @@ -385,7 +385,7 @@ bool pluginSync(bool dryRun) { // Get user's plugins list json_t *pluginsReqJ = json_object(); json_object_set(pluginsReqJ, "token", json_string(gToken.c_str())); - json_t *pluginsResJ = requestJson(METHOD_GET, gApiHost + "/plugins", pluginsReqJ); + json_t *pluginsResJ = network::requestJson(network::METHOD_GET, gApiHost + "/plugins", pluginsReqJ); json_decref(pluginsReqJ); if (!pluginsResJ) { warn("Request for user's plugins failed"); @@ -402,7 +402,7 @@ bool pluginSync(bool dryRun) { } // Get community manifests - json_t *manifestsResJ = requestJson(METHOD_GET, gApiHost + "/community/manifests", NULL); + json_t *manifestsResJ = network::requestJson(network::METHOD_GET, gApiHost + "/community/manifests", NULL); if (!manifestsResJ) { warn("Request for community manifests failed"); return false; @@ -450,7 +450,7 @@ void pluginLogIn(std::string email, std::string password) { json_t *reqJ = json_object(); json_object_set(reqJ, "email", json_string(email.c_str())); json_object_set(reqJ, "password", json_string(password.c_str())); - json_t *resJ = requestJson(METHOD_POST, gApiHost + "/token", reqJ); + json_t *resJ = network::requestJson(network::METHOD_POST, gApiHost + "/token", reqJ); json_decref(reqJ); if (resJ) {