Skip to content

Commit

Permalink
Merge branch 'gz-fuel-tools9' into Crola1702/9_to_main_01-03
Browse files Browse the repository at this point in the history
  • Loading branch information
Crola1702 committed Jan 3, 2024
2 parents 1e0a69d + 934d28d commit 34a3a32
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 148 deletions.
7 changes: 7 additions & 0 deletions include/gz/fuel_tools/FuelClient.hh
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ namespace gz
const std::vector<WorldIdentifier> &_ids,
size_t _jobs = 2);

/// \brief Check if a model is already present in the local cache.
/// \param[in] _id The model identifier
/// \param[out] _path Local path where the model can be found.
/// \return FETCH_ERROR if not cached, FETCH_ALREADY_EXISTS if cached.
public: Result CachedModel(const ModelIdentifier &_id,
std::string &_path);

/// \brief Check if a model is already present in the local cache.
/// \param[in] _modelUrl The unique URL of the model on a Fuel server.
/// E.g.: https://fuel.gazebosim.org/1.0/caguero/models/Beer
Expand Down
15 changes: 15 additions & 0 deletions include/gz/fuel_tools/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ namespace gz
{
namespace fuel_tools
{
/// \brief Convert the authority portion of a URI to a string
/// suitable to be used as a path on disk for all platforms.
///
/// It encodes illegal characters on Windows and Linux filesystems with
/// their corresponding URL-encoded values.
///
/// This assumes an authority of the form: username@host:port
/// "@" is encoded as %40
/// ":" is encoded as %3A
///
/// \param[in] _uriAuthority the authority section of the URI to convert.
/// \return String suitable to use in file paths
GZ_FUEL_TOOLS_VISIBLE
std::string sanitizeAuthority(const std::string &_uriAuthority);

/// \brief Convert a URI to a string suitable to use as a path on disk.
/// It strips the scheme and authority's `//` prefix.
/// \param[in] _uri URI to convert.
Expand Down
128 changes: 128 additions & 0 deletions src/ClientConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,134 @@ 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", true};

/// \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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionIdentifier_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST(CollectionIdentifier, SetFields)
/////////////////////////////////////////////////
/// \brief Unique Name
// See https://github.com/gazebosim/gz-fuel-tools/issues/231
TEST(CollectionIdentifier, GZ_UTILS_TEST_DISABLED_ON_WIN32(UniqueName))
TEST(CollectionIdentifier, UniqueName)
{
gz::fuel_tools::ServerConfig srv1;
srv1.SetUrl(common::URI("https://localhost:8001"));
Expand Down
71 changes: 44 additions & 27 deletions src/FuelClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,8 @@ Result FuelClient::ModelDependencies(const ModelIdentifier &_id,
// Locate any dependencies from the input model and download them.
std::string path;
gz::msgs::FuelMetadata meta;
if (this->CachedModel(gz::common::URI(_id.UniqueName()), path))

if (this->CachedModel(_id, path))
{
std::string metadataPath =
gz::common::joinPaths(path, "metadata.pbtxt");
Expand Down Expand Up @@ -1097,7 +1098,11 @@ bool FuelClient::ParseModelUrl(const common::URI &_modelUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1163,7 +1168,11 @@ bool FuelClient::ParseWorldUrl(const common::URI &_worldUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1231,7 +1240,11 @@ bool FuelClient::ParseModelFileUrl(const common::URI &_modelFileUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1300,7 +1313,11 @@ bool FuelClient::ParseWorldFileUrl(const common::URI &_worldFileUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1367,7 +1384,11 @@ bool FuelClient::ParseCollectionUrl(const common::URI &_url,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1414,9 +1435,6 @@ Result FuelClient::DownloadModel(const common::URI &_modelUrl,
if (!result)
return result;

// TODO(anyone) We shouldn't need to reconstruct the path, SaveModel should
// be changed to return it

// We need to figure out the version for the tip
if (id.Version() == 0 || id.VersionStr() == "tip")
{
Expand All @@ -1425,8 +1443,7 @@ Result FuelClient::DownloadModel(const common::URI &_modelUrl,
}

_path = gz::common::joinPaths(this->Config().CacheLocation(),
id.Server().Url().Path().Str(), id.Owner(), "models", id.Name(),
id.VersionStr());
id.UniqueName(), id.VersionStr());

return result;
}
Expand All @@ -1453,15 +1470,23 @@ Result FuelClient::DownloadWorld(const common::URI &_worldUrl,
}

//////////////////////////////////////////////////
bool FuelClient::CachedModel(const common::URI &_modelUrl)
Result FuelClient::CachedModel(const ModelIdentifier &_id,
std::string &_path)
{
// Get data from URL
ModelIdentifier id;
if (!this->ParseModelUrl(_modelUrl, id))
return Result(ResultType::FETCH_ERROR);
auto modelIter = this->dataPtr->cache->MatchingModel(_id);
if (modelIter)
{
_path = modelIter.PathToModel();
return Result(ResultType::FETCH_ALREADY_EXISTS);
}
return Result(ResultType::FETCH_ERROR);
}

// Check local cache
return this->dataPtr->cache->MatchingModel(id) ? true : false;
//////////////////////////////////////////////////
bool FuelClient::CachedModel(const common::URI &_modelUrl)
{
std::string path;
return this->CachedModel(_modelUrl, path).Type() != ResultType::FETCH_ERROR;
}

//////////////////////////////////////////////////
Expand All @@ -1475,15 +1500,7 @@ Result FuelClient::CachedModel(const common::URI &_modelUrl,
return Result(ResultType::FETCH_ERROR);
}

// Check local cache
auto modelIter = this->dataPtr->cache->MatchingModel(id);
if (modelIter)
{
_path = modelIter.PathToModel();
return Result(ResultType::FETCH_ALREADY_EXISTS);
}

return Result(ResultType::FETCH_ERROR);
return this->CachedModel(id, _path);
}

//////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 34a3a32

Please sign in to comment.