Methods
Instantiation
SdkAPI
Create a new Spire Linux Agent Client.
- Python
- C
from oort_sdk_client import SdkApi
agent = SdkApi()
#include "SdkAPI.h"
apiClient_t *client;
client = apiClient_create();
/* ... */
apiClient_free(client);
SendFile
Sends a file from the payload to the ground via the API.
- Python
- C
from oort_sdk_client.models import SendOptions, SendFileRequest, RetrieveFileRequest
ttl = TTLParams(urgent=9000, bulk=43200, surplus=172800)
# Where args are the parsed arguments send via payload_exec
tags = {"token": str(args.window_id)}
send_options = SendOptions(ttl_params=ttl, reliable=True, tags=tags)
req = SendFileRequest(
destination="ground",
topic="my-topic",
filepath="/path/to/file",
options=send_options)
resp = agent.send_file(req)
print("File sent successfully, UUID is {}".format(resp.uuid))
/* NB: all the strings are owned by the request object and will be freed
with it so they must be copied if they come from storage that cannot
be freed, like static strings or argv. This example assumes that all
strings not explicitly initialized from a constant within the example
are allocated on the heap.
*/
send_file_request_t *req;
send_options_t *send_opt;
send_file_response_t *resp;
ttl_params_t *ttl;
list_t *tags;
keyValuePair_t *token_tag;
char token[50];
/* where window_id is the int spire passes to the payload_exec for the customer via the payload_shim */
sprintf(token, "%lu", window_id);
tags = list_createList(token);
token_tag = keyValuePair_create(strdup("token"), strdup(token));
list_addElement(tags, token_tag);
ttl = ttl_params_create(urgent, bulk, surplus);
send_opt = send_options_create(ttl, reliable, tags, NULL);
req = send_file_request_create(dest, filename, topic, send_opt);
resp = SdkAPI_sendFile(client, req);
if (resp != NULL) {
fprintf(stderr, "resp uuid = %s", resp->uuid);
send_file_response_free(resp);
}
send_file_request_free(req);
Arguments
Type | Description |
---|---|
SendFileRequest | SendFileRequest Object |
Return value
Type | Description |
---|---|
SendFileResponse | Contains the UUID assigned for this file transfer |
QueryAvailableFiles
Queries files that have been uplinked from the ground to the payload.
- Python
- C
from oort_sdk_client import SdkApi
agent = SdkApi()
topic = "my-topic"
available = agent.query_available_files(topic)
for item in available.files:
print("Available file: {item}".format(item=item)) # SDK model classes provide helpful __repr__ for debugging
char *topic = NULL;
apiClient_t *client;
available_files_response_t *resp;
/* Some API calls require us to create a request struct, which the SDK will
allocate dynamically and eventually free, so we need to ensure the
request's parameters are on the heap, to avoid trying to free static or
stack-allocated memory. queryAvailableFiles, on the other hand, doesn't
need a request struct, and copies its topic argument, so it's safe to
use argv or frame-local memory directly. */
topic = argv[1];
client = apiClient_create();
resp = SdkAPI_queryAvailableFiles(client, topic);
if (resp != NULL) {
listEntry_t *item;
fprintf(stderr, "Available files:\n");
list_ForEach(item, resp->files) {
file_info_t *finfo = (file_info_t *)item->data;
/* The most convenient way to inspect an SDK model struct is to print
its json-ified contents. All the models provide a function named
<model_name>_convertToJSON. The cJSON library is included with
the SDK, and is brought into scope automatically when you
`#include "SdkAPI.h"` For the API, refer to the header file in
https://github.com/nsat/oort-sdk-c at external/cJSON.h.*/
cJSON* file_info_json = file_info_convertToJSON(finfo);
fprintf(stderr, "%s\n", cJSON_Print(file_info_json));
}
available_files_response_free(resp);
}
apiClient_free(client);
Arguments
Type | Description |
---|---|
string | The topic to check for available files. |
Return value
Type | Description |
---|---|
AvailableFilesResponse | AvailableFilesResponse Object |
RetrieveFile
Retrieve a file returned from QueryAvailableFiles
- Python
- C
import os
from oort_sdk_client import SdkApi
from oort_sdk_client.models import RetrieveFileRequest
agent = SdkApi()
topic = "my-topic"
# download files that are available in the agent's inbox. Please note that this is not production-level code, and is
# typically done as part of the signaling configure step. Please refer to the examples in the customer documentation that
# has been provided separely
available_files = agent.query_available_files(topic).files
if not available.files:
print("No files available for retreival")
for new_file_info in available_files:
# Delivery Hints are set when uploading a file through the Tasking API
dest_path = new_file_info.delivery_hints.dest_path
mode = int(f"0o{new_file_info.delivery_hints.mode}", 8)
print(f"Retrieving {new_file.id} in {dest_path}")
# The oort agent will not create intermediate directories when extracting a file, so we need to create them ourselves
os.makedirs(os.path.dirname(dest_path), mode=0o755, exist_ok=True)
req = RetrieveFileRequest(
id=new_file_info.id,
save_path=dest_path)
rfinfo = agent.retrieve_file(req)
# We will set the file mode based on the delivery hints
os.chmod(dest_path, mode)
char *filename = NULL, *id = NULL;
apiClient_t *client;
retrieve_file_request_t *req;
file_info_t *finfo;
if (argc != 3) {
usage(argv[0]);
}
id = strdup(argv[1]);
filename = strdup(argv[2]);
client = apiClient_create();
/* This example assumes a known file ID and destination path for simplicity.
For a realistic example of how to get file ID and destination of uploaded
files from the agent, see the documentation for QueryAvailableFiles. */
req = retrieve_file_request_create(id, filename);
finfo = SdkAPI_retrieveFile(client, req);
if (finfo != NULL) {
fprintf(stderr, "retrieved id = %s to %s",
req->id, finfo->path);
file_info_free(finfo);
}
retrieve_file_request_free(req);
apiClient_free(client);
Retrieve an available file from the Data Pipeline API.
Arguments
Type | Description |
---|---|
RetrieveFileRequest | RetrieveFileRequest Object |
Return value
Type | Description |
---|---|
FileInfo | Details about the file retrieved |