1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
\page cppapi C++ API
## Overview
Ignition Fuel Tools lets you perform a set of operations over the resources
hosted in any Ignition Fuel server. These operations allow you to list all
resources of a particular server, get the details of a resource or download it.
We're going to run a few examples, so let's start by creating a directory for
this tutorial:
```bash
mkdir /tmp/fuel_tutorial && cd /tmp/fuel_tutorial
```
Download the files `list.cc`, `details.cc`, `download.cc`,
`CMakeLists.txt`, and save them under `/tmp/fuel_tutorial`:
```bash
# Ubuntu or MacOS
wget https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/list.cc
wget https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/details.cc
wget https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/download.cc
wget https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/CMakeLists.txt
# Windows
## CMD
curl -sk https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/list.cc -o list.cc
curl -sk https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/details.cc -o details.cc
curl -sk https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/download.cc -o download.cc
curl -sk https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/CMakeLists.txt -o CMakeLists.txt
## PowerShell
curl https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/list.cc -o list.cc
curl https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/details.cc -o details.cc
curl https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/download.cc -o download.cc
curl https://github.com/ignitionrobotics/ign-fuel-tools/raw/main/example/CMakeLists.txt -o CMakeLists.txt
```
Let's start by compiling the examples:
```bash
mkdir build && cd build
cmake ..
```
### Ubuntu and MacOS
```bash
make
```
### Windows
```bash
cmake --build . --config Release
```
## List resources from a Fuel server
Run the following example to see the list of models hosted in the default
server:
### Ubuntu and MacOS
`./list -t world`
### Windows
`.\Release\list.exe -t world`
You should see the name of the server followed by its list of models. Here's an
example:
```
[https://fuel.ignitionrobotics.org]
Beer
Wastebasket
ambulance3
ambulance2
appartment3
hammer
another test model
Test Model
Bowl
Cardboard Box
Cessna C-172
Fire station (collapsed)
Beer
Apartment
Ambulance
```
### Walkthrough
This example contains some boilerplate to parse command line arguments and load
some configuration. Refer to the [configuration tutorial](configuration.html)
for details about this part of the code. Let's focus on the relevant code for
listing resources:
```
for (const auto &server : client.Config().Servers())
{
std::cout << "[" << server.Url().Str() << "]\n\n";
if (FLAGS_t == "model")
{
for (auto iter = client.Models(server); iter; ++iter)
std::cout << " " << iter->Identification().Name() << "\n";
}
else if (FLAGS_t == "world")
{
for (auto iter = client.Worlds(server); iter; ++iter)
std::cout << " " << iter->Name() << "\n";
}
std::cout << std::endl;
}
```
As you can see, we're iterating over the list of servers. For each server, we
use the `Models()` or the `Worlds()` method to retrieve all the relevant
resources from that server. Then, iterate over the resulting resources and print
their names.
## Get details of a resource
Run the following example to see the details of a model hosted in the default
server:
```
# Ubuntu and MacOS
./details -o caguero -n Beer -t model
# Windows
.\Release\details.exe -o caguero -n Beer -t model
```
You should see the details of the model.
Here's an example:
```
Name: Beer
Owner: caguero
Version: 2
Description: A beer can
File size: 2969297
Upload date: 1516833583
Downloads: 1573
License name: Creative Commons - Attribution
License URL: http://creativecommons.org/licenses/by/4.0
License image URL: https://i.creativecommons.org/l/by/4.0/88x31.png
Server:
URL: https://fuel.ignitionrobotics.org
Version: 1.0
```
### Walkthrough
This example contains some boilerplate to parse command line arguments and load
some configuration. Refer to the [configuration tutorial](configuration.html)
for details about this part of the code. Let's focus on the relevant code for
getting details of a resource:
```
// Fetch the details.
for (const auto &server : client.Config().Servers())
{
if (FLAGS_t == "model")
{
// Set server
auto id = modelIdentifier;
id.SetServer(server);
ignition::fuel_tools::ModelIdentifier model;
if (!client.ModelDetails(server, id, model))
continue;
std::cout << model.AsPrettyString() << std::endl;
}
else if (FLAGS_t == "world")
{
// Set server
auto id = worldIdentifier;
id.SetServer(server);
ignition::fuel_tools::WorldIdentifier world;
if (!client.WorldDetails(id, world))
continue;
std::cout << world.AsPrettyString() << std::endl;
}
}
```
As usual, we're iterating over the list of servers. For each server, we
use the `ModelDetails()` or `WorldDetails()` methods to retrieve most of the
properties of the resource. Then if the resource is retrieved successfully, we
use the `AsPrettyString()` method to print its information to the screen.
## Download a resource
Refer to the
[configuration tutorial](configuration.html)
to see an example of how to download resources programmatically.
|