Azure Cosmos REST-API Client for Modern C++
$ dotnet add package SiddiqSoft.CosmosClientThere is a need for a light-weight Azure Cosmos client that is crafted for C++ instead of "C" wrapper.
/c++20 no longer includes format and our code is stuck at /c++latest.You can start saving your documents in Cosmos with just 3 lines of code!
configurecreateThe detailed API is over at Github pages.
#include "nlohmann/json.hpp"
#include "siddiqsoft/azure-cosmos-restcl.hpp"
// See the example1 test for full source code
// Code here has been trimmed for brevity.
void example1(const std::string& p, const std::string& s)
{
// You should maintain one instance for each Cosmos service
// It is advised to maintain a single instance per runtime (exe/dll/service)
siddiqsoft::CosmosClient cc;
// If you provide valid information, this will configure by probing Azure for
// region information and sets up the read and write locations.
// We use JSON object as the primary configuration interface.
// The items you provide are muxed with the defaults so you can provide as much
// or as little as the following elements.
cc.configure({{"partitionKeyNames", {"__pk"}},
{"connectionStrings", {p, s}}} );
// This is all we need to worry about when creating a document!
// Cosmos requires the "id" field be in the document so we perform
// no checks for id nor the primaryKey field.
// The goal is to get you the response from Cosmos with little more
// than convenience overhead.
// Minimal abstractions ("there are no zero-cost abstractions").
// Just use the JSON object!
// Here, we ask a document to be created and our lambda be invoked
// on success or failure.
cc.async({.operation = siddiqsoft::CosmosOperation::create,
.database = dbName,
.collection = collectionName,
.document = {{"id", docId}, // We send the JSON document
{"ttl", 360}, // inline for this example
{"__pk", pkId}, // PKID is required
{"func", __func__},
{"source", "basic_tests.exe"}},
.onResponse = [&cc](auto const& ctx, auto const& resp){
if( 201 == resp.statusCode ) {
// Document created..
..
..
..
// You can even nest (this will queue into the worker
// pool another async request.. Just make sure you
// capture the cc instance.
// The ctx contains the current context/arguments for this callback.
cc.async({.operation = siddiqsoft::CosmosOperation::remove,
.database = ctx.database,
.collection = ctx.collection,
.id = ctx.id, // or resp.document["id"] from create op
.partitionKey = ctx.partitionKey,
.onResponse = [](auto const& ctx, auto const& resp){
// on remove of document..
}}); // end of the internal async op
}
else {
// handle failures..
}
}}); // end of the create async op
}
© 2021 Siddiq Software LLC. All rights reserved.