diff --git a/include/gz/fuel_tools/ClientConfig.hh b/include/gz/fuel_tools/ClientConfig.hh index da7eb962..870f8b39 100644 --- a/include/gz/fuel_tools/ClientConfig.hh +++ b/include/gz/fuel_tools/ClientConfig.hh @@ -25,6 +25,7 @@ #include #include "gz/fuel_tools/Export.hh" +#include "gz/fuel_tools/ServerConfig.hh" #ifdef _WIN32 // Disable warning C4251 which is triggered by @@ -37,73 +38,9 @@ namespace gz { namespace fuel_tools { - /// \brief forward declaration - class ServerConfigPrivate; - /// \brief Forward Declaration class ClientConfigPrivate; - /// \brief Describes options needed for a server. - class GZ_FUEL_TOOLS_VISIBLE ServerConfig - { - /// \brief Constructor. - public: ServerConfig(); - - /// \brief Copy constructor. - /// \param[in] _orig The server config to copy. - public: ServerConfig(const ServerConfig &_orig); - - /// \brief Assignment operator overload. - /// \param[in] _orig The server config to copy. - public: ServerConfig &operator=(const ServerConfig &_orig); - - /// \brief Destructor. - public: ~ServerConfig(); - - /// \brief Clear the server config. This will set all values to empty - /// strings, except the version string which will be set to its default - /// value. - public: void Clear(); - - /// \brief Get the URL to access the server. - /// \return The URL of this server. - public: common::URI Url() const; - - /// \brief Set the URL of this server. - /// \param[in] _url URL of this server. - public: void SetUrl(const common::URI &_url); - - /// \brief Get the API key to auth with the server. - /// \return The API key. - public: std::string ApiKey() const; - - /// \brief Set the API key to auth with the server. - /// \param[in] _key The API key. - public: void SetApiKey(const std::string &_key); - - /// \brief Get the protocol version used with this server. - /// \return The version. E.g.: "1.0". - public: std::string Version() const; - - /// \brief Set the protocol version used with this server. - /// \param[in] _version The version. E.g.: "1.0". - public: void SetVersion(const std::string &_version); - - /// \brief Returns all the server information as a string. - /// \param[in] _prefix Optional prefix for every line of the string. - /// \return Server information string - public: std::string AsString(const std::string &_prefix = "") const; - - /// \brief Returns all the available model information as a string using - /// colors for better human parsing. - /// \param[in] _prefix Optional prefix for every line of the string. - /// \return Model information string - public: std::string AsPrettyString(const std::string &_prefix = "") const; - - /// \brief PIMPL - private: std::unique_ptr dataPtr; - }; - /// \brief High level interface to Gazebo Fuel. /// class GZ_FUEL_TOOLS_VISIBLE ClientConfig diff --git a/include/gz/fuel_tools/ServerConfig.hh b/include/gz/fuel_tools/ServerConfig.hh new file mode 100644 index 00000000..66e7abc9 --- /dev/null +++ b/include/gz/fuel_tools/ServerConfig.hh @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2017 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#ifndef GZ_FUEL_TOOLS_SERVERCONFIG_HH_ +#define GZ_FUEL_TOOLS_SERVERCONFIG_HH_ + +#include +#include + +#include + +#include "gz/fuel_tools/Export.hh" + +#ifdef _WIN32 +// Disable warning C4251 which is triggered by +// std::unique_ptr +#pragma warning(push) +#pragma warning(disable: 4251) +#endif + +namespace gz +{ + namespace fuel_tools + { + /// \brief forward declaration + class ServerConfigPrivate; + + /// \brief Describes options needed for a server. + class GZ_FUEL_TOOLS_VISIBLE ServerConfig + { + /// \brief Constructor. + public: ServerConfig(); + + /// \brief Copy constructor. + /// \param[in] _orig The server config to copy. + public: ServerConfig(const ServerConfig &_orig); + + /// \brief Assignment operator overload. + /// \param[in] _orig The server config to copy. + public: ServerConfig &operator=(const ServerConfig &_orig); + + /// \brief Destructor. + public: ~ServerConfig(); + + /// \brief Clear the server config. This will set all values to empty + /// strings, except the version string which will be set to its default + /// value. + public: void Clear(); + + /// \brief Get the URL to access the server. + /// \return The URL of this server. + public: common::URI Url() const; + + /// \brief Set the URL of this server. + /// \param[in] _url URL of this server. + public: void SetUrl(const common::URI &_url); + + /// \brief Get the API key to auth with the server. + /// \return The API key. + public: std::string ApiKey() const; + + /// \brief Set the API key to auth with the server. + /// \param[in] _key The API key. + public: void SetApiKey(const std::string &_key); + + /// \brief Get the protocol version used with this server. + /// \return The version. E.g.: "1.0". + public: std::string Version() const; + + /// \brief Set the protocol version used with this server. + /// \param[in] _version The version. E.g.: "1.0". + public: void SetVersion(const std::string &_version); + + /// \brief Returns all the server information as a string. + /// \param[in] _prefix Optional prefix for every line of the string. + /// \return Server information string + public: std::string AsString(const std::string &_prefix = "") const; + + /// \brief Returns all the available model information as a string using + /// colors for better human parsing. + /// \param[in] _prefix Optional prefix for every line of the string. + /// \return Model information string + public: std::string AsPrettyString(const std::string &_prefix = "") const; + + /// \brief PIMPL + private: std::unique_ptr dataPtr; + }; + } +} + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#endif // GZ_FUEL_TOOLS_SERVERCONFIG_HH_ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c2ec97ac..2748e73f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ set (sources ModelIter.cc RestClient.cc Result.cc + ServerConfig.cc Zip.cc WorldIdentifier.cc WorldIter.cc @@ -31,6 +32,7 @@ set (gtest_sources Model_TEST.cc RestClient_TEST.cc Result_TEST.cc + ServerConfig_TEST.cc WorldIdentifier_TEST.cc WorldIter_TEST.cc Zip_TEST.cc diff --git a/src/ClientConfig.cc b/src/ClientConfig.cc index 1399e90a..371ff21d 100644 --- a/src/ClientConfig.cc +++ b/src/ClientConfig.cc @@ -70,134 +70,6 @@ class gz::fuel_tools::ClientConfigPrivate "GazeboFuelTools-" GZ_FUEL_TOOLS_VERSION_FULL; }; -////////////////////////////////////////////////// -/// \brief Private data class -class gz::fuel_tools::ServerConfigPrivate -{ - /// \brief Clear values. - public: void Clear() - { - this->url.Clear(); - this->key = ""; - this->version = "1.0"; - } - - /// \brief URL to reach server - public: common::URI url{"https://fuel.gazebosim.org"}; - - /// \brief A key to auth with the server - public: std::string key = ""; - - /// \brief The protocol version used when talking with this server. - public: std::string version = "1.0"; -}; - -////////////////////////////////////////////////// -ServerConfig::ServerConfig() - : dataPtr (new ServerConfigPrivate) -{ -} - -////////////////////////////////////////////////// -ServerConfig::ServerConfig(const ServerConfig &_orig) - : dataPtr(new ServerConfigPrivate) -{ - *(this->dataPtr) = *(_orig.dataPtr); -} - -////////////////////////////////////////////////// -void ServerConfig::Clear() -{ - this->dataPtr->Clear(); -} - -////////////////////////////////////////////////// -ServerConfig &ServerConfig::operator=(const ServerConfig &_orig) -{ - *(this->dataPtr) = *(_orig.dataPtr); - return *this; -} - -////////////////////////////////////////////////// -ServerConfig::~ServerConfig() -{ -} - -////////////////////////////////////////////////// -common::URI ServerConfig::Url() const -{ - return this->dataPtr->url; -} - -////////////////////////////////////////////////// -void ServerConfig::SetUrl(const common::URI &_url) -{ - this->dataPtr->url = _url; -} - -////////////////////////////////////////////////// -std::string ServerConfig::ApiKey() const -{ - return this->dataPtr->key; -} - -////////////////////////////////////////////////// -void ServerConfig::SetApiKey(const std::string &_key) -{ - this->dataPtr->key = _key; -} - -////////////////////////////////////////////////// -std::string ServerConfig::Version() const -{ - return this->dataPtr->version; -} - -////////////////////////////////////////////////// -void ServerConfig::SetVersion(const std::string &_version) -{ - this->dataPtr->version = _version; -} - -////////////////////////////////////////////////// -std::string ServerConfig::AsString(const std::string &_prefix) const -{ - std::stringstream out; - out << _prefix << "URL: " << this->Url().Str() << std::endl - << _prefix << "Version: " << this->Version() << std::endl - << _prefix << "API key: " << this->ApiKey() << std::endl; - return out.str(); -} - -////////////////////////////////////////////////// -std::string ServerConfig::AsPrettyString(const std::string &_prefix) const -{ - std::string prop = "\033[96m\033[1m"; - std::string value = "\033[37m"; - std::string reset = "\033[0m"; - - std::stringstream out; - - if (this->Url().Valid()) - { - out << _prefix << prop << "URL: " << reset - << value << this->Url().Str() << reset << std::endl; - } - - if (!this->Version().empty()) - { - out << _prefix << prop << "Version: " << reset - << value << this->Version() << reset << std::endl; - } - - if (!this->ApiKey().empty()) - { - out << _prefix << prop << "API key: " << reset - << value << this->ApiKey() << reset << std::endl; - } - return out.str(); -} - ////////////////////////////////////////////////// ClientConfig::ClientConfig() : dataPtr(new ClientConfigPrivate) { diff --git a/src/ClientConfig_TEST.cc b/src/ClientConfig_TEST.cc index facd548e..6a071db3 100644 --- a/src/ClientConfig_TEST.cc +++ b/src/ClientConfig_TEST.cc @@ -65,8 +65,6 @@ class ClientConfigTest: public ::testing::Test public: std::shared_ptr tempDir; }; -class ServerConfigTest: public ClientConfigTest {}; - ///////////////////////////////////////////////// /// \brief Initially only the default server in config TEST_F(ClientConfigTest, InitiallyDefaultServers) @@ -261,19 +259,6 @@ TEST_F(ClientConfigTest, UserAgent) EXPECT_EQ("my_user_agent", config.UserAgent()); } -///////////////////////////////////////////////// -TEST_F(ServerConfigTest, ApiKey) -{ - ServerConfig config; - EXPECT_TRUE(config.ApiKey().empty()); - - config.SetApiKey("my_api_key"); - EXPECT_EQ("my_api_key", config.ApiKey()); - - config.SetApiKey("my_other_api_key"); - EXPECT_EQ("my_other_api_key", config.ApiKey()); -} - ///////////////////////////////////////////////// TEST_F(ClientConfigTest, AsString) { @@ -364,48 +349,3 @@ TEST_F(ClientConfigTest, AsPrettyString) EXPECT_NE(str.find("ABCD"), std::string::npos); } } - -///////////////////////////////////////////////// -TEST_F(ServerConfigTest, Url) -{ - // Invalid URL string - { - ServerConfig srv; - srv.SetUrl(common::URI("asdf")); - EXPECT_TRUE(srv.Url().Str().empty()); - } - - // Valid URL - { - ServerConfig srv; - srv.SetUrl(common::URI("http://banana:8080")); - EXPECT_EQ("http://banana:8080", srv.Url().Str()); - EXPECT_EQ("http", srv.Url().Scheme()); - EXPECT_EQ("banana:8080", srv.Url().Path().Str()); - EXPECT_FALSE(srv.Url().Authority()); - } - - // Trailing / - { - ServerConfig srv; - srv.SetUrl(common::URI("http://banana:8080")); - EXPECT_EQ("http://banana:8080", srv.Url().Str()); - EXPECT_EQ("http", srv.Url().Scheme()); - EXPECT_EQ("banana:8080", srv.Url().Path().Str()); - EXPECT_FALSE(srv.Url().Authority()); - } - - // Set from URI - { - auto url = common::URI(); - url.SetScheme("http"); - url.Path() = common::URIPath("banana:8080"); - - ServerConfig srv; - srv.SetUrl(url); - EXPECT_EQ("http://banana:8080", srv.Url().Str()); - EXPECT_EQ("http", srv.Url().Scheme()); - EXPECT_EQ("banana:8080", srv.Url().Path().Str()); - EXPECT_FALSE(srv.Url().Authority()); - } -} diff --git a/src/ServerConfig.cc b/src/ServerConfig.cc new file mode 100644 index 00000000..fffaaff6 --- /dev/null +++ b/src/ServerConfig.cc @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2017 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gz/fuel_tools/ServerConfig.hh" + +using namespace gz; +using namespace fuel_tools; + +////////////////////////////////////////////////// +/// \brief Private data class +class gz::fuel_tools::ServerConfigPrivate +{ + /// \brief Clear values. + public: void Clear() + { + this->url.Clear(); + this->key = ""; + this->version = "1.0"; + } + + /// \brief URL to reach server + public: common::URI url{"https://fuel.gazebosim.org"}; + + /// \brief A key to auth with the server + public: std::string key = ""; + + /// \brief The protocol version used when talking with this server. + public: std::string version = "1.0"; +}; + +////////////////////////////////////////////////// +ServerConfig::ServerConfig() + : dataPtr (new ServerConfigPrivate) +{ +} + +////////////////////////////////////////////////// +ServerConfig::ServerConfig(const ServerConfig &_orig) + : dataPtr(new ServerConfigPrivate) +{ + *(this->dataPtr) = *(_orig.dataPtr); +} + +////////////////////////////////////////////////// +void ServerConfig::Clear() +{ + this->dataPtr->Clear(); +} + +////////////////////////////////////////////////// +ServerConfig &ServerConfig::operator=(const ServerConfig &_orig) +{ + *(this->dataPtr) = *(_orig.dataPtr); + return *this; +} + +////////////////////////////////////////////////// +ServerConfig::~ServerConfig() +{ +} + +////////////////////////////////////////////////// +common::URI ServerConfig::Url() const +{ + return this->dataPtr->url; +} + +////////////////////////////////////////////////// +void ServerConfig::SetUrl(const common::URI &_url) +{ + this->dataPtr->url = _url; +} + +////////////////////////////////////////////////// +std::string ServerConfig::ApiKey() const +{ + return this->dataPtr->key; +} + +////////////////////////////////////////////////// +void ServerConfig::SetApiKey(const std::string &_key) +{ + this->dataPtr->key = _key; +} + +////////////////////////////////////////////////// +std::string ServerConfig::Version() const +{ + return this->dataPtr->version; +} + +////////////////////////////////////////////////// +void ServerConfig::SetVersion(const std::string &_version) +{ + this->dataPtr->version = _version; +} + +////////////////////////////////////////////////// +std::string ServerConfig::AsString(const std::string &_prefix) const +{ + std::stringstream out; + out << _prefix << "URL: " << this->Url().Str() << std::endl + << _prefix << "Version: " << this->Version() << std::endl + << _prefix << "API key: " << this->ApiKey() << std::endl; + return out.str(); +} + +////////////////////////////////////////////////// +std::string ServerConfig::AsPrettyString(const std::string &_prefix) const +{ + std::string prop = "\033[96m\033[1m"; + std::string value = "\033[37m"; + std::string reset = "\033[0m"; + + std::stringstream out; + + if (this->Url().Valid()) + { + out << _prefix << prop << "URL: " << reset + << value << this->Url().Str() << reset << std::endl; + } + + if (!this->Version().empty()) + { + out << _prefix << prop << "Version: " << reset + << value << this->Version() << reset << std::endl; + } + + if (!this->ApiKey().empty()) + { + out << _prefix << prop << "API key: " << reset + << value << this->ApiKey() << reset << std::endl; + } + return out.str(); +} diff --git a/src/ServerConfig_TEST.cc b/src/ServerConfig_TEST.cc new file mode 100644 index 00000000..3e51e6e1 --- /dev/null +++ b/src/ServerConfig_TEST.cc @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2017 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gz/fuel_tools/ServerConfig.hh" + +using namespace gz; +using namespace fuel_tools; + +///////////////////////////////////////////////// +/// \brief Get home directory. +/// \return Home directory or empty string if home wasn't found. +/// \ToDo: Move this function to gz::common::Filesystem +std::string homePath() +{ + std::string homePath; +#ifndef _WIN32 + gz::common::env("HOME", homePath); +#else + gz::common::env("USERPROFILE", homePath); +#endif + return homePath; +} + +///////////////////////////////////////////////// +class ServerConfigTest: public ::testing::Test +{ + public: void SetUp() override + { + gz::common::Console::SetVerbosity(4); + tempDir = gz::common::testing::MakeTestTempDirectory(); + ASSERT_TRUE(tempDir->Valid()) << tempDir->Path(); + + gz::common::chdir(tempDir->Path()); + } + + public: std::string cachePath() + { + return this->tempDir->Path(); + } + + public: std::shared_ptr tempDir; +}; + +///////////////////////////////////////////////// +TEST_F(ServerConfigTest, ApiKey) +{ + ServerConfig config; + EXPECT_TRUE(config.ApiKey().empty()); + + config.SetApiKey("my_api_key"); + EXPECT_EQ("my_api_key", config.ApiKey()); + + config.SetApiKey("my_other_api_key"); + EXPECT_EQ("my_other_api_key", config.ApiKey()); +} + +///////////////////////////////////////////////// +TEST_F(ServerConfigTest, Url) +{ + // Invalid URL string + { + ServerConfig srv; + srv.SetUrl(common::URI("asdf")); + EXPECT_TRUE(srv.Url().Str().empty()); + } + + // Valid URL + { + ServerConfig srv; + srv.SetUrl(common::URI("http://banana:8080")); + EXPECT_EQ("http://banana:8080", srv.Url().Str()); + EXPECT_EQ("http", srv.Url().Scheme()); + EXPECT_EQ("banana:8080", srv.Url().Path().Str()); + EXPECT_FALSE(srv.Url().Authority()); + } + + // Trailing / + { + ServerConfig srv; + srv.SetUrl(common::URI("http://banana:8080")); + EXPECT_EQ("http://banana:8080", srv.Url().Str()); + EXPECT_EQ("http", srv.Url().Scheme()); + EXPECT_EQ("banana:8080", srv.Url().Path().Str()); + EXPECT_FALSE(srv.Url().Authority()); + } + + // Set from URI + { + auto url = common::URI(); + url.SetScheme("http"); + url.Path() = common::URIPath("banana:8080"); + + ServerConfig srv; + srv.SetUrl(url); + EXPECT_EQ("http://banana:8080", srv.Url().Str()); + EXPECT_EQ("http", srv.Url().Scheme()); + EXPECT_EQ("banana:8080", srv.Url().Path().Str()); + EXPECT_FALSE(srv.Url().Authority()); + } +}