From 8308fa4e154a89a7d00d0098b0ecc85258b97a77 Mon Sep 17 00:00:00 2001 From: Caroline Twigg Date: Wed, 3 Oct 2018 09:48:11 -0400 Subject: [PATCH 1/4] add success codes to accept --- src/env/env.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/env/env.h b/src/env/env.h index 118bcf3..4499ee0 100644 --- a/src/env/env.h +++ b/src/env/env.h @@ -10,6 +10,7 @@ #define VERIFY_AUTH Env::instance()->verify_auth #define ENV_PATHS Env::instance()->paths +const std::list SUCCESS_CODES = {200, 201, 202, 203, 204}; const std::string METIS_API_TOKEN = "METIS_API_TOKEN"; const std::string METIS_AUTH_TOKEN = "METIS_OAUTH"; const std::string METIS_DEFAULT_ORG = "METIS_DEFAULT_ORG"; From 476c9fbf6240ffe2c8cc42a9a38eb4f5d08d9061 Mon Sep 17 00:00:00 2001 From: Caroline Twigg Date: Wed, 3 Oct 2018 09:49:01 -0400 Subject: [PATCH 2/4] error if status code is not one of the success codes --- src/organization/organization.cpp | 5 +++-- src/project/project.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/organization/organization.cpp b/src/organization/organization.cpp index 78a39aa..9685daa 100644 --- a/src/organization/organization.cpp +++ b/src/organization/organization.cpp @@ -1,5 +1,6 @@ #include "file/file.h" #include "organization.h" +#include "env/env.h" using namespace json11; @@ -11,7 +12,7 @@ void Organization::list() { int status_code = resp.code; std::string error_message = json["message"].string_value(); - if (status_code != 200) { + if (not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) { console::error("There was an error listing your organizations: " + std::to_string(status_code) + " - " + error_message + "\n"); } else if (json.is_array()) { console::info("Your organizations: "); @@ -32,7 +33,7 @@ void Organization::set_default(std::string org_name) { int status_code = resp.code; std::string error_message = json["message"].string_value(); - if (status_code != 201) { + if (not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) { console::error("There was an error setting your default organization: " + std::to_string(status_code) + " - " + error_message + "\n"); } else { Env::instance()->write_default_org(org_name); diff --git a/src/project/project.cpp b/src/project/project.cpp index 50894dd..f1022ef 100644 --- a/src/project/project.cpp +++ b/src/project/project.cpp @@ -87,7 +87,7 @@ void Project::template_init(string name, string org_name, string tpl, bool maste status_code = resp.code; error_message = json["message"].string_value(); - if(status_code != 201) { + if(not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) { if (org_name.size() == 0) { console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n"); } else { @@ -173,7 +173,7 @@ void Project::existing_init(string name, string org_name, bool master) { status_code = resp.code; error_message = json["message"].string_value(); - if(status_code != 201) { + if(not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) { if (org_name.size() == 0) { console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n"); } else { From 576a7f9678d6330bb504f8ebe33d4143461872a8 Mon Sep 17 00:00:00 2001 From: Caroline Twigg Date: Wed, 3 Oct 2018 09:49:42 -0400 Subject: [PATCH 3/4] if not able to load default return false otherwise check token and org --- src/env/env.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/env/env.cpp b/src/env/env.cpp index f75c940..45cd829 100644 --- a/src/env/env.cpp +++ b/src/env/env.cpp @@ -38,9 +38,12 @@ void Env::setup() { } bool Env::authenticated() { - Env::instance()->load_defaults(); - string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG); - return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong") && (Request::org_by_name(default_org_name).code == 200)); + if (Env::instance()->load_defaults()){ + string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG); + cout << Request::org_by_name(default_org_name).body; + return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong") && (std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), Request::org_by_name(default_org_name).code) != SUCCESS_CODES.end())); + } + return false; } string Env::get(string key) { @@ -95,6 +98,10 @@ bool Env::load_defaults() { } string org_name = json["org_name"].string_value(); + if (org_name.length() < 1){ + console::error("Defaults error: no default organization found."); + return false; + } this->set(METIS_DEFAULT_ORG, org_name); return true; } From 03c982452c4d643885c074b776be7d962f4d912e Mon Sep 17 00:00:00 2001 From: Caroline Twigg Date: Wed, 3 Oct 2018 09:58:30 -0400 Subject: [PATCH 4/4] remove print --- src/env/env.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/env/env.cpp b/src/env/env.cpp index 45cd829..189ac96 100644 --- a/src/env/env.cpp +++ b/src/env/env.cpp @@ -40,7 +40,6 @@ void Env::setup() { bool Env::authenticated() { if (Env::instance()->load_defaults()){ string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG); - cout << Request::org_by_name(default_org_name).body; return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong") && (std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), Request::org_by_name(default_org_name).code) != SUCCESS_CODES.end())); } return false;