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
120 std::pair<std::string, std::string> getAssetIdent(const nlohmann::json &uriResolverResponse);
121
125 std::shared_ptr<AyonLogger> logPointer();
126
131 const std::unordered_map<std::string, std::string>& getSiteRoots(); // TODO think about if this should only support current project or multiple projects
132
139 std::string rootReplace(const std::string &rootLessPath);
140
141 private:
150 std::string serialCorePost(const std::string &endPoint,
151 httplib::Headers headers,
152 std::string &payload,
153 const int &successStatus);
162 std::string generativeCorePost(const std::string &endPoint,
163 httplib::Headers headers,
164 std::string &payload,
165 const int &successStatus);
166
172 std::string convertUriVecToString(const std::vector<std::string> &uriVec);
173
180 bool isSSL() const;
181
185 void setSSL();
186
187 // Core Dependencies
188 std::unique_ptr<httplib::Client> m_ayonServer;
189 std::shared_ptr<AyonLogger> m_log;
190
191 // Configuration from Constructor
192 const std::string m_authKey;
193 const std::string m_serverUrl;
194 std::string m_ayonProjectName;
195 std::string m_siteId;
196 const int m_numThreads;
197
198 // Runtime State
199 std::unordered_map<std::string, std::string> m_siteRoots;
200 httplib::Headers m_headers;
201 std::string m_userName;
202 bool m_serverBusy = false;
203
204 // URI Resolution Configuration
205 std::string m_uriResolverEndpoint = "/api/resolve";
206 std::string m_uriResolverEndpointPathOnlyVar = "?pathOnly=true";
208
217
218 // Retry and Timeout Configuration
219 uint8_t m_maxCallRetries = 8;
220 uint16_t m_retryWait = 800;
221 uint16_t m_serverBusyCode = 503;
224 uint8_t m_readTimeoutMax = 160;
226
227 // Thread Synchronization
230 std::condition_variable m_serverBusyCondVar;
232};
233
234#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:214
std::string getUrl()
Returns the stored AYON server URL.
Definition: AyonCppApi.cpp:584
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:592
std::string m_uriResolverEndpoint
Definition: AyonCppApi.h:205
const int m_numThreads
Definition: AyonCppApi.h:196
httplib::Headers m_headers
Definition: AyonCppApi.h:200
std::string rootReplace(const std::string &rootLessPath)
Replaces {root[var]} for ayon:// paths.
Definition: AyonCppApi.cpp:246
uint8_t m_maxConcurrentRequestsAfterServerBusy
Definition: AyonCppApi.h:231
bool m_serverBusy
Definition: AyonCppApi.h:202
std::unordered_map< std::string, std::string > m_siteRoots
Definition: AyonCppApi.h:199
uint8_t m_minGroupSizeForAsyncRequests
Definition: AyonCppApi.h:213
std::shared_ptr< AyonLogger > m_log
Definition: AyonCppApi.h:189
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:406
uint8_t m_readTimeoutMax
Definition: AyonCppApi.h:224
const std::unordered_map< std::string, std::string > & getSiteRoots()
Gets the site root overwrites for the current project.
Definition: AyonCppApi.cpp:215
std::mutex m_concurrentRequestAfterServerBusyMutex
Definition: AyonCppApi.h:229
std::string m_userName
Definition: AyonCppApi.h:201
uint8_t m_maxCallRetries
Definition: AyonCppApi.h:219
uint16_t m_connectionTimeoutMax
Definition: AyonCppApi.h:223
uint16_t m_serverBusyCode
Definition: AyonCppApi.h:221
std::unique_ptr< httplib::Client > m_ayonServer
Definition: AyonCppApi.h:188
std::mutex m_ayonServerMutex
Definition: AyonCppApi.h:228
bool m_pathOnlyResolution
Definition: AyonCppApi.h:207
std::condition_variable m_serverBusyCondVar
Definition: AyonCppApi.h:230
uint16_t m_generativeCorePostMaxLoopIterations
Definition: AyonCppApi.h:225
std::string m_ayonProjectName
Definition: AyonCppApi.h:194
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:636
uint16_t m_minVecSizeForGroupSplitAsyncRequests
Definition: AyonCppApi.h:216
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:554
const std::string m_authKey
Definition: AyonCppApi.h:192
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:746
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:427
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:328
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:280
void setSSL()
Sets the SSL cert path for the m_ayonServer httplib client.
Definition: AyonCppApi.cpp:772
std::string getKey()
Returns the stored API key.
Definition: AyonCppApi.cpp:577
bool m_batchResolveOptimizeVector
Decides if the cpp API removes duplicates from batch request vector.
Definition: AyonCppApi.h:212
bool isSSL() const
Checks if the m_ayonServer is running on SSL based on m_serverUrl Simple implementation - httplib's b...
Definition: AyonCppApi.cpp:766
~AyonApi()
Destructor.
Definition: AyonCppApi.cpp:210
std::string m_uriResolverEndpointPathOnlyVar
Definition: AyonCppApi.h:206
uint16_t m_maxGroupSizeForAsyncRequests
Definition: AyonCppApi.h:215
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:373
const std::string m_serverUrl
Definition: AyonCppApi.h:193
std::string m_siteId
Definition: AyonCppApi.h:195
uint16_t m_retryWait
Definition: AyonCppApi.h:220
std::shared_ptr< AyonLogger > logPointer()
Get function for shared AyonLogger pointer used by this class instance.
Definition: AyonCppApi.cpp:760
uint16_t m_requestDelayWhenServerBusy
Definition: AyonCppApi.h:222