AYON Cpp Api  0.1.0
Loading...
Searching...
No Matches
AyonCppApi.h
Go to the documentation of this file.
1#ifndef AYONCPPAPI_H
2#define AYONCPPAPI_H
3
4#include <cstdint>
5
6#include <condition_variable>
7#include <memory>
8#include <mutex>
9#include <optional>
10#include <string>
11#include <unordered_map>
12#include <utility>
13#include <vector>
14
15#include <sys/types.h>
16
17#ifdef __linux__
18#include <dlfcn.h>
19#endif
20
21#include "httplib.h"
22#include "nlohmann/json_fwd.hpp"
23
24#include "appDataFolder.h"
25#include "lib/ynput/lib/logging/AyonLogger.hpp"
26
32class AyonApi {
33 public:
37 AyonApi(const std::optional<std::string> &logFilePos,
38 const std::string &authKey,
39 const std::string &serverUrl,
40 const std::string &ayonProjectName,
41 const std::string &siteId,
42 std::optional<int> concurrency = std::nullopt);
46 ~AyonApi();
47
51 std::string getKey();
55 std::string getUrl();
56
65 nlohmann::json GET(const std::shared_ptr<std::string> endPoint,
66 const std::shared_ptr<httplib::Headers> headers,
67 uint8_t successStatus);
68
78 nlohmann::json SPOST(const std::shared_ptr<std::string> endPoint,
79 const std::shared_ptr<httplib::Headers> headers,
80 nlohmann::json jsonPayload,
81 const std::shared_ptr<uint8_t> successStatus);
91 nlohmann::json CPOST(const std::shared_ptr<std::string> endPoint,
92 const std::shared_ptr<httplib::Headers> headers,
93 nlohmann::json jsonPayload,
94 const std::shared_ptr<uint8_t> successStatus);
95
103 std::pair<std::string, std::string> resolvePath(const std::string &uriPath);
104
111 std::unordered_map<std::string, std::string> batchResolvePath(std::vector<std::string> &uriPaths);
112
126 std::unordered_map<std::string, std::string> batchResolvePathSerial(const std::vector<std::string> &uriPaths);
127
135 std::pair<std::string, std::string> getAssetIdent(const nlohmann::json &uriResolverResponse);
136
140 std::shared_ptr<AyonLogger> logPointer();
141
146 const std::unordered_map<std::string, std::string>& getSiteRoots(); // TODO think about if this should only support current project or multiple projects
147
154 std::string rootReplace(const std::string &rootLessPath);
155
156 private:
165 std::string serialCorePost(const std::string &endPoint,
166 httplib::Headers headers,
167 std::string &payload,
168 const int &successStatus);
177 std::string generativeCorePost(const std::string &endPoint,
178 httplib::Headers headers,
179 std::string &payload,
180 const int &successStatus);
181
187 std::string convertUriVecToString(const std::vector<std::string> &uriVec);
188
195 bool isSSL() const;
196
200 void setSSL();
201
202 // Core Dependencies
203 std::unique_ptr<httplib::Client> m_ayonServer;
204 std::shared_ptr<AyonLogger> m_log;
205
206 // Configuration from Constructor
207 const std::string m_authKey;
208 const std::string m_serverUrl;
209 std::string m_ayonProjectName;
210 std::string m_siteId;
211 const int m_numThreads;
212
213 // Runtime State
214 std::unordered_map<std::string, std::string> m_siteRoots;
215 httplib::Headers m_headers;
216 std::string m_userName;
217 bool m_serverBusy = false;
218
219 // URI Resolution Configuration
220 std::string m_uriResolverEndpoint = "/api/resolve";
221 std::string m_uriResolverEndpointPathOnlyVar = "?pathOnly=true";
223
232 // Max uris per POST in batchResolvePathSerial. The server resolves a batch in one
233 // sequential transaction, so an unbounded request holds a DB connection for the whole
234 // frontier (risking its connection-pool 503 guard) and grows the body/response without
235 // limit. Splitting into capped chunks releases the connection between chunks. Matches
236 // m_regroupSizeForAsyncRequests so the serial and async paths cap requests alike.
237 uint16_t m_maxSerialBatchSize = 200;
238
239 // Retry and Timeout Configuration
240 uint8_t m_maxCallRetries = 8;
241 uint16_t m_retryWait = 800;
242 uint16_t m_serverBusyCode = 503;
245 uint8_t m_readTimeoutMax = 160;
247
248 // Thread Synchronization
251 std::condition_variable m_serverBusyCondVar;
253};
254
255#endif // !AYONCPPAPI_H
Central Ayon api class Class for exposing Ayon server functions to C++ users.
Definition: AyonCppApi.h:32
uint16_t m_regroupSizeForAsyncRequests
Definition: AyonCppApi.h:229
std::string getUrl()
Returns the stored AYON server URL.
Definition: AyonCppApi.cpp:662
std::string serialCorePost(const std::string &endPoint, httplib::Headers headers, std::string &payload, const int &successStatus)
Calls the server in a serial way by sharing the AyonServer pointer.
Definition: AyonCppApi.cpp:670
std::string m_uriResolverEndpoint
Definition: AyonCppApi.h:220
const int m_numThreads
Definition: AyonCppApi.h:211
httplib::Headers m_headers
Definition: AyonCppApi.h:215
std::string rootReplace(const std::string &rootLessPath)
Replaces {root[var]} for ayon:// paths.
Definition: AyonCppApi.cpp:260
std::unordered_map< std::string, std::string > batchResolvePathSerial(const std::vector< std::string > &uriPaths)
Resolves a vector of paths in a SINGLE batched request over the persistent keep-alive client.
Definition: AyonCppApi.cpp:567
uint8_t m_maxConcurrentRequestsAfterServerBusy
Definition: AyonCppApi.h:252
bool m_serverBusy
Definition: AyonCppApi.h:217
std::unordered_map< std::string, std::string > m_siteRoots
Definition: AyonCppApi.h:214
uint8_t m_minGroupSizeForAsyncRequests
Definition: AyonCppApi.h:228
std::shared_ptr< AyonLogger > m_log
Definition: AyonCppApi.h:204
std::pair< std::string, std::string > resolvePath(const std::string &uriPath)
Uses the URI resolve endpoint on the AYON server to resolve a URI path to the local path.
Definition: AyonCppApi.cpp:420
uint8_t m_readTimeoutMax
Definition: AyonCppApi.h:245
const std::unordered_map< std::string, std::string > & getSiteRoots()
Gets the site root overwrites for the current project.
Definition: AyonCppApi.cpp:229
std::mutex m_concurrentRequestAfterServerBusyMutex
Definition: AyonCppApi.h:250
std::string m_userName
Definition: AyonCppApi.h:216
uint8_t m_maxCallRetries
Definition: AyonCppApi.h:240
uint16_t m_connectionTimeoutMax
Definition: AyonCppApi.h:244
uint16_t m_serverBusyCode
Definition: AyonCppApi.h:242
std::unique_ptr< httplib::Client > m_ayonServer
Definition: AyonCppApi.h:203
std::mutex m_ayonServerMutex
Definition: AyonCppApi.h:249
bool m_pathOnlyResolution
Definition: AyonCppApi.h:222
std::condition_variable m_serverBusyCondVar
Definition: AyonCppApi.h:251
uint16_t m_generativeCorePostMaxLoopIterations
Definition: AyonCppApi.h:246
std::string m_ayonProjectName
Definition: AyonCppApi.h:209
std::string generativeCorePost(const std::string &endPoint, httplib::Headers headers, std::string &payload, const int &successStatus)
Calls the server while creating a new client instance to stay async.
Definition: AyonCppApi.cpp:714
uint16_t m_minVecSizeForGroupSplitAsyncRequests
Definition: AyonCppApi.h:231
std::pair< std::string, std::string > getAssetIdent(const nlohmann::json &uriResolverResponse)
Takes an AYON path URI response (resolved ayon://path) and returns a pair of asset identifier (ayon:/...
Definition: AyonCppApi.cpp:632
const std::string m_authKey
Definition: AyonCppApi.h:207
std::string convertUriVecToString(const std::vector< std::string > &uriVec)
Converts a vector of URIs into a string to serve into CorePost functions.
Definition: AyonCppApi.cpp:824
std::unordered_map< std::string, std::string > batchResolvePath(std::vector< std::string > &uriPaths)
Resolves a vector of paths against the AYON server asynchronously using auto-generated batch requests...
Definition: AyonCppApi.cpp:441
nlohmann::json SPOST(const std::shared_ptr< std::string > endPoint, const std::shared_ptr< httplib::Headers > headers, nlohmann::json jsonPayload, const std::shared_ptr< uint8_t > successStatus)
POST request via a shared httplib client (serial)
Definition: AyonCppApi.cpp:342
nlohmann::json GET(const std::shared_ptr< std::string > endPoint, const std::shared_ptr< httplib::Headers > headers, uint8_t successStatus)
Runs a GET command.
Definition: AyonCppApi.cpp:294
void setSSL()
Sets the SSL cert path for the m_ayonServer httplib client.
Definition: AyonCppApi.cpp:850
std::string getKey()
Returns the stored API key.
Definition: AyonCppApi.cpp:655
bool m_batchResolveOptimizeVector
Decides if the cpp API removes duplicates from batch request vector.
Definition: AyonCppApi.h:227
bool isSSL() const
Checks if the m_ayonServer is running on SSL based on m_serverUrl Simple implementation - httplib's b...
Definition: AyonCppApi.cpp:844
~AyonApi()
Destructor.
Definition: AyonCppApi.cpp:224
std::string m_uriResolverEndpointPathOnlyVar
Definition: AyonCppApi.h:221
uint16_t m_maxGroupSizeForAsyncRequests
Definition: AyonCppApi.h:230
uint16_t m_maxSerialBatchSize
Definition: AyonCppApi.h:237
nlohmann::json CPOST(const std::shared_ptr< std::string > endPoint, const std::shared_ptr< httplib::Headers > headers, nlohmann::json jsonPayload, const std::shared_ptr< uint8_t > successStatus)
HTTP POST request utilizing the creation of a new httplib client (Generative Async)
Definition: AyonCppApi.cpp:387
const std::string m_serverUrl
Definition: AyonCppApi.h:208
std::string m_siteId
Definition: AyonCppApi.h:210
uint16_t m_retryWait
Definition: AyonCppApi.h:241
std::shared_ptr< AyonLogger > logPointer()
Get function for shared AyonLogger pointer used by this class instance.
Definition: AyonCppApi.cpp:838
uint16_t m_requestDelayWhenServerBusy
Definition: AyonCppApi.h:243