From e5daa5ddb535f3a827fbdb97d7b9a804834998b5 Mon Sep 17 00:00:00 2001 From: Suwatchai K Date: Tue, 21 Jan 2025 00:16:19 +0700 Subject: [PATCH] Rename enums and structs --- .../Async/Callback/Upload/Upload.ino | 6 +- .../Async/NoCallback/Upload/Upload.ino | 6 +- examples/CloudStorage/Sync/Upload/Upload.ino | 6 +- src/cloud_storage/CloudStorage.h | 156 ++--- src/cloud_storage/DataOptions.h | 52 +- src/core/AppBase.h | 10 +- src/core/AsyncClient/AsyncClient.h | 559 +++++++++--------- src/core/AsyncClient/RequestHandler.h | 106 ++-- src/core/AsyncClient/ResponseHandler.h | 41 +- src/core/AsyncResult/AsyncResult.h | 2 +- src/core/FirebaseApp.h | 52 +- src/core/JSON.h | 2 +- src/core/JWT.cpp | 6 +- src/database/DataOptions.h | 7 +- src/database/RealtimeDatabase.h | 119 ++-- src/firestore/CollectionGroups.h | 29 +- src/firestore/DataOptions.h | 62 +- src/firestore/Databases.h | 101 ++-- src/firestore/Documents.h | 70 +-- src/firestore/FirestoreBase.h | 175 +++--- src/functions/DataOptions.h | 40 +- src/functions/Functions.h | 247 ++++---- src/messaging/DataOptions.h | 21 +- src/messaging/Messaging.h | 35 +- src/storage/DataOptions.h | 30 +- src/storage/Storage.h | 135 ++--- 26 files changed, 1020 insertions(+), 1055 deletions(-) diff --git a/examples/CloudStorage/Async/Callback/Upload/Upload.ino b/examples/CloudStorage/Async/Callback/Upload/Upload.ino index bb06346b..54e2a676 100644 --- a/examples/CloudStorage/Async/Callback/Upload/Upload.ino +++ b/examples/CloudStorage/Async/Callback/Upload/Upload.ino @@ -26,12 +26,12 @@ * * 2.------------------------ * - * CloudStorage::upload(, , , , , ); + * CloudStorage::upload(, , , , , ); * * - The async client. * - The GoogleCloudStorage::Parent object included Storage bucket Id and object in its constructor. * - The filesystem data (file_config_data) obtained from FileConfig class object. - * - The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * - The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * - The async result callback (AsyncResultCallback). @@ -182,7 +182,7 @@ void loop() { taskCompleted = true; - GoogleCloudStorage::uploadOptions options; + GoogleCloudStorage::UploadOptions options; options.mime = "video/mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; diff --git a/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino b/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino index e044b4ec..17199081 100644 --- a/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino +++ b/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino @@ -26,12 +26,12 @@ * * 2.------------------------ * - * CloudStorage::upload(, , , , ); + * CloudStorage::upload(, , , , ); * * - The async client. * - The GoogleCloudStorage::Parent object included Storage bucket Id and object in its constructor. * - The filesystem data (file_config_data) obtained from FileConfig class object. - * - The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * - The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * - The async result (AsyncResult). @@ -181,7 +181,7 @@ void loop() { taskCompleted = true; - GoogleCloudStorage::uploadOptions options; + GoogleCloudStorage::UploadOptions options; options.mime = "video/mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; diff --git a/examples/CloudStorage/Sync/Upload/Upload.ino b/examples/CloudStorage/Sync/Upload/Upload.ino index f31d5349..d9cd49dc 100644 --- a/examples/CloudStorage/Sync/Upload/Upload.ino +++ b/examples/CloudStorage/Sync/Upload/Upload.ino @@ -26,12 +26,12 @@ * * 2.------------------------ * - * bool CloudStorage::upload(, , , ); + * bool CloudStorage::upload(, , , ); * * - The async client. * - The GoogleCloudStorage::Parent object included Storage bucket Id and object in its constructor. * - The filesystem data (file_config_data) obtained from FileConfig class object. - * - The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * - The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * @@ -186,7 +186,7 @@ void loop() { taskCompleted = true; - GoogleCloudStorage::uploadOptions options; + GoogleCloudStorage::UploadOptions options; options.mime = "media.mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; diff --git a/src/cloud_storage/CloudStorage.h b/src/cloud_storage/CloudStorage.h index 9ded9244..9249ccca 100644 --- a/src/cloud_storage/CloudStorage.h +++ b/src/cloud_storage/CloudStorage.h @@ -40,6 +40,12 @@ class CloudStorage friend class AppBase; public: + using Parent = GoogleCloudStorage::Parent; + using GetOptions = GoogleCloudStorage::GetOptions; + using UploadOptions = GoogleCloudStorage::UploadOptions; + using ListOptions = GoogleCloudStorage::ListOptions; + using DeleteOptions = GoogleCloudStorage::DeleteOptions; + std::vector cVec; // AsyncClient vector ~CloudStorage() {} @@ -96,9 +102,9 @@ class CloudStorage * @return Boolean value, indicates the success of the operation. * */ - bool download(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::GetOptions &options) + bool download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, GetOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::cs_download, false); return aClient.getResult()->lastError.code() == 0; } @@ -114,9 +120,9 @@ class CloudStorage * @param aResult The async result (AsyncResult). * */ - void download(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::GetOptions &options, AsyncResult &aResult) + void download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, GetOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download, true); + sendRequest(aClient, &aResult, NULL, "", parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::cs_download, true); } /** Download object from the Google Cloud Storage. @@ -132,9 +138,9 @@ class CloudStorage * @param uid The user specified UID of async result (optional). * */ - void download(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::GetOptions &options, AsyncResultCallback cb, const String &uid = "") + void download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, GetOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download, true); + sendRequest(aClient, nullptr, cb, uid, parent, &file, &options, nullptr, nullptr, GoogleCloudStorage::cs_download, true); } /** Upload file to the Google Cloud Storage. @@ -144,16 +150,16 @@ class CloudStorage * The bucketid is the Storage bucket Id of object to upload. * The object is the object to be stored in the Storage bucket. * @param file The filesystem data (file_config_data) obtained from FileConfig class object. - * @param options Optional. The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * @param options Optional. The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * * @return Boolean value, indicates the success of the operation. * */ - bool upload(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::uploadOptions &options) + bool upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, UploadOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_uploads, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::cs_uploads, false); return aClient.getResult()->lastError.code() == 0; } @@ -164,16 +170,16 @@ class CloudStorage * The bucketid is the Storage bucket Id of object to upload. * The object is the object to be stored in the Storage bucket. * @param file The filesystem data (file_config_data) obtained from FileConfig class object. - * @param options Optional. The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * @param options Optional. The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * @param aResult The async result (AsyncResult). * * */ - void upload(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::uploadOptions &options, AsyncResult &aResult) + void upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, UploadOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_uploads, true); + sendRequest(aClient, &aResult, NULL, "", parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::cs_uploads, true); } /** Upload file to the Google Cloud Storage. @@ -183,16 +189,16 @@ class CloudStorage * The bucketid is the Storage bucket Id of object to upload. * The object is the object to be stored in the Storage bucket. * @param file The filesystem data (file_config_data) obtained from FileConfig class object. - * @param options Optional. The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload. + * @param options Optional. The GoogleCloudStorage::UploadOptions that holds the information for insert options, properties and types of upload. * For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters * For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties * @param cb The async result callback (AsyncResultCallback). * @param uid The user specified UID of async result (optional). * */ - void upload(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, file_config_data &file, GoogleCloudStorage::uploadOptions &options, AsyncResultCallback cb, const String &uid = "") + void upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, UploadOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_uploads, true); + sendRequest(aClient, nullptr, cb, uid, parent, &file, nullptr, &options, nullptr, GoogleCloudStorage::cs_uploads, true); } /** Perform OTA update using a firmware (object) from the Google Cloud Storage. @@ -207,9 +213,9 @@ class CloudStorage * @return Boolean value, indicates the success of the operation. * */ - bool ota(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options) + bool ota(AsyncClientClass &aClient, const Parent &parent, GetOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download_ota, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_download_ota, false); return aClient.getResult()->lastError.code() == 0; } @@ -224,9 +230,9 @@ class CloudStorage * @param aResult The async result (AsyncResult). * */ - void ota(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options, AsyncResult &aResult) + void ota(AsyncClientClass &aClient, const Parent &parent, GetOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download_ota, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_download_ota, true); } /** Perform OTA update using a firmware (object) from the Google Cloud Storage. @@ -241,9 +247,9 @@ class CloudStorage * @param uid The user specified UID of async result (optional). * */ - void ota(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options, AsyncResultCallback cb, const String &uid = "") + void ota(AsyncClientClass &aClient, const Parent &parent, GetOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_download_ota, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_download_ota, true); } /** Get the metadata of object in Google Cloud Storage data bucket. @@ -258,9 +264,9 @@ class CloudStorage * @return String The response payload. * */ - String getMetadata(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options) + String getMetadata(AsyncClientClass &aClient, const Parent &parent, GetOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_get_meta, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_get_meta, false); return aClient.getResult()->c_str(); } @@ -275,9 +281,9 @@ class CloudStorage * @param aResult The async result (AsyncResult). * */ - void getMetadata(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options, AsyncResult &aResult) + void getMetadata(AsyncClientClass &aClient, const Parent &parent, GetOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_get_meta, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_get_meta, true); } /** Get the metadata of object in Google Cloud Storage data bucket. @@ -292,10 +298,10 @@ class CloudStorage * @param uid The user specified UID of async result (optional). * */ - void getMetadata(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::GetOptions &options, AsyncResultCallback cb, const String &uid = "") + void getMetadata(AsyncClientClass &aClient, const Parent &parent, GetOptions &options, AsyncResultCallback cb, const String &uid = "") { // file_config_data file; - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_get_meta, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_get_meta, true); } /** List all objects in Google Cloud Storage data bucket. @@ -308,9 +314,9 @@ class CloudStorage * @return String The response payload. * */ - String list(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::ListOptions &options) + String list(AsyncClientClass &aClient, const Parent &parent, ListOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::google_cloud_storage_request_type_list, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::cs_list, false); return aClient.getResult()->c_str(); } @@ -324,9 +330,9 @@ class CloudStorage * @param aResult The async result (AsyncResult). * */ - void list(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::ListOptions &options, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, ListOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::google_cloud_storage_request_type_list, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::cs_list, true); } /** List all objects in Google Cloud Storage data bucket. @@ -340,9 +346,9 @@ class CloudStorage * @param uid The user specified UID of async result (optional). * */ - void list(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::ListOptions &options, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, ListOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::google_cloud_storage_request_type_list, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, nullptr, nullptr, &options, GoogleCloudStorage::cs_list, true); } /** Delete the object in Google Cloud Storage data bucket. @@ -357,9 +363,9 @@ class CloudStorage * @return Boolean value, indicates the success of the operation. * */ - bool deleteObject(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::DeleteOptions &options) + bool deleteObject(AsyncClientClass &aClient, const Parent &parent, DeleteOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_delete, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_delete, false); return aClient.getResult()->lastError.code() == 0; } @@ -374,9 +380,9 @@ class CloudStorage * @param aResult The async result (AsyncResult). * */ - void deleteObject(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::DeleteOptions &options, AsyncResult &aResult) + void deleteObject(AsyncClientClass &aClient, const Parent &parent, DeleteOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_delete, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_delete, true); } /** Delete the object in Firebase Storage data bucket. @@ -391,9 +397,9 @@ class CloudStorage * @param uid The user specified UID of async result (optional). * */ - void deleteObject(AsyncClientClass &aClient, const GoogleCloudStorage::Parent &parent, GoogleCloudStorage::DeleteOptions &options, AsyncResultCallback cb, const String &uid = "") + void deleteObject(AsyncClientClass &aClient, const Parent &parent, DeleteOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::google_cloud_storage_request_type_delete, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, &options, nullptr, nullptr, GoogleCloudStorage::cs_delete, true); } #if defined(FIREBASE_OTA_STORAGE) @@ -440,28 +446,28 @@ class CloudStorage return nullptr; } - void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const GoogleCloudStorage::Parent &parent, file_config_data *file, const GoogleCloudStorage::BaseOptions *baseOptions, GoogleCloudStorage::uploadOptions *uploadOptions, const GoogleCloudStorage::ListOptions *listOptions, GoogleCloudStorage::google_cloud_storage_request_type requestType, bool async) + void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, file_config_data *file, const GoogleCloudStorage::BaseOptions *baseOptions, GoogleCloudStorage::UploadOptions *UploadOptions, const GoogleCloudStorage::ListOptions *listOptions, GoogleCloudStorage::google_cloud_storage_request_type requestType, bool async) { GoogleCloudStorage::DataOptions options; options.requestType = requestType; options.parent = parent; - if (uploadOptions && strlen(uploadOptions->insertProps.c_str()) && uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable) - options.payload = uploadOptions->insertProps.c_str(); + if (UploadOptions && strlen(UploadOptions->insertProps.c_str()) && UploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable) + options.payload = UploadOptions->insertProps.c_str(); - async_request_handler_t::http_request_method method = async_request_handler_t::http_post; + reqns::http_request_method method = reqns::http_post; - if (requestType == GoogleCloudStorage::google_cloud_storage_request_type_download || - requestType == GoogleCloudStorage::google_cloud_storage_request_type_download_ota || - requestType == GoogleCloudStorage::google_cloud_storage_request_type_list || - requestType == GoogleCloudStorage::google_cloud_storage_request_type_get_meta) + if (requestType == GoogleCloudStorage::cs_download || + requestType == GoogleCloudStorage::cs_download_ota || + requestType == GoogleCloudStorage::cs_list || + requestType == GoogleCloudStorage::cs_get_meta) { - method = async_request_handler_t::http_get; - if (requestType == GoogleCloudStorage::google_cloud_storage_request_type_download || - requestType == GoogleCloudStorage::google_cloud_storage_request_type_download_ota) + method = reqns::http_get; + if (requestType == GoogleCloudStorage::cs_download || + requestType == GoogleCloudStorage::cs_download_ota) options.extras += "?alt=media"; } - else if (requestType == GoogleCloudStorage::google_cloud_storage_request_type_uploads) + else if (requestType == GoogleCloudStorage::cs_uploads) { URLUtil uut; options.extras += "?name="; @@ -482,7 +488,7 @@ class CloudStorage options.extras += "&uploadType="; // resumable upload is only for file bigger than 256k - if (uploadOptions && uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable && sz >= 256 * 1024) + if (UploadOptions && UploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable && sz >= 256 * 1024) options.extras += "resumable"; else { @@ -490,8 +496,8 @@ class CloudStorage sut.clear(options.payload); } } - else if (requestType == GoogleCloudStorage::google_cloud_storage_request_type_delete) - method = async_request_handler_t::http_delete; + else if (requestType == GoogleCloudStorage::cs_delete) + method = reqns::http_delete; if (baseOptions && strlen(baseOptions->c_str())) { @@ -503,21 +509,21 @@ class CloudStorage options.extras += options.extras.length() ? '&' : '?'; options.extras += listOptions->c_str(); } - else if (uploadOptions && strlen(uploadOptions->insertOptions.c_str())) + else if (UploadOptions && strlen(UploadOptions->insertOptions.c_str())) { options.extras += options.extras.length() ? '&' : '?'; - options.extras += uploadOptions->insertOptions.c_str(); + options.extras += UploadOptions->insertOptions.c_str(); } - GoogleCloudStorage::async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, requestType == GoogleCloudStorage::google_cloud_storage_request_type_download_ota, false), &options, file, result, cb, uid); + GoogleCloudStorage::req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, requestType == GoogleCloudStorage::cs_download_ota, false), &options, file, result, cb, uid); - if (uploadOptions && uploadOptions->mime.length() && requestType == GoogleCloudStorage::google_cloud_storage_request_type_uploads) - aReq.mime = uploadOptions->mime; + if (UploadOptions && UploadOptions->mime.length() && requestType == GoogleCloudStorage::cs_uploads) + aReq.mime = UploadOptions->mime; asyncRequest(aReq); } - void asyncRequest(GoogleCloudStorage::async_request_data_t &request, int beta = 0) + void asyncRequest(GoogleCloudStorage::req_data &request, int beta = 0) { app_token_t *atoken = appToken(); @@ -527,14 +533,14 @@ class CloudStorage request.opt.app_token = atoken; String extras; - if (request.method == async_request_handler_t::http_post || request.method == async_request_handler_t::http_put) + if (request.method == reqns::http_post || request.method == reqns::http_put) request.path += "/upload"; request.path += "/storage/v1/b/"; request.path += request.options->parent.getBucketId(); request.path += "/o"; - if ((request.method == async_request_handler_t::http_get || request.method == async_request_handler_t::http_delete) && request.options->parent.getObject().length()) + if ((request.method == reqns::http_get || request.method == reqns::http_delete) && request.options->parent.getObject().length()) { URLUtil uut; request.path += "/"; @@ -545,7 +551,7 @@ class CloudStorage url(FPSTR("storage.googleapis.com")); - async_data_item_t *sData = request.aClient->createSlot(request.opt); + async_data *sData = request.aClient->createSlot(request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -574,10 +580,10 @@ class CloudStorage { if (request.options->extras.indexOf("uploadType=resumable") > -1) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; - sData->request.val[req_hndlr_ns::header] += FPSTR("X-Upload-Content-Type: "); - sData->request.val[req_hndlr_ns::header] += request.mime; - sData->request.val[req_hndlr_ns::header] += "\r\n"; + sData->request.val[reqns::payload] = request.options->payload; + sData->request.val[reqns::header] += FPSTR("X-Upload-Content-Type: "); + sData->request.val[reqns::header] += request.mime; + sData->request.val[reqns::header] += "\r\n"; request.aClient->setFileContentLength(sData, 0, "X-Upload-Content-Length"); request.aClient->setContentType(sData, "application/json; charset=UTF-8"); request.aClient->setContentLength(sData, request.options->payload.length()); @@ -604,7 +610,7 @@ class CloudStorage } else if (request.options->payload.length()) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; + sData->request.val[reqns::payload] = request.options->payload; request.aClient->setContentLength(sData, request.options->payload.length()); } @@ -620,7 +626,7 @@ class CloudStorage request.aClient->handleRemove(); } - void setClientError(GoogleCloudStorage::async_request_data_t &request, int code) + void setClientError(GoogleCloudStorage::req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -639,21 +645,21 @@ class CloudStorage } } - void addParams(const GoogleCloudStorage::async_request_data_t &request, String &extras) + void addParams(const GoogleCloudStorage::req_data &request, String &extras) { extras += request.options->extras; extras.replace(" ", "%20"); extras.replace(",", "%2C"); } - void setFileStatus(async_data_item_t *sData, const GoogleCloudStorage::async_request_data_t &request) + void setFileStatus(async_data *sData, const GoogleCloudStorage::req_data &request) { if ((request.file && (request.file->filename.length() || request.file->data_size)) || request.opt.ota) { - sData->download = request.method == async_request_handler_t::http_get; - sData->upload = request.method == async_request_handler_t::http_post || - request.method == async_request_handler_t::http_put || - request.method == async_request_handler_t::http_patch; + sData->download = request.method == reqns::http_get; + sData->upload = request.method == reqns::http_post || + request.method == reqns::http_put || + request.method == reqns::http_patch; } } }; diff --git a/src/cloud_storage/DataOptions.h b/src/cloud_storage/DataOptions.h index 10206a72..1b2fa850 100644 --- a/src/cloud_storage/DataOptions.h +++ b/src/cloud_storage/DataOptions.h @@ -38,20 +38,20 @@ namespace GoogleCloudStorage { enum google_cloud_storage_request_type { - google_cloud_storage_request_type_undefined, - google_cloud_storage_request_type_uploads, - google_cloud_storage_request_type_upload_simple, - google_cloud_storage_request_type_upload_multipart, - google_cloud_storage_request_type_upload_resumable_init, - google_cloud_storage_request_type_upload_resumable_run, - google_cloud_storage_request_type_download, - google_cloud_storage_request_type_patch, - google_cloud_storage_request_type_get_meta, - google_cloud_storage_request_type_set_meta, - google_cloud_storage_request_type_update_meta, - google_cloud_storage_request_type_delete, - google_cloud_storage_request_type_list, - google_cloud_storage_request_type_download_ota + cs_undefined, + cs_uploads, + cs_upload_simple, + cs_upload_multipart, + cs_upload_resumable_init, + cs_upload_resumable_run, + cs_download, + cs_patch, + cs_get_meta, + cs_set_meta, + cs_update_meta, + cs_delete, + cs_list, + cs_download_ota }; enum upload_type @@ -391,8 +391,8 @@ namespace GoogleCloudStorage public: String payload, extras; - GoogleCloudStorage::Parent parent; - google_cloud_storage_request_type requestType = google_cloud_storage_request_type_undefined; + Parent parent; + google_cloud_storage_request_type requestType = cs_undefined; unsigned long requestTime = 0; void copy(const DataOptions &rhs) { @@ -403,29 +403,27 @@ namespace GoogleCloudStorage private: }; - struct uploadOptions + struct UploadOptions { String mime; - GoogleCloudStorage::upload_type uploadType; - GoogleCloudStorage::InsertOptions insertOptions; - GoogleCloudStorage::InsertProperties insertProps; + upload_type uploadType; + InsertOptions insertOptions; + InsertProperties insertProps; }; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; - String path; - String uid; - String mime; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + String path, uid, mime; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; DataOptions *options = nullptr; file_config_data *file = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; diff --git a/src/core/AppBase.h b/src/core/AppBase.h index c9ad3b18..6e9460ef 100644 --- a/src/core/AppBase.h +++ b/src/core/AppBase.h @@ -1,5 +1,5 @@ /** - * Created June 25, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -49,9 +49,9 @@ class AppBase app_event_t *getAppEvent(AsyncClientClass *aClient) { return &aClient->app_event; } - void stopAsync(AsyncClientClass *aClient, async_data_item_t *sData) { aClient->stop(sData); } + void stopAsync(AsyncClientClass *aClient, async_data *sData) { aClient->stop(sData); } - async_data_item_t *createSlotBase(AsyncClientClass *aClient, slot_options_t &soption) + async_data *createSlotBase(AsyncClientClass *aClient, slot_options_t &soption) { if (aClient) return aClient->createSlot(soption); @@ -60,13 +60,13 @@ class AppBase AsyncResult *getResultBase(AsyncClientClass *aClient) { return aClient->getResult(); } - void newRequestBase(AsyncClientClass *aClient, async_data_item_t *sData, const String &url, const String &path, const String &extras, async_request_handler_t::http_request_method method, const slot_options_t &options, const String &uid) { aClient->newRequest(sData, url, path, extras, method, options, uid); } + void newRequestBase(AsyncClientClass *aClient, async_data *sData, const String &url, const String &path, const String &extras, reqns::http_request_method method, const slot_options_t &options, const String &uid) { aClient->newRequest(sData, url, path, extras, method, options, uid); } void setAuthTsBase(AsyncClientClass *aClient, uint32_t ts) { aClient->auth_ts = ts; } void addRemoveClientVecBase(AsyncClientClass *aClient, uint32_t cvec_addr, bool add) { aClient->addRemoveClientVec(cvec_addr, add); } - void setContentLengthBase(AsyncClientClass *aClient, async_data_item_t *sData, size_t len) { aClient->setContentLength(sData, len); } + void setContentLengthBase(AsyncClientClass *aClient, async_data *sData, size_t len) { aClient->setContentLength(sData, len); } void handleRemoveBase(AsyncClientClass *aClient) { aClient->handleRemove(); } diff --git a/src/core/AsyncClient/AsyncClient.h b/src/core/AsyncClient/AsyncClient.h index 4bb4ad01..5a30d61a 100644 --- a/src/core/AsyncClient/AsyncClient.h +++ b/src/core/AsyncClient/AsyncClient.h @@ -1,5 +1,5 @@ /** - * Created January 17, 2025 + * Created January 20, 2025 * * For MCU build target (CORE_ARDUINO_XXXX), see Options.h. * @@ -51,55 +51,47 @@ using namespace firebase; enum async_state { - async_state_undefined, - async_state_send_header, - async_state_send_payload, - async_state_read_response, - async_state_complete + astate_undefined, + astate_send_header, + astate_send_payload, + astate_read_response, + astate_complete }; enum function_return_type { - function_return_type_undefined = -2, - function_return_type_failure = -1, - function_return_type_continue = 0, - function_return_type_complete = 1, - function_return_type_retry = 2 + ret_undefined = -2, + ret_failure = -1, + ret_continue = 0, + ret_complete = 1, + ret_retry = 2 }; -struct async_data_item_t +struct async_data { friend class FirebaseApp; public: struct async_error_t { - async_state state = async_state_undefined; + async_state state = astate_undefined; int code = 0; }; - async_state state = async_state_undefined; - function_return_type return_type = function_return_type_undefined; - async_request_handler_t request; - async_response_handler_t response; + async_state state = astate_undefined; + function_return_type return_type = ret_undefined; + req_handler request; + res_handler response; async_error_t error; - bool to_remove = false; - bool auth_used = false; - bool complete = false; - bool async = false; - bool sse = false; - bool path_not_existed = false; - bool download = false; - bool upload_progress_enabled = false; - bool upload = false; - uint32_t auth_ts = 0; - uint32_t addr = 0; + bool to_remove = false, auth_used = false, complete = false, async = false, sse = false, path_not_existed = false; + bool download = false, upload_progress_enabled = false, upload = false; + uint32_t auth_ts = 0, addr = 0, ref_result_addr = 0; AsyncResult aResult; AsyncResult *refResult = nullptr; - uint32_t ref_result_addr = 0; AsyncResultCallback cb = NULL; Timer err_timer; - async_data_item_t() + + async_data() { addr = reinterpret_cast(this); err_timer.feed(0); @@ -120,12 +112,12 @@ struct async_data_item_t void reset() { - state = async_state_undefined; - return_type = function_return_type_undefined; + state = astate_undefined; + return_type = ret_undefined; request.clear(); response.clear(); error.code = 0; - error.state = async_state_undefined; + error.state = astate_undefined; to_remove = false; auth_used = false; complete = false; @@ -140,13 +132,7 @@ struct async_data_item_t struct slot_options_t { public: - bool auth_used = false; - bool sse = false; - bool async = false; - bool sv = false; - bool ota = false; - bool no_etag = false; - bool auth_param = false; + bool auth_used = false, sse = false, async = false, sv = false, ota = false, no_etag = false, auth_param = false; app_token_t *app_token = nullptr; slot_options_t() {} explicit slot_options_t(bool auth_used, bool sse, bool async, bool sv, bool ota, bool no_etag, bool auth_param = false) @@ -182,10 +168,8 @@ class AsyncClientClass : public ResultBase, RTDBResultBase AsyncResult *refResult = nullptr; AsyncResult aResult; int netErrState = 0; - uint32_t auth_ts = 0; - uint32_t cvec_addr = 0; - uint32_t result_addr = 0; - uint32_t sync_send_timeout_sec = 0, sync_read_timeout_sec = 0, session_timeout_sec = 0; + uint32_t auth_ts = 0, cvec_addr = 0, result_addr = 0, sync_send_timeout_sec = 0, sync_read_timeout_sec = 0, session_timeout_sec = 0; + uint32_t addr = 0; Timer session_timer; Client *client = nullptr; bool client_changed = false, network_changed = false; @@ -194,21 +178,18 @@ class AsyncClientClass : public ResultBase, RTDBResultBase #else void *async_tcp_config = nullptr; #endif - async_request_handler_t::tcp_client_type client_type = async_request_handler_t::tcp_client_type_sync; - bool sse = false; + reqns::tcp_client_type client_type = reqns::tcpc_sync; + bool sse = false, connected = false; String host; uint16_t port; - bool connected = false; std::vector sVec; Memory mem; Base64Util b64ut; OTAUtil otaut; network_config_data net; - uint32_t addr = 0; - bool inProcess = false; - bool inStopAsync = false; + bool inProcess = false, inStopAsync = false; - void closeFile(async_data_item_t *sData) + void closeFile(async_data *sData) { #if defined(ENABLE_FS) if (sData->request.file_data.file && sData->request.file_data.file_status == file_config_data::file_status_opened) @@ -222,7 +203,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase #endif } - bool openFile(async_data_item_t *sData, file_operating_mode mode) + bool openFile(async_data *sData, file_operating_mode mode) { #if defined(ENABLE_FS) sData->request.file_data.cb(sData->request.file_data.file, sData->request.file_data.filename.c_str(), mode); @@ -235,10 +216,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return true; } - void newCon(async_data_item_t *sData, const char *host, uint16_t port) + void newCon(async_data *sData, const char *host, uint16_t port) { - if ((!sData->sse && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC && session_timer.remaining() == 0) || (sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == async_state_undefined) || + if ((!sData->sse && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC && session_timer.remaining() == 0) || (sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == astate_undefined) || strcmp(this->host.c_str(), host) != 0 || this->port != port) { stop(sData); @@ -253,33 +234,33 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - function_return_type networkConnect(async_data_item_t *sData) + function_return_type networkConnect(async_data *sData) { function_return_type ret = netConnect(sData); - if (!sData || ret == function_return_type_failure) + if (!sData || ret == ret_failure) { // In case TCP (network) disconnected error. - setAsyncError(sData ? sData : nullptr, sData ? sData->state : async_state_undefined, FIREBASE_ERROR_TCP_DISCONNECTED, sData ? !sData->sse : true, false); - return function_return_type_failure; + setAsyncError(sData ? sData : nullptr, sData ? sData->state : astate_undefined, FIREBASE_ERROR_TCP_DISCONNECTED, sData ? !sData->sse : true, false); + return ret_failure; } return ret; } - function_return_type sendHeader(async_data_item_t *sData, const char *data) + function_return_type sendHeader(async_data *sData, const char *data) { - return send(sData, reinterpret_cast(data), data ? strlen(data) : 0, data ? strlen(data) : 0, async_state_send_header); + return send(sData, reinterpret_cast(data), data ? strlen(data) : 0, data ? strlen(data) : 0, astate_send_header); } - function_return_type sendHeader(async_data_item_t *sData, const uint8_t *data, size_t len) + function_return_type sendHeader(async_data *sData, const uint8_t *data, size_t len) { - return send(sData, data, len, len, async_state_send_header); + return send(sData, data, len, len, astate_send_header); } - function_return_type sendBuff(async_data_item_t *sData, async_state state = async_state_send_payload) + function_return_type sendBuff(async_data *sData, async_state state = astate_send_payload) { - function_return_type ret = function_return_type_continue; + function_return_type ret = ret_continue; size_t totalLen = sData->request.file_data.file_size; bool fileopen = sData->request.payloadIndex == 0; @@ -298,15 +279,15 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { // In case file open error. setAsyncError(sData, state, FIREBASE_ERROR_OPEN_FILE, !sData->sse, true); - return function_return_type_failure; + return ret_failure; } } } #endif if (sData->request.base64) { - ret = send(sData, reinterpret_cast("\""), 1, totalLen, async_state_send_payload); - if (ret != function_return_type_continue) + ret = send(sData, reinterpret_cast("\""), 1, totalLen, astate_send_payload); + if (ret != ret_continue) return ret; } } @@ -346,7 +327,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { // In case file read error. setAsyncError(sData, state, FIREBASE_ERROR_FILE_READ, !sData->sse, true); - ret = function_return_type_failure; + ret = ret_failure; goto exit; } } @@ -381,7 +362,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { // In case file read error. setAsyncError(sData, state, FIREBASE_ERROR_FILE_READ, !sData->sse, true); - ret = function_return_type_failure; + ret = ret_failure; goto exit; } } @@ -394,10 +375,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase sData->request.file_data.data_pos += toSend; } - ret = send(sData, buf, toSend, totalLen, async_state_send_payload); + ret = send(sData, buf, toSend, totalLen, astate_send_payload); } else if (sData->request.base64) - ret = send(sData, reinterpret_cast("\""), 1, totalLen, async_state_send_payload); + ret = send(sData, reinterpret_cast("\""), 1, totalLen, astate_send_payload); #if defined(ENABLE_FS) exit: @@ -409,12 +390,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return ret; } - function_return_type send(async_data_item_t *sData, const char *data, async_state state = async_state_send_payload) + function_return_type send(async_data *sData, const char *data, async_state state = astate_send_payload) { return send(sData, reinterpret_cast(data), data ? strlen(data) : 0, data ? strlen(data) : 0, state); } - function_return_type send(async_data_item_t *sData, const uint8_t *data, size_t len, size_t size, async_state state = async_state_send_payload) + function_return_type send(async_data *sData, const uint8_t *data, size_t len, size_t size, async_state state = astate_send_payload) { sData->state = state; @@ -447,8 +428,8 @@ class AsyncClientClass : public ResultBase, RTDBResultBase int ret = sData->request.file_data.resumable.isComplete(size, sData->request.payloadIndex); if (ret == 2) { - sData->state = async_state_read_response; - sData->return_type = function_return_type_complete; + sData->state = astate_read_response; + sData->return_type = ret_complete; sData->request.dataIndex = 0; sData->request.payloadIndex = 0; sData->response.clear(); @@ -456,48 +437,48 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } else if (ret == 1) { - sData->return_type = function_return_type_continue; + sData->return_type = ret_continue; return sData->return_type; } } else if (sData->request.payloadIndex < size) { - sData->return_type = function_return_type_continue; + sData->return_type = ret_continue; return sData->return_type; } #else if (sData->request.payloadIndex < size) { - sData->return_type = function_return_type_continue; + sData->return_type = ret_continue; return sData->return_type; } #endif } } - sData->return_type = sData->request.payloadIndex == size && size > 0 ? function_return_type_complete : function_return_type_failure; + sData->return_type = sData->request.payloadIndex == size && size > 0 ? ret_complete : ret_failure; // In case TCP write error. - if (sData->return_type == function_return_type_failure) + if (sData->return_type == ret_failure) setAsyncError(sData, state, FIREBASE_ERROR_TCP_SEND, !sData->sse, false); sData->request.payloadIndex = 0; sData->request.dataIndex = 0; sData->request.file_data.data_pos = 0; - if (sData->return_type == function_return_type_complete) + if (sData->return_type == ret_complete) { - if (state == async_state_send_header) + if (state == astate_send_header) { // No payload when Content-Length is 0, or not set. - if (sData->request.val[req_hndlr_ns::header].indexOf("Content-Length: 0\r\n") > -1 || sData->request.val[req_hndlr_ns::header].indexOf("Content-Length") == -1) - sData->state = async_state_read_response; + if (sData->request.val[reqns::header].indexOf("Content-Length: 0\r\n") > -1 || sData->request.val[reqns::header].indexOf("Content-Length") == -1) + sData->state = astate_read_response; else - sData->state = async_state_send_payload; + sData->state = astate_send_payload; } - else if (state == async_state_send_payload) - sData->state = async_state_read_response; + else if (state == astate_send_payload) + sData->state = astate_read_response; #if defined(ENABLE_CLOUD_STORAGE) if (sData->upload) @@ -507,35 +488,35 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } #endif - if (sData->state != async_state_read_response) + if (sData->state != astate_read_response) sData->response.clear(); } return sData->return_type; } - function_return_type send(async_data_item_t *sData) + function_return_type send(async_data *sData) { function_return_type ret = networkConnect(sData); - if (ret != function_return_type_complete) + if (ret != ret_complete) return ret; - ret = function_return_type_continue; + ret = ret_continue; - if (sData->state == async_state_undefined || sData->state == async_state_send_header) + if (sData->state == astate_undefined || sData->state == astate_send_header) { newCon(sData, getHost(sData, true).c_str(), sData->request.port); - if ((client_type == async_request_handler_t::tcp_client_type_sync && !isConnected()) || client_type == async_request_handler_t::tcp_client_type_async) + if ((client_type == reqns::tcpc_sync && !isConnected()) || client_type == reqns::tcpc_async) { ret = connect(sData, getHost(sData, true).c_str(), sData->request.port); // allow non-blocking async tcp connection - if (ret == function_return_type_continue) + if (ret == ret_continue) return ret; - if (ret != function_return_type_complete) + if (ret != ret_complete) return connErrorHandler(sData, sData->state); sse = sData->sse; @@ -551,28 +532,28 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { // In case missing auth token error. setAsyncError(sData, sData->state, FIREBASE_ERROR_UNAUTHENTICATE, !sData->sse, false); - return function_return_type_failure; + return ret_failure; } - header = sData->request.val[req_hndlr_ns::header]; + header = sData->request.val[reqns::header]; header.replace(FIREBASE_AUTH_PLACEHOLDER, sData->request.app_token->val[app_tk_ns::token]); ret = sendHeader(sData, header.c_str()); sut.clear(header); return ret; } - return sendHeader(sData, sData->request.val[req_hndlr_ns::header].c_str()); + return sendHeader(sData, sData->request.val[reqns::header].c_str()); } - else if (sData->state == async_state_send_payload) + else if (sData->state == astate_send_payload) { if (sData->upload) sData->upload_progress_enabled = true; - if (sData->request.method == async_request_handler_t::http_get || sData->request.method == async_request_handler_t::http_delete) - sData->state = async_state_read_response; + if (sData->request.method == reqns::http_get || sData->request.method == reqns::http_delete) + sData->state = astate_read_response; else { - if (sData->request.val[req_hndlr_ns::payload].length()) - ret = send(sData, sData->request.val[req_hndlr_ns::payload].c_str()); + if (sData->request.val[reqns::payload].length()) + ret = send(sData, sData->request.val[reqns::payload].c_str()); else if (sData->upload) { if (sData->request.data && sData->request.dataLen) @@ -585,11 +566,11 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return ret; } - function_return_type receive(async_data_item_t *sData) + function_return_type receive(async_data *sData) { function_return_type ret = networkConnect(sData); - if (ret != function_return_type_complete) + if (ret != ret_complete) return ret; // HTTP error is allowed in case non-auth task to get its response. @@ -597,11 +578,11 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { // In case HTTP or TCP read error. setAsyncError(sData, sData->state, sData->response.httpCode > 0 ? sData->response.httpCode : FIREBASE_ERROR_TCP_RECEIVE_TIMEOUT, !sData->sse, false); - return function_return_type_failure; + return ret_failure; } if (sData->response.httpCode == 0) - return function_return_type_continue; + return ret_continue; #if defined(ENABLE_CLOUD_STORAGE) @@ -610,20 +591,20 @@ class AsyncClientClass : public ResultBase, RTDBResultBase String ext; String _host = getHost(sData, false, &ext); - if (connect(sData, _host.c_str(), sData->request.port) > function_return_type_failure) + if (connect(sData, _host.c_str(), sData->request.port) > ret_failure) { - sut.clear(sData->request.val[req_hndlr_ns::payload]); - sData->request.file_data.resumable.getHeader(sData->request.val[req_hndlr_ns::header], _host, ext); - sData->state = async_state_send_header; + sut.clear(sData->request.val[reqns::payload]); + sData->request.file_data.resumable.getHeader(sData->request.val[reqns::header], _host, ext); + sData->state = astate_send_header; sData->request.file_data.resumable.setHeaderState(); - return function_return_type_continue; + return ret_continue; } return connErrorHandler(sData, sData->state); } #else - if (sData->response.val[res_hndlr_ns::location].length() && !sData->response.flags.header_remaining && !sData->response.flags.payload_remaining) + if (sData->response.val[resns::location].length() && !sData->response.flags.header_remaining && !sData->response.flags.payload_remaining) { String ext; String _host = getHost(sData, false, &ext); @@ -632,13 +613,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase client->stop(); this->connected = false; } - if (connect(sData, _host.c_str(), sData->request.port) > function_return_type_failure) + if (connect(sData, _host.c_str(), sData->request.port) > ret_failure) { URLUtil uut; - uut.relocate(sData->request.val[req_hndlr_ns::header], _host, ext); - sut.clear(sData->request.val[req_hndlr_ns::payload]); - sData->state = async_state_send_header; - return function_return_type_continue; + uut.relocate(sData->request.val[reqns::header], _host, ext); + sut.clear(sData->request.val[reqns::payload]); + sData->state = astate_send_header; + return ret_continue; } return connErrorHandler(sData, sData->state); @@ -648,21 +629,21 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!sData->sse && sData->response.httpCode > 0 && !sData->response.flags.header_remaining && !sData->response.flags.payload_remaining) { - sData->state = async_state_undefined; - return function_return_type_complete; + sData->state = astate_undefined; + return ret_complete; } - return function_return_type_continue; + return ret_continue; } - function_return_type connErrorHandler(async_data_item_t *sData, async_state state) + function_return_type connErrorHandler(async_data *sData, async_state state) { // In case TCP connection error. setAsyncError(sData, state, FIREBASE_ERROR_TCP_CONNECTION, !sData->sse, false); - return function_return_type_failure; + return ret_failure; } - void setAsyncError(async_data_item_t *sData, async_state state, int code, bool toRemove, bool toCloseFile) + void setAsyncError(async_data *sData, async_state state, int code, bool toRemove, bool toCloseFile) { if (!sData) return; @@ -679,16 +660,16 @@ class AsyncClientClass : public ResultBase, RTDBResultBase setLastError(sData); } - async_data_item_t *getData(uint8_t slot) + async_data *getData(uint8_t slot) { if (slot < sVec.size()) - return reinterpret_cast(sVec[slot]); + return reinterpret_cast(sVec[slot]); return nullptr; } - async_data_item_t *addSlot(int index = -1) + async_data *addSlot(int index = -1) { - async_data_item_t *sData = new async_data_item_t(); + async_data *sData = new async_data(); sData->aResult.app_debug = &app_debug; sData->aResult.app_event = &app_event; @@ -701,7 +682,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return sData; } - AsyncResult *getResult(async_data_item_t *sData) + AsyncResult *getResult(async_data *sData) { List vec; return vec.existed(rVec, sData->ref_result_addr) ? sData->refResult : nullptr; @@ -713,7 +694,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return vec.existed(rVec, result_addr) ? refResult : &aResult; } - void returnResult(async_data_item_t *sData, bool setData) + void returnResult(async_data *sData, bool setData) { bool error_notify_timeout = false; @@ -769,7 +750,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - void setLastError(async_data_item_t *sData) + void setLastError(async_data *sData) { if (sData && sData->error.code < 0) { @@ -786,8 +767,8 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } else if (sData && sData->response.httpCode > 0 && sData->response.httpCode >= FIREBASE_ERROR_HTTP_CODE_BAD_REQUEST) { - sData->aResult.lastError.setResponseError(sData->response.val[res_hndlr_ns::payload], sData->response.httpCode); - lastErr.setResponseError(sData->response.val[res_hndlr_ns::payload], sData->response.httpCode); + sData->aResult.lastError.setResponseError(sData->response.val[resns::payload], sData->response.httpCode); + lastErr.setResponseError(sData->response.val[resns::payload], sData->response.httpCode); clearAppData(sData->aResult.app_data); // Required for sync task. @@ -799,7 +780,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - int readLine(async_data_item_t *sData, String &buf) + int readLine(async_data *sData, String &buf) { int p = 0; @@ -839,7 +820,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase void clear(String &str) { sut.clear(str); } - bool readResponse(async_data_item_t *sData) + bool readResponse(async_data *sData) { if (!client || !sData) return false; @@ -858,7 +839,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!readPayload(sData)) return false; - String *payload = &sData->response.val[res_hndlr_ns::payload]; + String *payload = &sData->response.val[resns::payload]; if (sData->response.flags.sse || !sData->response.flags.payload_remaining) { @@ -872,7 +853,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase #if defined(ENABLE_DATABASE) - if (sData->request.method == async_request_handler_t::http_post) + if (sData->request.method == reqns::http_post) parseNodeName(&sData->aResult.rtdbResult); // Data available from sse event @@ -936,16 +917,16 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return 0; } - bool readStatusLine(async_data_item_t *sData) + bool readStatusLine(async_data *sData) { if (sData->response.httpCode > 0) return false; - sData->response.val[res_hndlr_ns::header].reserve(1024); + sData->response.val[resns::header].reserve(1024); // the first chunk (line) can be http response status or already connected stream payload - readLine(sData, sData->response.val[res_hndlr_ns::header]); - int status = getStatusCode(sData->response.val[res_hndlr_ns::header]); + readLine(sData, sData->response.val[resns::header]); + int status = getStatusCode(sData->response.val[resns::header]); if (status > 0) { // http response status @@ -955,47 +936,47 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return true; } - void readHeader(async_data_item_t *sData) + void readHeader(async_data *sData) { if (sData->response.flags.header_remaining) { - int read = readLine(sData, sData->response.val[res_hndlr_ns::header]); - if ((read == 1 && sData->response.val[res_hndlr_ns::header][sData->response.val[res_hndlr_ns::header].length() - 1] == '\n') || - (read == 2 && sData->response.val[res_hndlr_ns::header][sData->response.val[res_hndlr_ns::header].length() - 2] == '\r' && sData->response.val[res_hndlr_ns::header][sData->response.val[res_hndlr_ns::header].length() - 1] == '\n')) + int read = readLine(sData, sData->response.val[resns::header]); + if ((read == 1 && sData->response.val[resns::header][sData->response.val[resns::header].length() - 1] == '\n') || + (read == 2 && sData->response.val[resns::header][sData->response.val[resns::header].length() - 2] == '\r' && sData->response.val[resns::header][sData->response.val[resns::header].length() - 1] == '\n')) { sData->response.flags.http_response = true; - clear(sData->response.val[res_hndlr_ns::etag]); + clear(sData->response.val[resns::etag]); String temp[5]; #if defined(ENABLE_CLOUD_STORAGE) if (sData->upload) - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], sData->request.file_data.resumable.getLocationRef(), "Location"); + parseRespHeader(sData, sData->response.val[resns::header], sData->request.file_data.resumable.getLocationRef(), "Location"); #else - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], sData->response.val[res_hndlr_ns::location], "Location"); + parseRespHeader(sData, sData->response.val[resns::header], sData->response.val[resns::location], "Location"); #endif - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], sData->response.val[res_hndlr_ns::etag], "ETag"); - resETag = sData->response.val[res_hndlr_ns::etag]; - sData->aResult.val[ares_ns::res_etag] = sData->response.val[res_hndlr_ns::etag]; - sData->aResult.val[ares_ns::data_path] = sData->request.val[req_hndlr_ns::path]; + parseRespHeader(sData, sData->response.val[resns::header], sData->response.val[resns::etag], "ETag"); + resETag = sData->response.val[resns::etag]; + sData->aResult.val[ares_ns::res_etag] = sData->response.val[resns::etag]; + sData->aResult.val[ares_ns::data_path] = sData->request.val[reqns::path]; #if defined(ENABLE_DATABASE) - setNullETagOption(&sData->aResult.rtdbResult, sData->response.val[res_hndlr_ns::etag].indexOf("null_etag") > -1); + setNullETagOption(&sData->aResult.rtdbResult, sData->response.val[resns::etag].indexOf("null_etag") > -1); #endif - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], temp[0], "Content-Length"); + parseRespHeader(sData, sData->response.val[resns::header], temp[0], "Content-Length"); sData->response.payloadLen = atoi(temp[0].c_str()); - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], temp[1], "Connection"); + parseRespHeader(sData, sData->response.val[resns::header], temp[1], "Connection"); sData->response.flags.keep_alive = temp[1].length() && temp[1].indexOf("keep-alive") > -1; - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], temp[2], "Transfer-Encoding"); + parseRespHeader(sData, sData->response.val[resns::header], temp[2], "Transfer-Encoding"); sData->response.flags.chunks = temp[2].length() && temp[2].indexOf("chunked") > -1; - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], temp[3], "Content-Type"); + parseRespHeader(sData, sData->response.val[resns::header], temp[3], "Content-Type"); sData->response.flags.sse = temp[3].length() && temp[3].indexOf("text/event-stream") > -1; if (sData->upload) - parseRespHeader(sData, sData->response.val[res_hndlr_ns::header], temp[4], "Range"); + parseRespHeader(sData, sData->response.val[resns::header], temp[4], "Range"); clear(sData); @@ -1016,13 +997,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!sData->sse && (sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_OK || sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_PERMANENT_REDIRECT || sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_NO_CONTENT) && !sData->response.flags.chunks && sData->response.payloadLen == 0) sData->response.flags.payload_remaining = false; - if (sData->request.method == async_request_handler_t::http_delete && sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_NO_CONTENT) + if (sData->request.method == reqns::http_delete && sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_NO_CONTENT) setDebugBase(app_debug, FPSTR("Delete operation complete")); } } } - void parseRespHeader(async_data_item_t *sData, const String &src, String &out, const char *header) + void parseRespHeader(async_data *sData, const String &src, String &out, const char *header) { if (sData->response.httpCode > 0) { @@ -1041,7 +1022,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - int getChunkSize(async_data_item_t *sData, Client *client, String &line) + int getChunkSize(async_data *sData, Client *client, String &line) { if (line.length() == 0) readLine(sData, line); @@ -1056,7 +1037,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } // Returns -1 when complete - int decodeChunks(async_data_item_t *sData, Client *client, String *out) + int decodeChunks(async_data *sData, Client *client, String *out) { if (!client || !sData || !out) return 0; @@ -1071,9 +1052,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // when available() is less than the remaining amount of data in the chunk // read chunk-size, chunk-extension (if any) and CRLF - if (sData->response.chunkInfo.phase == async_response_handler_t::READ_CHUNK_SIZE) + if (sData->response.chunkInfo.phase == res_handler::READ_CHUNK_SIZE) { - sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_DATA; + sData->response.chunkInfo.phase = res_handler::READ_CHUNK_DATA; sData->response.chunkInfo.chunkSize = -1; sData->response.chunkInfo.dataLen = 0; res = getChunkSize(sData, client, line); @@ -1113,12 +1094,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // check if we're done reading this chunk if (sData->response.chunkInfo.dataLen == sData->response.chunkInfo.chunkSize) - sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_SIZE; + sData->response.chunkInfo.phase = res_handler::READ_CHUNK_SIZE; } // if we read 0 bytes, read next chunk size else { - sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_SIZE; + sData->response.chunkInfo.phase = res_handler::READ_CHUNK_SIZE; } } else @@ -1137,7 +1118,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return res; } - bool readPayload(async_data_item_t *sData) + bool readPayload(async_data *sData) { uint8_t *buf = nullptr; @@ -1157,7 +1138,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (temp.length()) { reserveString(sData); - sData->response.val[res_hndlr_ns::payload] += temp; + sData->response.val[resns::payload] += temp; } if (res == -1) @@ -1181,7 +1162,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (sData->request.ota_error != 0) { // In case OTA error. - setAsyncError(sData, async_state_read_response, sData->request.ota_error, !sData->sse, false); + setAsyncError(sData, astate_read_response, sData->request.ota_error, !sData->sse, false); return false; } } @@ -1193,7 +1174,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!openFile(sData, file_mode_open_write)) { // In case file open error. - setAsyncError(sData, async_state_read_response, FIREBASE_ERROR_OPEN_FILE, !sData->sse, true); + setAsyncError(sData, astate_read_response, FIREBASE_ERROR_OPEN_FILE, !sData->sse, true); return false; } } @@ -1239,7 +1220,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (sData->request.ota_error != 0) { // In case OTA error. - setAsyncError(sData, async_state_read_response, sData->request.ota_error, !sData->sse, false); + setAsyncError(sData, astate_read_response, sData->request.ota_error, !sData->sse, false); goto exit; } @@ -1249,7 +1230,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (sData->request.ota_error != 0) { // In case OTA error. - setAsyncError(sData, async_state_read_response, sData->request.ota_error, !sData->sse, false); + setAsyncError(sData, astate_read_response, sData->request.ota_error, !sData->sse, false); goto exit; } } @@ -1261,7 +1242,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!b64ut.decodeToFile(mem, sData->request.file_data.file, reinterpret_cast(buf + ofs))) { // In case file write error. - setAsyncError(sData, async_state_read_response, FIREBASE_ERROR_FILE_WRITE, !sData->sse, true); + setAsyncError(sData, astate_read_response, FIREBASE_ERROR_FILE_WRITE, !sData->sse, true); goto exit; } } @@ -1281,7 +1262,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (sData->request.ota_error != 0) { // In case OTA error. - setAsyncError(sData, async_state_read_response, sData->request.ota_error, !sData->sse, false); + setAsyncError(sData, astate_read_response, sData->request.ota_error, !sData->sse, false); goto exit; } } @@ -1293,7 +1274,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (write < read) { // In case file write error. - setAsyncError(sData, async_state_read_response, FIREBASE_ERROR_FILE_WRITE, !sData->sse, true); + setAsyncError(sData, astate_read_response, FIREBASE_ERROR_FILE_WRITE, !sData->sse, true); goto exit; } } @@ -1318,7 +1299,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase size_t len = readLine(sData, temp); sData->response.payloadRead += len; reserveString(sData); - sData->response.val[res_hndlr_ns::payload] += temp; + sData->response.val[resns::payload] += temp; } } } @@ -1334,17 +1315,17 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // Async payload and header data collision workaround from session reusage. if (!sData->response.flags.chunks && sData->response.payloadRead > sData->response.payloadLen) { - sData->response.val[res_hndlr_ns::header] = sData->response.val[res_hndlr_ns::payload].substring(sData->response.payloadRead - sData->response.payloadLen); - sData->response.val[res_hndlr_ns::payload].remove(0, sData->response.payloadLen); - sData->return_type = function_return_type_continue; - sData->state = async_state_read_response; + sData->response.val[resns::header] = sData->response.val[resns::payload].substring(sData->response.payloadRead - sData->response.payloadLen); + sData->response.val[resns::payload].remove(0, sData->response.payloadLen); + sData->return_type = ret_continue; + sData->state = astate_read_response; sData->response.flags.header_remaining = true; } if (sData->upload) { URLUtil uut; - uut.updateDownloadURL(sData->aResult.upload_data.downloadUrl, sData->response.val[res_hndlr_ns::payload]); + uut.updateDownloadURL(sData->aResult.upload_data.downloadUrl, sData->response.val[resns::payload]); } if (sData->response.flags.chunks && sData->auth_used) @@ -1354,7 +1335,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (sData->response.httpCode >= FIREBASE_ERROR_HTTP_CODE_BAD_REQUEST) { setAsyncError(sData, sData->state, sData->response.httpCode, !sData->sse, true); - sData->return_type = function_return_type_failure; + sData->return_type = ret_failure; returnResult(sData, false); } @@ -1379,19 +1360,19 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return sData->error.code == 0; } - void reserveString(async_data_item_t *sData) + void reserveString(async_data *sData) { // String memory reservation is needed to hadle large data in external memory. #if defined(ENABLE_PSRAM) && ((defined(ESP8266) && defined(MMU_EXTERNAL_HEAP)) || (defined(ESP32) && defined(BOARD_HAS_PSRAM))) - String old = sData->response.val[res_hndlr_ns::payload]; - sData->response.val[res_hndlr_ns::payload].remove(0, sData->response.val[res_hndlr_ns::payload].length()); - sData->response.val[res_hndlr_ns::payload].reserve(sData->response.payloadRead + 1); - sData->response.val[res_hndlr_ns::payload] = old; + String old = sData->response.val[resns::payload]; + sData->response.val[resns::payload].remove(0, sData->response.val[resns::payload].length()); + sData->response.val[resns::payload].reserve(sData->response.payloadRead + 1); + sData->response.val[resns::payload] = old; #endif } // non-block memory buffer for collecting the multiple of 4 data prepared for base64 decoding - uint8_t *asyncBase64Buffer(async_data_item_t *sData, Memory &mem, int &toRead, int &read) + uint8_t *asyncBase64Buffer(async_data *sData, Memory &mem, int &toRead, int &read) { uint8_t *buf = nullptr; @@ -1421,11 +1402,11 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return buf; } - void clear(async_data_item_t *sData) + void clear(async_data *sData) { - clear(sData->response.val[res_hndlr_ns::header]); + clear(sData->response.val[resns::header]); if (!sData->auth_used) - clear(sData->response.val[res_hndlr_ns::payload]); + clear(sData->response.val[resns::payload]); sData->response.flags.header_remaining = false; sData->response.flags.payload_remaining = false; sData->response.payloadRead = 0; @@ -1433,10 +1414,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase clear(sData->response.error.string); sData->response.chunkInfo.chunkSize = 0; sData->response.chunkInfo.dataLen = 0; - sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_SIZE; + sData->response.chunkInfo.phase = res_handler::READ_CHUNK_SIZE; } - void reset(async_data_item_t *sData, bool disconnect) + void reset(async_data *sData, bool disconnect) { if (disconnect) stop(sData); @@ -1444,22 +1425,22 @@ class AsyncClientClass : public ResultBase, RTDBResultBase sData->response.httpCode = 0; sData->error.code = 0; sData->response.flags.reset(); - sData->state = async_state_undefined; - sData->return_type = function_return_type_undefined; - clear(sData->response.val[res_hndlr_ns::etag]); + sData->state = astate_undefined; + sData->return_type = ret_undefined; + clear(sData->response.val[resns::etag]); sData->aResult.download_data.reset(); sData->aResult.upload_data.reset(); clear(sData); } - function_return_type connect(async_data_item_t *sData, const char *host, uint16_t port) + function_return_type connect(async_data *sData, const char *host, uint16_t port) { sData->aResult.lastError.clearError(); lastErr.clearError(); clearAppData(sData->aResult.app_data); if ((sData->auth_used || sData->sse) && (millis() - sData->aResult.conn_ms < FIREBASE_RECONNECTION_TIMEOUT_MSEC) && sData->aResult.conn_ms > 0) - return function_return_type_continue; + return ret_continue; sData->aResult.conn_ms = millis(); resetDebug(app_debug); @@ -1467,9 +1448,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (!isConnected() && !sData->auth_used) // This info is already show in auth task setDebugBase(app_debug, FPSTR("Connecting to server...")); - if (!isConnected() && client_type == async_request_handler_t::tcp_client_type_sync) - sData->return_type = client->connect(host, port) > 0 ? function_return_type_complete : function_return_type_failure; - else if (client_type == async_request_handler_t::tcp_client_type_async) + if (!isConnected() && client_type == reqns::tcpc_sync) + sData->return_type = client->connect(host, port) > 0 ? ret_complete : ret_failure; + else if (client_type == reqns::tcpc_async) { #if defined(ENABLE_ASYNC_TCP_CLIENT) @@ -1488,14 +1469,14 @@ class AsyncClientClass : public ResultBase, RTDBResultBase async_tcp_config->tcpStatus(status); } - sData->return_type = status ? function_return_type_complete : function_return_type_continue; + sData->return_type = status ? ret_complete : ret_continue; } #endif } this->host = host; this->port = port; - this->connected = sData->return_type == function_return_type_complete; + this->connected = sData->return_type == ret_complete; if (isConnected() && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC) session_timer.feed(session_timeout_sec); @@ -1584,7 +1565,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return strcmp(buf, "0.0.0.0") != 0; } - function_return_type gprsConnect(async_data_item_t *sData) + function_return_type gprsConnect(async_data *sData) { #if defined(FIREBASE_GSM_MODEM_IS_AVAILABLE) @@ -1608,7 +1589,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase #endif setDebugBase(app_debug, FPSTR("Waiting for network...")); - return function_return_type_continue; + return ret_continue; } else if (net.gsm.conn_status == network_config_data::gsm_conn_status_waits_network) { @@ -1618,7 +1599,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase netErrState = 1; net.network_status = false; net.gsm.conn_status = network_config_data::gsm_conn_status_idle; - return function_return_type_failure; + return ret_failure; } net.gsm.conn_status = network_config_data::gsm_conn_status_waits_gprs; @@ -1635,7 +1616,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - return function_return_type_continue; + return ret_continue; } else if (net.gsm.conn_status == network_config_data::gsm_conn_status_waits_gprs) { @@ -1654,11 +1635,11 @@ class AsyncClientClass : public ResultBase, RTDBResultBase net.gsm.conn_status = network_config_data::gsm_conn_status_idle; - return net.network_status ? function_return_type_complete : function_return_type_failure; + return net.network_status ? ret_complete : ret_failure; } #endif - return function_return_type_failure; + return ret_failure; } bool gprsConnected() @@ -1679,7 +1660,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return !net.network_status; } - function_return_type ethernetConnect(async_data_item_t *sData) + function_return_type ethernetConnect(async_data *sData) { #if defined(FIREBASE_ETHERNET_MODULE_IS_AVAILABLE) && defined(ENABLE_ETHERNET_NETWORK) @@ -1743,7 +1724,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { if (FIREBASE_ETHERNET_MODULE_CLASS_IMPL.linkStatus() != LinkON && net.eth_timer.remaining() > 0) - return function_return_type_continue; + return ret_continue; net.ethernet.conn_satatus = network_config_data::ethernet_conn_status_idle; @@ -1760,12 +1741,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase setDebugBase(app_debug, FPSTR("Can't connect to network")); } - return ret ? function_return_type_complete : function_return_type_failure; + return ret ? ret_complete : ret_failure; } #endif - return function_return_type_continue; + return ret_continue; } bool ethernetConnected() @@ -1781,7 +1762,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return net.network_status; } - function_return_type netConnect(async_data_item_t *sData) + function_return_type netConnect(async_data *sData) { if (!getNetworkStatus()) { @@ -1802,7 +1783,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (net.network_data_type == firebase_network_data_generic_network) { if (generic_network_owner_addr > 0 && generic_network_owner_addr != reinterpret_cast(this)) - return function_return_type_continue; + return ret_continue; if (generic_network_owner_addr == 0) generic_network_owner_addr = reinterpret_cast(this); @@ -1817,26 +1798,26 @@ class AsyncClientClass : public ResultBase, RTDBResultBase else if (net.network_data_type == firebase_network_data_gsm_network) { if (gsm_network_owner_addr > 0 && gsm_network_owner_addr != reinterpret_cast(this)) - return function_return_type_continue; + return ret_continue; if (gsm_network_owner_addr == 0) gsm_network_owner_addr = reinterpret_cast(this); - if (gprsConnect(sData) == function_return_type_continue) - return function_return_type_continue; + if (gprsConnect(sData) == ret_continue) + return ret_continue; gsm_network_owner_addr = 0; } else if (net.network_data_type == firebase_network_data_ethernet_network) { if (ethernet_network_owner_addr > 0 && ethernet_network_owner_addr != reinterpret_cast(this)) - return function_return_type_continue; + return ret_continue; if (ethernet_network_owner_addr == 0) ethernet_network_owner_addr = reinterpret_cast(this); - if (ethernetConnect(sData) == function_return_type_continue) - return function_return_type_continue; + if (ethernetConnect(sData) == ret_continue) + return ret_continue; ethernet_network_owner_addr = 0; } @@ -1866,7 +1847,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - return getNetworkStatus() ? function_return_type_complete : function_return_type_failure; + return getNetworkStatus() ? ret_complete : ret_failure; } bool getNetworkStatus() @@ -1936,12 +1917,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return slot; } - void setContentType(async_data_item_t *sData, const String &type) + void setContentType(async_data *sData, const String &type) { sData->request.addContentTypeHeader(type.c_str()); } - void setFileContentLength(async_data_item_t *sData, int headerLen = 0, const String &customHeader = "") + void setFileContentLength(async_data *sData, int headerLen = 0, const String &customHeader = "") { size_t sz = 0; @@ -1960,10 +1941,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase sData->request.file_data.file_size = sData->request.base64 ? 2 + b64ut.getBase64Len(sz) : sz; if (customHeader.length()) { - sData->request.val[req_hndlr_ns::header] += customHeader; - sData->request.val[req_hndlr_ns::header] += ":"; - sData->request.val[req_hndlr_ns::header] += sData->request.file_data.file_size + headerLen; - sData->request.val[req_hndlr_ns::header] += "\r\n"; + sData->request.val[reqns::header] += customHeader; + sData->request.val[reqns::header] += ":"; + sData->request.val[reqns::header] += sData->request.file_data.file_size + headerLen; + sData->request.val[reqns::header] += "\r\n"; } else setContentLength(sData, sData->request.file_data.file_size); @@ -1983,7 +1964,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } #if defined(ENABLE_DATABASE) - void handleEventTimeout(async_data_item_t *sData) + void handleEventTimeout(async_data *sData) { if (sData->sse && sData->aResult.rtdbResult.eventTimeout()) { @@ -2002,13 +1983,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } #endif - bool handleSendTimeout(async_data_item_t *sData) + bool handleSendTimeout(async_data *sData) { if (sData->request.send_timer.remaining() == 0) { // In case TCP write error. setAsyncError(sData, sData->state, FIREBASE_ERROR_TCP_SEND, !sData->sse, false); - sData->return_type = function_return_type_failure; + sData->return_type = ret_failure; // This requires by WiFiSSLClient before stating a new connection in case session was reused. reset(sData, true); return true; @@ -2016,13 +1997,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return false; } - bool handleReadTimeout(async_data_item_t *sData) + bool handleReadTimeout(async_data *sData) { if (!sData->sse && sData->response.read_timer.remaining() == 0) { // In case TCP read error. setAsyncError(sData, sData->state, FIREBASE_ERROR_TCP_RECEIVE_TIMEOUT, !sData->sse, false); - sData->return_type = function_return_type_failure; + sData->return_type = ret_failure; // This requires by WiFiSSLClient before stating a new connection in case session was reused. reset(sData, true); return true; @@ -2030,9 +2011,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return false; } - void handleProcessFailure(async_data_item_t *sData) + void handleProcessFailure(async_data *sData) { - if (sData->return_type == function_return_type_failure) + if (sData->return_type == ret_failure) { if (sData->async) returnResult(sData, false); @@ -2040,12 +2021,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - String getHost(async_data_item_t *sData, bool fromReq, String *ext = nullptr) + String getHost(async_data *sData, bool fromReq, String *ext = nullptr) { #if defined(ENABLE_CLOUD_STORAGE) - String url = fromReq ? sData->request.val[req_hndlr_ns::url] : sData->request.file_data.resumable.getLocation(); + String url = fromReq ? sData->request.val[reqns::url] : sData->request.file_data.resumable.getLocation(); #else - String url = fromReq ? sData->request.val[req_hndlr_ns::url] : sData->response.val[res_hndlr_ns::location]; + String url = fromReq ? sData->request.val[reqns::url] : sData->response.val[resns::location]; #endif URLUtil uut; return uut.getHost(url, ext); @@ -2063,7 +2044,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase for (int i = size - 1; i >= 0; i--) { sys_idle(); - async_data_item_t *sData = getData(i); + async_data *sData = getData(i); if (sData && sData->async && !sData->auth_used && !sData->to_remove) { // Reset the app data to reset clear the available status when the task was canceled. @@ -2098,9 +2079,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // the unopen socket. if (this->connected) { - if (client_type == async_request_handler_t::tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) this->connected = client && client->connected(); - else if (client_type == async_request_handler_t::tcp_client_type_async) + else if (client_type == reqns::tcpc_async) { #if defined(ENABLE_ASYNC_TCP_CLIENT) if (async_tcp_config && async_tcp_config->tcpStatus) @@ -2111,12 +2092,12 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return this->connected; } - void stop(async_data_item_t *sData) + void stop(async_data *sData) { if (isConnected()) setDebugBase(app_debug, FPSTR("Terminating the server connection...")); - if (client_type == async_request_handler_t::tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) { if (client) client->stop(); @@ -2136,7 +2117,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase network_changed = false; } - async_data_item_t *createSlot(slot_options_t &options) + async_data *createSlot(slot_options_t &options) { if (!options.auth_used) sut.clear(sse_events_filter); @@ -2145,31 +2126,31 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // Only one SSE mode is allowed if (slot_index == -2) return nullptr; - async_data_item_t *sData = addSlot(slot_index); + async_data *sData = addSlot(slot_index); sData->reset(); return sData; } - void newRequest(async_data_item_t *sData, const String &url, const String &path, const String &extras, async_request_handler_t::http_request_method method, const slot_options_t &options, const String &uid) + void newRequest(async_data *sData, const String &url, const String &path, const String &extras, reqns::http_request_method method, const slot_options_t &options, const String &uid) { sData->async = options.async; - sData->request.val[req_hndlr_ns::url] = url; - sData->request.val[req_hndlr_ns::path] = path; + sData->request.val[reqns::url] = url; + sData->request.val[reqns::path] = path; sData->request.method = method; sData->sse = options.sse; - sData->request.val[req_hndlr_ns::etag] = reqEtag; + sData->request.val[reqns::etag] = reqEtag; clear(reqEtag); sData->aResult.setUID(uid); - clear(sData->request.val[req_hndlr_ns::header]); + clear(sData->request.val[reqns::header]); sData->request.addRequestHeaderFirst(method); if (path.length() == 0) - sData->request.val[req_hndlr_ns::header] += '/'; + sData->request.val[reqns::header] += '/'; else if (path[0] != '/') - sData->request.val[req_hndlr_ns::header] += '/'; - sData->request.val[req_hndlr_ns::header] += path; - sData->request.val[req_hndlr_ns::header] += extras; + sData->request.val[reqns::header] += '/'; + sData->request.val[reqns::header] += path; + sData->request.val[reqns::header] += extras; sData->request.addRequestHeaderLast(); sData->request.addHostHeader(getHost(sData, true).c_str()); @@ -2181,37 +2162,37 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (options.app_token && !options.auth_param && (options.app_token->auth_type > auth_unknown_token && options.app_token->auth_type < auth_refresh_token)) { sData->request.addAuthHeaderFirst(options.app_token->auth_type); - sData->request.val[req_hndlr_ns::header] += FIREBASE_AUTH_PLACEHOLDER; + sData->request.val[reqns::header] += FIREBASE_AUTH_PLACEHOLDER; sData->request.addNewLine(); } sData->request.addConnectionHeader(true); - if (!options.sv && !options.no_etag && method != async_request_handler_t::http_patch && extras.indexOf("orderBy") == -1) + if (!options.sv && !options.no_etag && method != reqns::http_patch && extras.indexOf("orderBy") == -1) { - sData->request.val[req_hndlr_ns::header] += FPSTR("X-Firebase-ETag: true"); + sData->request.val[reqns::header] += FPSTR("X-Firebase-ETag: true"); sData->request.addNewLine(); } - if (sData->request.val[req_hndlr_ns::etag].length() > 0 && (method == async_request_handler_t::http_put || method == async_request_handler_t::http_delete)) + if (sData->request.val[reqns::etag].length() > 0 && (method == reqns::http_put || method == reqns::http_delete)) { - sData->request.val[req_hndlr_ns::header] += FPSTR("if-match: "); - sData->request.val[req_hndlr_ns::header] += sData->request.val[req_hndlr_ns::etag]; + sData->request.val[reqns::header] += FPSTR("if-match: "); + sData->request.val[reqns::header] += sData->request.val[reqns::etag]; sData->request.addNewLine(); } if (options.sse) { - sData->request.val[req_hndlr_ns::header] += FPSTR("Accept: text/event-stream"); + sData->request.val[reqns::header] += FPSTR("Accept: text/event-stream"); sData->request.addNewLine(); } } - if (method == async_request_handler_t::http_get || method == async_request_handler_t::http_delete) + if (method == reqns::http_get || method == reqns::http_delete) sData->request.addNewLine(); } - void returnResult(async_data_item_t *sData) { *sData->refResult = sData->aResult; } + void returnResult(async_data *sData) { *sData->refResult = sData->aResult; } void setAuthTs(uint32_t ts) { auth_ts = ts; } @@ -2226,9 +2207,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } } - void setContentLength(async_data_item_t *sData, size_t len) + void setContentLength(async_data *sData, size_t len) { - if (sData->request.method == async_request_handler_t::http_post || sData->request.method == async_request_handler_t::http_put || sData->request.method == async_request_handler_t::http_patch) + if (sData->request.method == reqns::http_post || sData->request.method == reqns::http_put || sData->request.method == reqns::http_patch) { sData->request.addContentLengthHeader(len); sData->request.addNewLine(); @@ -2239,20 +2220,20 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { for (size_t slot = 0; slot < slotCount(); slot++) { - const async_data_item_t *sData = getData(slot); + const async_data *sData = getData(slot); if (sData && sData->to_remove) removeSlot(slot); } } - void setEvent(async_data_item_t *sData, int code, const String &msg) + void setEvent(async_data *sData, int code, const String &msg) { setEventBase(app_event, code, msg); } void removeSlot(uint8_t slot, bool sse = true) { - async_data_item_t *sData = getData(slot); + async_data *sData = getData(slot); if (!sData) return; @@ -2296,7 +2277,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if (slotCount()) { size_t slot = 0; - async_data_item_t *sData = getData(slot); + async_data *sData = getData(slot); if (!sData) return exitProcess(false); @@ -2311,7 +2292,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase *ul_dl_task_running = true; } - if (networkConnect(sData) == function_return_type_failure) + if (networkConnect(sData) == ret_failure) { // In case TCP (network) disconnected error. setAsyncError(sData, sData->state, FIREBASE_ERROR_TCP_DISCONNECTED, !sData->sse, false); @@ -2331,22 +2312,22 @@ class AsyncClientClass : public ResultBase, RTDBResultBase if ((sData->sse && sData->auth_ts != auth_ts) || client_changed || network_changed) { stop(sData); - sData->state = async_state_send_header; + sData->state = astate_send_header; } bool sending = false; - if (sData->state == async_state_undefined || sData->state == async_state_send_header || sData->state == async_state_send_payload) + if (sData->state == astate_undefined || sData->state == astate_send_header || sData->state == astate_send_payload) { sData->response.clear(); sData->request.feedTimer(!sData->async && sync_send_timeout_sec > 0 ? sync_send_timeout_sec : -1); sending = true; sData->return_type = send(sData); - while (sData->state == async_state_send_header || sData->state == async_state_send_payload) + while (sData->state == astate_send_header || sData->state == astate_send_payload) { sData->return_type = send(sData); handleSendTimeout(sData); - if (sData->async || sData->return_type == function_return_type_failure) + if (sData->async || sData->return_type == ret_failure) break; } @@ -2354,24 +2335,24 @@ class AsyncClientClass : public ResultBase, RTDBResultBase // when the request was sucessfully sent (with or without payload). // Without this initialization, the subsequent sData->response.feedTimer may not execute in case // no server response returns because of server is out of reach or network/router is not responding. - if (sData->state == async_state_read_response) + if (sData->state == astate_read_response) sData->response.feedTimer(!sData->async && sync_read_timeout_sec > 0 ? sync_read_timeout_sec : -1); } if (sending) { handleSendTimeout(sData); - if (sData->async && sData->return_type == function_return_type_continue) + if (sData->async && sData->return_type == ret_continue) return exitProcess(false); } sys_idle(); - if (sData->state == async_state_read_response) + if (sData->state == astate_read_response) { // it can be complete response from payload sending - if (sData->return_type == function_return_type_complete) - sData->return_type = function_return_type_continue; + if (sData->return_type == ret_complete) + sData->return_type = ret_continue; if (sData->async && !sData->response.tcpAvailable(client_type, client, async_tcp_config)) { @@ -2387,7 +2368,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } else if (!sData->async) // wait for non async { - while (!sData->response.tcpAvailable(client_type, client, async_tcp_config) && networkConnect(sData) == function_return_type_complete) + while (!sData->response.tcpAvailable(client_type, client, async_tcp_config) && networkConnect(sData) == ret_complete) { sys_idle(); if (handleReadTimeout(sData)) @@ -2397,10 +2378,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase } // Read until status code > 0, header finished and payload read complete - if (sData->state == async_state_read_response) + if (sData->state == astate_read_response) { sData->error.code = 0; - while (sData->return_type == function_return_type_continue && (sData->response.httpCode == 0 || sData->response.flags.header_remaining || sData->response.flags.payload_remaining)) + while (sData->return_type == ret_continue && (sData->response.httpCode == 0 || sData->response.flags.header_remaining || sData->response.flags.payload_remaining)) { sData->response.feedTimer(!sData->async && sync_read_timeout_sec > 0 ? sync_read_timeout_sec : -1); sData->return_type = receive(sData); @@ -2417,10 +2398,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase clearSSE(&sData->aResult.rtdbResult); #endif } - sData->return_type = function_return_type_failure; + sData->return_type = ret_failure; } - if (sData->async || allRead || sData->return_type == function_return_type_failure) + if (sData->async || allRead || sData->return_type == ret_failure) break; } } @@ -2431,7 +2412,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase handleEventTimeout(sData); #endif - if (!sData->sse && sData->return_type == function_return_type_complete && sData->state != async_state_send_payload) + if (!sData->sse && sData->return_type == ret_complete && sData->state != astate_send_payload) sData->to_remove = true; if (sData->to_remove) @@ -2446,14 +2427,14 @@ class AsyncClientClass : public ResultBase, RTDBResultBase public: AsyncClientClass() { - client_type = async_request_handler_t::tcp_client_type_none; + client_type = reqns::tcpc_none; } AsyncClientClass(Client &client, network_config_data &net) : client(&client) { this->net.copy(net); this->addr = reinterpret_cast(this); - client_type = async_request_handler_t::tcp_client_type_sync; + client_type = reqns::tcpc_sync; } #if defined(ENABLE_ASYNC_TCP_CLIENT) @@ -2461,7 +2442,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { this->net.copy(net); this->addr = reinterpret_cast(this); - client_type = async_request_handler_t::tcp_client_type_async; + client_type = reqns::tcpc_async; } #endif @@ -2472,7 +2453,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase for (size_t i = 0; i < sVec.size(); i++) { reset(getData(i), true); - async_data_item_t *sData = getData(i); + async_data *sData = getData(i); delete sData; sData = nullptr; } diff --git a/src/core/AsyncClient/RequestHandler.h b/src/core/AsyncClient/RequestHandler.h index 0c546a69..aeb2b29b 100644 --- a/src/core/AsyncClient/RequestHandler.h +++ b/src/core/AsyncClient/RequestHandler.h @@ -1,5 +1,5 @@ /** - * Created December 27, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -59,7 +59,7 @@ typedef void (*NetworkReconnect)(void); using namespace firebase; -namespace req_hndlr_ns +namespace reqns { enum data_item_type_t { @@ -70,19 +70,12 @@ namespace req_hndlr_ns payload, max_type }; -} - -struct async_request_handler_t -{ -private: - StringUtil sut; -public: enum tcp_client_type { - tcp_client_type_none, - tcp_client_type_sync, - tcp_client_type_async + tcpc_none, + tcpc_sync, + tcpc_async }; enum http_request_method @@ -94,34 +87,37 @@ struct async_request_handler_t http_patch, http_delete, }; +} + +struct req_handler +{ +private: + StringUtil sut; - String val[req_hndlr_ns::max_type]; +public: + String val[reqns::max_type]; String location; app_token_t *app_token = nullptr; uint16_t port = 443; uint8_t *data = nullptr; file_config_data file_data; - bool base64 = false; - bool ota = false; - uint32_t ul_dl_task_running_addr = 0; - uint32_t ota_storage_addr = 0; - uint32_t payloadLen = 0; - uint32_t dataLen = 0; - uint32_t payloadIndex = 0; + bool base64 = false, ota = false; + uint32_t ul_dl_task_running_addr = 0, ota_storage_addr = 0, payloadLen = 0; + uint32_t dataLen = 0, payloadIndex = 0; uint16_t dataIndex = 0; int8_t b64Pad = 0; int16_t ota_error = 0; - http_request_method method = http_undefined; + reqns::http_request_method method = reqns::http_undefined; Timer send_timer; - async_request_handler_t() + req_handler() { } void clear() { - for (size_t i = 0; i < req_hndlr_ns::max_type; i++) + for (size_t i = 0; i < reqns::max_type; i++) sut.clear(val[i]); port = 443; if (data) @@ -136,12 +132,12 @@ struct async_request_handler_t dataIndex = 0; b64Pad = 0; ota_error = 0; - method = http_undefined; + method = reqns::http_undefined; } void addNewLine() { - val[req_hndlr_ns::header] += "\r\n"; + val[reqns::header] += "\r\n"; } void addGAPIsHost(String &str, PGM_P sub) @@ -154,77 +150,77 @@ struct async_request_handler_t void addGAPIsHostHeader(PGM_P sub) { - val[req_hndlr_ns::header] += FPSTR("Host: "); - addGAPIsHost(val[req_hndlr_ns::header], sub); + val[reqns::header] += FPSTR("Host: "); + addGAPIsHost(val[reqns::header], sub); addNewLine(); } void addHostHeader(PGM_P host) { - val[req_hndlr_ns::header] += FPSTR("Host: "); - val[req_hndlr_ns::header] += host; + val[reqns::header] += FPSTR("Host: "); + val[reqns::header] += host; addNewLine(); } void addContentTypeHeader(PGM_P v) { - val[req_hndlr_ns::header] += FPSTR("Content-Type: "); - val[req_hndlr_ns::header] += v; + val[reqns::header] += FPSTR("Content-Type: "); + val[reqns::header] += v; addNewLine(); } void addContentLengthHeader(size_t len) { - val[req_hndlr_ns::header] += FPSTR("Content-Length: "); - val[req_hndlr_ns::header] += len; + val[reqns::header] += FPSTR("Content-Length: "); + val[reqns::header] += len; addNewLine(); } void addUAHeader() { - val[req_hndlr_ns::header] += FPSTR("User-Agent: ESP"); + val[reqns::header] += FPSTR("User-Agent: ESP"); addNewLine(); } void addConnectionHeader(bool keepAlive) { - val[req_hndlr_ns::header] += keepAlive ? FPSTR("Connection: keep-alive") : FPSTR("Connection: close"); + val[reqns::header] += keepAlive ? FPSTR("Connection: keep-alive") : FPSTR("Connection: close"); addNewLine(); } /* Append the string with first request line (HTTP method) */ - bool addRequestHeaderFirst(async_request_handler_t::http_request_method method) + bool addRequestHeaderFirst(reqns::http_request_method method) { bool post = false; switch (method) { - case async_request_handler_t::http_get: - val[req_hndlr_ns::header] += FPSTR("GET"); + case reqns::http_get: + val[reqns::header] += FPSTR("GET"); break; - case async_request_handler_t::http_post: - val[req_hndlr_ns::header] += FPSTR("POST"); + case reqns::http_post: + val[reqns::header] += FPSTR("POST"); post = true; break; - case async_request_handler_t::http_patch: - val[req_hndlr_ns::header] += FPSTR("PATCH"); + case reqns::http_patch: + val[reqns::header] += FPSTR("PATCH"); post = true; break; - case async_request_handler_t::http_delete: - val[req_hndlr_ns::header] += FPSTR("DELETE"); + case reqns::http_delete: + val[reqns::header] += FPSTR("DELETE"); break; - case async_request_handler_t::http_put: - val[req_hndlr_ns::header] += FPSTR("PUT"); + case reqns::http_put: + val[reqns::header] += FPSTR("PUT"); break; default: break; } - if (method == async_request_handler_t::http_get || method == async_request_handler_t::http_post || method == async_request_handler_t::http_patch || method == async_request_handler_t::http_delete || method == async_request_handler_t::http_put) - val[req_hndlr_ns::header] += FPSTR(" "); + if (method == reqns::http_get || method == reqns::http_post || method == reqns::http_patch || method == reqns::http_delete || method == reqns::http_put) + val[reqns::header] += FPSTR(" "); return post; } @@ -232,19 +228,19 @@ struct async_request_handler_t /* Append the string with last request line (HTTP version) */ void addRequestHeaderLast() { - val[req_hndlr_ns::header] += FPSTR(" HTTP/1.1\r\n"); + val[reqns::header] += FPSTR(" HTTP/1.1\r\n"); } /* Append the string with first part of Authorization header */ void addAuthHeaderFirst(auth_token_type type) { - val[req_hndlr_ns::header] += FPSTR("Authorization: "); + val[reqns::header] += FPSTR("Authorization: "); if (type == auth_access_token || type == auth_sa_access_token) - val[req_hndlr_ns::header] += FPSTR("Bearer "); + val[reqns::header] += FPSTR("Bearer "); else if (type == auth_user_id_token || type == auth_id_token || type == auth_custom_token || type == auth_sa_custom_token) - val[req_hndlr_ns::header] += FPSTR("Firebase "); + val[reqns::header] += FPSTR("Firebase "); else - val[req_hndlr_ns::header] += FPSTR("key="); + val[reqns::header] += FPSTR("key="); } void feedTimer(int interval = -1) @@ -252,9 +248,9 @@ struct async_request_handler_t send_timer.feed(interval == -1 ? FIREBASE_TCP_WRITE_TIMEOUT_SEC : interval); } - size_t tcpWrite(async_request_handler_t::tcp_client_type client_type, Client *client, void *atcp_config, const uint8_t *data, size_t size) + size_t tcpWrite(reqns::tcp_client_type client_type, Client *client, void *atcp_config, const uint8_t *data, size_t size) { - if (client_type == tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) return client ? client->write(data, size) : 0; else { diff --git a/src/core/AsyncClient/ResponseHandler.h b/src/core/AsyncClient/ResponseHandler.h index bfc95c30..83f0825f 100644 --- a/src/core/AsyncClient/ResponseHandler.h +++ b/src/core/AsyncClient/ResponseHandler.h @@ -1,5 +1,5 @@ /** - * Created December 27, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -31,7 +31,7 @@ #define FIREBASE_TCP_READ_TIMEOUT_SEC 30 // Do not change -namespace res_hndlr_ns +namespace resns { enum data_item_type_t { @@ -43,7 +43,7 @@ namespace res_hndlr_ns }; } -struct async_response_handler_t +struct res_handler { private: StringUtil sut; @@ -59,13 +59,8 @@ struct async_response_handler_t { public: - bool header_remaining = false; - bool payload_remaining = false; - bool keep_alive = false; - bool sse = false; - bool http_response = false; - bool chunks = false; - bool payload_available = false; + bool header_remaining = false, payload_remaining = false, keep_alive = false; + bool sse = false, http_response = false, chunks = false, payload_available = false; void reset() { @@ -93,22 +88,20 @@ struct async_response_handler_t int httpCode = 0; response_flags flags; - size_t payloadLen = 0; - size_t payloadRead = 0; + size_t payloadLen = 0, payloadRead = 0; auth_error_t error; uint8_t *toFill = nullptr; - uint16_t toFillLen = 0; - uint16_t toFillIndex = 0; - String val[res_hndlr_ns::max_type]; + uint16_t toFillLen = 0, toFillIndex = 0; + String val[resns::max_type]; chunk_info_t chunkInfo; Timer read_timer; bool auth_data_available = false; - async_response_handler_t() + res_handler() { } - ~async_response_handler_t() + ~res_handler() { if (toFill) free(toFill); @@ -130,7 +123,7 @@ struct async_response_handler_t toFill = nullptr; toFillLen = 0; toFillIndex = 0; - for (size_t i = 0; i < res_hndlr_ns::max_type; i++) + for (size_t i = 0; i < resns::max_type; i++) sut.clear(val[i]); chunkInfo.chunkSize = 0; chunkInfo.dataLen = 0; @@ -142,9 +135,9 @@ struct async_response_handler_t read_timer.feed(interval == -1 ? FIREBASE_TCP_READ_TIMEOUT_SEC : interval); } - int tcpAvailable(async_request_handler_t::tcp_client_type client_type, Client *client, void *atcp_config) + int tcpAvailable(reqns::tcp_client_type client_type, Client *client, void *atcp_config) { - if (client_type == async_request_handler_t::tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) return client ? client->available() : 0; else { @@ -169,9 +162,9 @@ struct async_response_handler_t return 0; } - int tcpRead(async_request_handler_t::tcp_client_type client_type, Client *client, void *atcp_config) + int tcpRead(reqns::tcp_client_type client_type, Client *client, void *atcp_config) { - if (client_type == async_request_handler_t::tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) return client ? client->read() : -1; else { @@ -204,9 +197,9 @@ struct async_response_handler_t return 0; } - int tcpRead(async_request_handler_t::tcp_client_type client_type, Client *client, void *atcp_config, uint8_t *buf, size_t size) + int tcpRead(reqns::tcp_client_type client_type, Client *client, void *atcp_config, uint8_t *buf, size_t size) { - if (client_type == async_request_handler_t::tcp_client_type_sync) + if (client_type == reqns::tcpc_sync) return client ? client->read(buf, size) : -1; else { diff --git a/src/core/AsyncResult/AsyncResult.h b/src/core/AsyncResult/AsyncResult.h index 4d1e646a..fd9a579b 100644 --- a/src/core/AsyncResult/AsyncResult.h +++ b/src/core/AsyncResult/AsyncResult.h @@ -62,7 +62,7 @@ class AsyncResult : public ResultBase, RealtimeDatabaseResult friend class CloudStorage; friend class FirestoreBase; friend class FirestoreDocuments; - friend class async_data_item_t; + friend class async_data; struct download_data_t { diff --git a/src/core/FirebaseApp.h b/src/core/FirebaseApp.h index 7037c0a9..aa40ec1d 100644 --- a/src/core/FirebaseApp.h +++ b/src/core/FirebaseApp.h @@ -1,5 +1,5 @@ /** - * Created December 27, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -57,7 +57,7 @@ namespace firebase private: uint16_t slot = 0; - async_data_item_t *sData = nullptr; + async_data *sData = nullptr; auth_data_t auth_data; AsyncClientClass *aClient = nullptr; uint32_t aclient_addr = 0, app_addr = 0, ref_ts = 0; @@ -303,10 +303,10 @@ namespace firebase if (sData) { addGAPIsHost(host, subdomain.c_str()); - newRequestBase(aClient, sData, host, extras, "", async_request_handler_t::http_post, soption, uid); + newRequestBase(aClient, sData, host, extras, "", reqns::http_post, soption, uid); - addContentTypeHeader(sData->request.val[req_hndlr_ns::header], "application/json"); - setContentLengthBase(aClient, sData, sData->request.val[req_hndlr_ns::payload].length()); + addContentTypeHeader(sData->request.val[reqns::header], "application/json"); + setContentLengthBase(aClient, sData, sData->request.val[reqns::payload].length()); req_timer.feed(FIREBASE_TCP_READ_TIMEOUT_SEC); slot = slotCountBase(aClient) - 1; @@ -471,9 +471,9 @@ namespace firebase if (auth_data.user_auth.auth_type == auth_sa_access_token) { #if defined(ENABLE_SERVICE_AUTH) - json.addObject(sData->request.val[req_hndlr_ns::payload], "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer", true); + json.addObject(sData->request.val[reqns::payload], "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer", true); #if defined(ENABLE_JWT) - json.addObject(sData->request.val[req_hndlr_ns::payload], "assertion", jwtProcessor()->token(), true, true); + json.addObject(sData->request.val[reqns::payload], "assertion", jwtProcessor()->token(), true, true); jwtProcessor()->clear(); #endif #endif @@ -483,24 +483,24 @@ namespace firebase #if defined(ENABLE_CUSTOM_AUTH) && defined(ENABLE_JWT) if (auth_data.user_auth.auth_type == auth_sa_custom_token) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "token", jwtProcessor()->token(), true); + json.addObject(sData->request.val[reqns::payload], "token", jwtProcessor()->token(), true); jwtProcessor()->clear(); } #endif #if defined(ENABLE_CUSTOM_TOKEN) if (auth_data.user_auth.auth_type == auth_custom_token) - json.addObject(sData->request.val[req_hndlr_ns::payload], "token", auth_data.user_auth.custom_token.val[cust_tk_ns::token], true); + json.addObject(sData->request.val[reqns::payload], "token", auth_data.user_auth.custom_token.val[cust_tk_ns::token], true); #endif - json.addObject(sData->request.val[req_hndlr_ns::payload], "returnSecureToken", "true", false, true); + json.addObject(sData->request.val[reqns::payload], "returnSecureToken", "true", false, true); } else if (auth_data.user_auth.auth_type == auth_access_token && auth_data.user_auth.task_type == firebase_core_auth_task_type_refresh_token) { #if defined(ENABLE_ACCESS_TOKEN) - json.addObject(sData->request.val[req_hndlr_ns::payload], "client_id", auth_data.user_auth.access_token.val[access_tk_ns::cid], true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "client_secret", auth_data.user_auth.access_token.val[access_tk_ns::csec], true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "grant_type", "refresh_token", true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "refresh_token", auth_data.user_auth.access_token.val[access_tk_ns::refresh], true, true); + json.addObject(sData->request.val[reqns::payload], "client_id", auth_data.user_auth.access_token.val[access_tk_ns::cid], true); + json.addObject(sData->request.val[reqns::payload], "client_secret", auth_data.user_auth.access_token.val[access_tk_ns::csec], true); + json.addObject(sData->request.val[reqns::payload], "grant_type", "refresh_token", true); + json.addObject(sData->request.val[reqns::payload], "refresh_token", auth_data.user_auth.access_token.val[access_tk_ns::refresh], true, true); #endif } @@ -549,36 +549,36 @@ namespace firebase { if (auth_data.user_auth.task_type == firebase_core_auth_task_type_send_verify_email) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "requestType", "VERIFY_EMAIL", true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "idToken", auth_data.user_auth.user.val[user_ns::id_token].length() > 0 ? auth_data.user_auth.user.val[user_ns::id_token] : auth_data.app_token.val[app_tk_ns::token], true, true); + json.addObject(sData->request.val[reqns::payload], "requestType", "VERIFY_EMAIL", true); + json.addObject(sData->request.val[reqns::payload], "idToken", auth_data.user_auth.user.val[user_ns::id_token].length() > 0 ? auth_data.user_auth.user.val[user_ns::id_token] : auth_data.app_token.val[app_tk_ns::token], true, true); } else if (auth_data.user_auth.task_type == firebase_core_auth_task_type_reset_password) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "requestType", "PASSWORD_RESET", true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "email", auth_data.user_auth.user.val[user_ns::em], true, true); + json.addObject(sData->request.val[reqns::payload], "requestType", "PASSWORD_RESET", true); + json.addObject(sData->request.val[reqns::payload], "email", auth_data.user_auth.user.val[user_ns::em], true, true); } extras = FPSTR("/v1/accounts:sendOobCode?key="); } else if (auth_data.user_auth.task_type == firebase_core_auth_task_type_delete_user) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "idToken", auth_data.user_auth.user.val[user_ns::id_token].length() ? auth_data.user_auth.user.val[user_ns::id_token] : auth_data.app_token.val[app_tk_ns::token], true, true); + json.addObject(sData->request.val[reqns::payload], "idToken", auth_data.user_auth.user.val[user_ns::id_token].length() ? auth_data.user_auth.user.val[user_ns::id_token] : auth_data.app_token.val[app_tk_ns::token], true, true); extras = FPSTR("/v1/accounts:delete?key="); } else if (auth_data.user_auth.task_type == firebase_core_auth_task_type_refresh_token) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "grantType", "refresh_token", true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "refreshToken", auth_data.app_token.val[app_tk_ns::refresh], true, true); + json.addObject(sData->request.val[reqns::payload], "grantType", "refresh_token", true); + json.addObject(sData->request.val[reqns::payload], "refreshToken", auth_data.app_token.val[app_tk_ns::refresh], true, true); extras = FPSTR("/v1/token?key="); } else { if (auth_data.user_auth.user.val[user_ns::em].length() && auth_data.user_auth.user.val[user_ns::psw].length()) { - json.addObject(sData->request.val[req_hndlr_ns::payload], "email", auth_data.user_auth.user.val[user_ns::em], true); - json.addObject(sData->request.val[req_hndlr_ns::payload], "password", auth_data.user_auth.user.val[user_ns::psw], true); + json.addObject(sData->request.val[reqns::payload], "email", auth_data.user_auth.user.val[user_ns::em], true); + json.addObject(sData->request.val[reqns::payload], "password", auth_data.user_auth.user.val[user_ns::psw], true); } - json.addObject(sData->request.val[req_hndlr_ns::payload], "returnSecureToken", "true", false, true); + json.addObject(sData->request.val[reqns::payload], "returnSecureToken", "true", false, true); extras = auth_data.user_auth.task_type == firebase_core_auth_task_type_signup ? FPSTR("/v1/accounts:signUp?key=") : FPSTR("/v1/accounts:signInWithPassword?key="); } @@ -637,9 +637,9 @@ namespace firebase if (auth_data.user_auth.task_type == firebase_core_auth_task_type_signup) auth_data.user_auth.anonymous = auth_data.user_auth.user.val[user_ns::em].length() == 0 && auth_data.user_auth.user.val[user_ns::psw].length() == 0; - if (parseToken(sData->response.val[res_hndlr_ns::payload].c_str())) + if (parseToken(sData->response.val[resns::payload].c_str())) { - sut.clear(sData->response.val[res_hndlr_ns::payload]); + sut.clear(sData->response.val[resns::payload]); auth_timer.feed(expire && expire < auth_data.app_token.expire ? expire : auth_data.app_token.expire - 2 * 60); auth_data.app_token.authenticated = true; auth_data.app_token.auth_ts = millis(); diff --git a/src/core/JSON.h b/src/core/JSON.h index 8ebcd911..c658d507 100644 --- a/src/core/JSON.h +++ b/src/core/JSON.h @@ -1,5 +1,5 @@ /** - * Created November 7, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) diff --git a/src/core/JWT.cpp b/src/core/JWT.cpp index 99540fc9..dd347fc7 100644 --- a/src/core/JWT.cpp +++ b/src/core/JWT.cpp @@ -1,5 +1,5 @@ /** - * Created December 27, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -309,7 +309,7 @@ bool JWTClass::create() if (!pk) { jwt_data.err_code = FIREBASE_ERROR_TOKEN_PARSE_PK; - jwt_data.msg = FPSTR("JWT, private key parsing fail"); + jwt_data.msg = FPSTR("JWT, private key parsing failed"); auth_data->user_auth.sa.step = jwt_step_error; return exit(false); } @@ -354,7 +354,7 @@ bool JWTClass::create() { mem.release(&buf); jwt_data.err_code = FIREBASE_ERROR_TOKEN_SIGN; - jwt_data.msg = FPSTR("JWT, token signing fail"); + jwt_data.msg = FPSTR("JWT, token signing failed"); auth_data->user_auth.sa.step = jwt_step_error; return exit(false); } diff --git a/src/database/DataOptions.h b/src/database/DataOptions.h index 21fe5ef1..969bfc80 100644 --- a/src/database/DataOptions.h +++ b/src/database/DataOptions.h @@ -42,9 +42,7 @@ class DatabaseOptions public: uint32_t readTimeout = 0; String writeSizeLimit; - bool shallow = false; - bool silent = false; - bool classicRequest = false; + bool shallow = false, silent = false, classicRequest = false; DatabaseFilter filter; void copy(const DatabaseOptions &rhs) @@ -70,8 +68,7 @@ class DatabaseOptions } private: - bool base64 = false; - bool ota = false; + bool base64 = false, ota = false; }; #endif diff --git a/src/database/RealtimeDatabase.h b/src/database/RealtimeDatabase.h index a9a5d7b1..ef8ec09c 100644 --- a/src/database/RealtimeDatabase.h +++ b/src/database/RealtimeDatabase.h @@ -1,5 +1,5 @@ /** - * Created January 19, 2025 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -99,7 +99,7 @@ class RealtimeDatabase : public RTDBResultBase template auto get(AsyncClientClass &aClient, const String &path) -> typename std::enable_if::value && !std::is_same::value, T>::type { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL); asyncRequest(aReq); return aClient.getResult()->rtdbResult.to(); } @@ -142,7 +142,7 @@ class RealtimeDatabase : public RTDBResultBase template auto get(AsyncClientClass &aClient, const String &path, DatabaseOptions &options) -> typename std::enable_if::value && !std::is_same::value, T>::type { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, false, false, false, options.shallow), &options, nullptr, aClient.getResult(), NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, false, false, false, options.shallow), &options, nullptr, aClient.getResult(), NULL); asyncRequest(aReq); return aClient.getResult()->rtdbResult.to(); } @@ -162,7 +162,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, AsyncResult &aResult, bool sse = false) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, sse, true, false, false, false), nullptr, nullptr, &aResult, NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, sse, true, false, false, false), nullptr, nullptr, &aResult, NULL); asyncRequest(aReq); } @@ -182,7 +182,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, AsyncResultCallback cb, bool sse = false, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, sse, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, sse, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); asyncRequest(aReq); } @@ -208,7 +208,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, DatabaseOptions &options, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, true, false, false, false), &options, nullptr, &aResult, NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, true, false, false, false), &options, nullptr, &aResult, NULL); asyncRequest(aReq); } @@ -235,7 +235,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, DatabaseOptions &options, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, true, false, false, false), &options, nullptr, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, true, false, false, false), &options, nullptr, nullptr, cb, uid); asyncRequest(aReq); } @@ -249,7 +249,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -287,7 +287,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, NULL); asyncRequest(aReq); } @@ -301,7 +301,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -340,7 +340,7 @@ class RealtimeDatabase : public RTDBResultBase */ void get(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(), nullptr, &file, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(), nullptr, &file, nullptr, cb, uid); asyncRequest(aReq); } @@ -360,7 +360,7 @@ class RealtimeDatabase : public RTDBResultBase { DatabaseOptions options; options.silent = true; - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(), &options, nullptr, aClient.getResult(), NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(), &options, nullptr, aClient.getResult(), NULL); asyncRequest(aReq); return !getNullETagOption(&aClient.getResult()->rtdbResult); } @@ -381,7 +381,7 @@ class RealtimeDatabase : public RTDBResultBase */ void ota(AsyncClientClass &aClient, const String &path, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, true, false, true, false), nullptr, nullptr, &aResult, NULL); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, true, false, true, false), nullptr, nullptr, &aResult, NULL); asyncRequest(aReq); } @@ -402,7 +402,7 @@ class RealtimeDatabase : public RTDBResultBase */ void ota(AsyncClientClass &aClient, const String &path, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, true, false, true, false), nullptr, nullptr, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, true, false, true, false), nullptr, nullptr, nullptr, cb, uid); asyncRequest(aReq); } @@ -428,7 +428,7 @@ class RealtimeDatabase : public RTDBResultBase template bool set(AsyncClientClass &aClient, const String &path, T value) { - return storeAsync(aClient, path, value, async_request_handler_t::http_put, false, aClient.getResult(), NULL, ""); + return storeAsync(aClient, path, value, reqns::http_put, false, aClient.getResult(), NULL, ""); } /** @@ -453,7 +453,7 @@ class RealtimeDatabase : public RTDBResultBase template void set(AsyncClientClass &aClient, const String &path, T value, AsyncResult &aResult) { - storeAsync(aClient, path, value, async_request_handler_t::http_put, true, &aResult, NULL, ""); + storeAsync(aClient, path, value, reqns::http_put, true, &aResult, NULL, ""); } /** @@ -479,7 +479,7 @@ class RealtimeDatabase : public RTDBResultBase template void set(AsyncClientClass &aClient, const String &path, T value, AsyncResultCallback cb, const String &uid = "") { - storeAsync(aClient, path, value, async_request_handler_t::http_put, true, nullptr, cb, uid); + storeAsync(aClient, path, value, reqns::http_put, true, nullptr, cb, uid); } /** @@ -492,7 +492,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -514,7 +514,7 @@ class RealtimeDatabase : public RTDBResultBase * } * file = myFile; * } - * + * * FileConfig fileConfig("/example.txt", fileCallback); * * Database.set(aClient, "/path/to/data", getFile(fileConfig), aResult); @@ -529,7 +529,7 @@ class RealtimeDatabase : public RTDBResultBase */ void set(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_put, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, nullptr); + req_data aReq(&aClient, path, reqns::http_put, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, nullptr); asyncRequest(aReq); } @@ -543,7 +543,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -565,7 +565,7 @@ class RealtimeDatabase : public RTDBResultBase * } * file = myFile; * } - * + * * FileConfig fileConfig("/example.txt", fileCallback); * * Database.set(aClient, "/path/to/data", getFile(fileConfig), cb); @@ -581,7 +581,7 @@ class RealtimeDatabase : public RTDBResultBase */ void set(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_put, slot_options_t(false, false, true, false, true, false), nullptr, &file, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_put, slot_options_t(false, false, true, false, true, false), nullptr, &file, nullptr, cb, uid); asyncRequest(aReq); } @@ -611,7 +611,7 @@ class RealtimeDatabase : public RTDBResultBase ValueConverter vcon; String payload; vcon.getVal(payload, value); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL); asyncRequest(aReq, payload.c_str()); return aClient.getResult()->rtdbResult.name(); } @@ -641,7 +641,7 @@ class RealtimeDatabase : public RTDBResultBase ValueConverter vcon; String payload; vcon.getVal(payload, value); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, &aResult, NULL); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, &aResult, NULL); asyncRequest(aReq, payload.c_str()); } @@ -671,7 +671,7 @@ class RealtimeDatabase : public RTDBResultBase ValueConverter vcon; String payload; vcon.getVal(payload, value); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); asyncRequest(aReq, payload.c_str()); } @@ -685,7 +685,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -707,7 +707,7 @@ class RealtimeDatabase : public RTDBResultBase * } * file = myFile; * } - * + * * FileConfig fileConfig("/example.txt", fileCallback); * * Database.push(aClient, "/path/to/data", getFile(fileConfig), aResult); @@ -723,7 +723,7 @@ class RealtimeDatabase : public RTDBResultBase */ void push(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, nullptr); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, true, false, false, false), nullptr, &file, &aResult, nullptr); asyncRequest(aReq); } @@ -737,7 +737,7 @@ class RealtimeDatabase : public RTDBResultBase * #include * File myFile; // Define the File object globally. * #defined FILESYSTEMS SPIFFS - * + * * void fileCallback(File &file, const char *filename, file_operating_mode mode) * { * switch (mode) @@ -759,7 +759,7 @@ class RealtimeDatabase : public RTDBResultBase * } * file = myFile; * } - * + * * FileConfig fileConfig("/example.txt", fileCallback); * * Database.push(aClient, "/path/to/data", getFile(fileConfig), cb); @@ -774,7 +774,7 @@ class RealtimeDatabase : public RTDBResultBase */ void push(AsyncClientClass &aClient, const String &path, file_config_data &file, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, true, false, false, false), nullptr, &file, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, true, false, false, false), nullptr, &file, nullptr, cb, uid); asyncRequest(aReq); } @@ -794,7 +794,7 @@ class RealtimeDatabase : public RTDBResultBase template bool update(AsyncClientClass &aClient, const String &path, const T &value) { - return storeAsync(aClient, path, value, async_request_handler_t::http_patch, false, aClient.getResult(), NULL, ""); + return storeAsync(aClient, path, value, reqns::http_patch, false, aClient.getResult(), NULL, ""); } /** @@ -812,7 +812,7 @@ class RealtimeDatabase : public RTDBResultBase template void update(AsyncClientClass &aClient, const String &path, const T &value, AsyncResult &aResult) { - storeAsync(aClient, path, value, async_request_handler_t::http_patch, true, &aResult, NULL, ""); + storeAsync(aClient, path, value, reqns::http_patch, true, &aResult, NULL, ""); } /** @@ -831,7 +831,7 @@ class RealtimeDatabase : public RTDBResultBase template void update(AsyncClientClass &aClient, const String &path, const T &value, AsyncResultCallback cb, const String &uid = "") { - storeAsync(aClient, path, value, async_request_handler_t::http_patch, true, nullptr, cb, uid); + storeAsync(aClient, path, value, reqns::http_patch, true, nullptr, cb, uid); } /** @@ -848,7 +848,7 @@ class RealtimeDatabase : public RTDBResultBase */ bool remove(AsyncClientClass &aClient, const String &path) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_delete, slot_options_t(), nullptr, nullptr, aClient.getResult(), nullptr); + req_data aReq(&aClient, path, reqns::http_delete, slot_options_t(), nullptr, nullptr, aClient.getResult(), nullptr); asyncRequest(aReq); return getNullETagOption(&aClient.getResult()->rtdbResult) && String(aClient.getResult()->rtdbResult.data()).indexOf("null") > -1; } @@ -867,7 +867,7 @@ class RealtimeDatabase : public RTDBResultBase */ void remove(AsyncClientClass &aClient, const String &path, AsyncResult &aResult) { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_delete, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, &aResult, nullptr); + req_data aReq(&aClient, path, reqns::http_delete, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, &aResult, nullptr); asyncRequest(aReq); } @@ -885,7 +885,7 @@ class RealtimeDatabase : public RTDBResultBase */ void remove(AsyncClientClass &aClient, const String &path, AsyncResultCallback cb, const String &uid = "") { - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_delete, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); + req_data aReq(&aClient, path, reqns::http_delete, slot_options_t(false, false, true, false, false, false), nullptr, nullptr, nullptr, cb, uid); asyncRequest(aReq); } @@ -941,29 +941,25 @@ class RealtimeDatabase : public RTDBResultBase } private: - String service_url; - String sse_events_filter; + String service_url, sse_events_filter; // FirebaseApp address and FirebaseApp vector address - uint32_t app_addr = 0, avec_addr = 0; - uint32_t ul_dl_task_running_addr = 0; - uint32_t ota_storage_addr = 0; + uint32_t app_addr = 0, avec_addr = 0, ul_dl_task_running_addr = 0, ota_storage_addr = 0; app_token_t *app_token = nullptr; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; - String path; - String uid; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + String path, uid; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; DatabaseOptions *options = nullptr; file_config_data *file = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, DatabaseOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, DatabaseOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; @@ -997,7 +993,7 @@ class RealtimeDatabase : public RTDBResultBase } template - bool storeAsync(AsyncClientClass &aClient, const String &path, const T &value, async_request_handler_t::http_request_method mode, bool async, AsyncResult *aResult, AsyncResultCallback cb, const String &uid) + bool storeAsync(AsyncClientClass &aClient, const String &path, const T &value, reqns::http_request_method mode, bool async, AsyncResult *aResult, AsyncResultCallback cb, const String &uid) { ValueConverter vcon; String payload; @@ -1005,14 +1001,14 @@ class RealtimeDatabase : public RTDBResultBase DatabaseOptions options; if (!async && aClient.reqEtag.length() == 0) options.silent = true; - async_request_data_t aReq(&aClient, path, mode, slot_options_t(false, false, async, payload.indexOf("\".sv\"") > -1, false, false), &options, nullptr, aResult, cb, uid); + req_data aReq(&aClient, path, mode, slot_options_t(false, false, async, payload.indexOf("\".sv\"") > -1, false, false), &options, nullptr, aResult, cb, uid); asyncRequest(aReq, payload.c_str()); if (!async) return aResult->lastError.code() == 0; return true; } - void asyncRequest(async_request_data_t &request, const char *payload = "") + void asyncRequest(req_data &request, const char *payload = "") { app_token_t *atoken = appToken(); @@ -1025,7 +1021,7 @@ class RealtimeDatabase : public RTDBResultBase addParams(request.opt.auth_param, extras, request.method, request.options, request.file); - async_data_item_t *sData = request.aClient->createSlot(request.opt); + async_data *sData = request.aClient->createSlot(request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -1056,7 +1052,7 @@ class RealtimeDatabase : public RTDBResultBase } else if (strlen(payload)) { - sData->request.val[req_hndlr_ns::payload] = payload; + sData->request.val[reqns::payload] = payload; request.aClient->setContentLength(sData, strlen(payload)); } @@ -1075,7 +1071,7 @@ class RealtimeDatabase : public RTDBResultBase request.aClient->handleRemove(); } - void addParams(bool hasQueryParams, String &extras, async_request_handler_t::http_request_method method, DatabaseOptions *options, bool isFile) + void addParams(bool hasQueryParams, String &extras, reqns::http_request_method method, DatabaseOptions *options, bool isFile) { URLUtil uut; bool hasParam = hasQueryParams; @@ -1091,7 +1087,7 @@ class RealtimeDatabase : public RTDBResultBase uut.addParam(extras, "shallow", "true", hasQueryParams, true); } - if ((options && options->silent) || ((method == async_request_handler_t::http_put || method == async_request_handler_t::http_post || method == async_request_handler_t::http_patch) && isFile)) + if ((options && options->silent) || ((method == reqns::http_put || method == reqns::http_post || method == reqns::http_patch) && isFile)) uut.addParam(extras, "print", "silent", hasQueryParams, true); if (options && options->filter.complete) @@ -1101,7 +1097,7 @@ class RealtimeDatabase : public RTDBResultBase } } - void setClientError(async_request_data_t &request, int code) + void setClientError(req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -1120,14 +1116,13 @@ class RealtimeDatabase : public RTDBResultBase } } - void setFileStatus(async_data_item_t *sData, const async_request_data_t &request) + void setFileStatus(async_data *sData, const req_data &request) { + using namespace reqns; if ((request.file && request.file->filename.length()) || request.opt.ota) { - sData->download = request.method == async_request_handler_t::http_get; - sData->upload = request.method == async_request_handler_t::http_post || - request.method == async_request_handler_t::http_put || - request.method == async_request_handler_t::http_patch; + sData->download = request.method == http_get; + sData->upload = request.method == http_post || request.method == http_put || request.method == http_patch; } } }; diff --git a/src/firestore/CollectionGroups.h b/src/firestore/CollectionGroups.h index c5692ca4..10490a29 100644 --- a/src/firestore/CollectionGroups.h +++ b/src/firestore/CollectionGroups.h @@ -1,5 +1,5 @@ /** - * Created June 12, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -39,6 +39,9 @@ namespace Firestore class CollectionGroups : public FirestoreBase { public: + + using Parent = Firestore::Parent; + CollectionGroups() {} class Indexes : public FirestoreBase @@ -62,7 +65,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String create(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index) + String create(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index) { collectionGroupIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, index, collectionId, "", false, false); return getResultBase(&aClient)->c_str(); @@ -83,7 +86,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index, AsyncResult &aResult) + void create(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index, AsyncResult &aResult) { collectionGroupIndexManager(aClient, &aResult, NULL, "", parent, index, collectionId, "", false, true); } @@ -104,7 +107,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index, AsyncResultCallback cb, const String &uid = "") + void create(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const CollectionGroupsIndex::Index &index, AsyncResultCallback cb, const String &uid = "") { collectionGroupIndexManager(aClient, nullptr, cb, uid, parent, index, collectionId, "", false, true); } @@ -125,7 +128,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId) + String deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, collectionId, indexId, true, false); @@ -147,7 +150,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId, AsyncResult &aResult) + void deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId, AsyncResult &aResult) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, &aResult, NULL, "", parent, idx, collectionId, indexId, true, true); @@ -169,7 +172,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId, AsyncResultCallback cb, const String &uid = "") + void deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId, AsyncResultCallback cb, const String &uid = "") { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, nullptr, cb, uid, parent, idx, collectionId, indexId, true, true); @@ -191,7 +194,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId) + String get(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, collectionId, indexId, false, false); @@ -213,7 +216,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId, AsyncResult &aResult) + void get(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId, AsyncResult &aResult) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, &aResult, NULL, "", parent, idx, collectionId, indexId, false, true); @@ -235,7 +238,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const String &indexId, AsyncResultCallback cb, const String &uid = "") + void get(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const String &indexId, AsyncResultCallback cb, const String &uid = "") { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, nullptr, cb, uid, parent, idx, collectionId, indexId, false, true); @@ -255,7 +258,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId) + String list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, collectionId, "", false, false); @@ -276,7 +279,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, AsyncResult &aResult) { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, &aResult, NULL, "", parent, idx, collectionId, "", false, true); @@ -296,7 +299,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, AsyncResultCallback cb, const String &uid = "") { CollectionGroupsIndex::Index idx; collectionGroupIndexManager(aClient, nullptr, cb, uid, parent, idx, collectionId, "", false, true); diff --git a/src/firestore/DataOptions.h b/src/firestore/DataOptions.h index a82e990d..8657e72e 100644 --- a/src/firestore/DataOptions.h +++ b/src/firestore/DataOptions.h @@ -1,5 +1,5 @@ /** - * Created December 27, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -42,30 +42,30 @@ using namespace FirestoreQuery; enum firebase_firestore_request_type { - firebase_firestore_request_type_undefined, - firebase_firestore_request_type_rollback = 200, - firebase_firestore_request_type_begin_transaction, - firebase_firestore_request_type_commit_document, - firebase_firestore_request_type_batch_write_doc, - firebase_firestore_request_type_run_query, - firebase_firestore_request_type_list_collection, - firebase_firestore_request_type_export_docs, - firebase_firestore_request_type_import_docs, - firebase_firestore_request_type_create_doc, - firebase_firestore_request_type_batch_get_doc, - firebase_firestore_request_type_create_composite_index, - firebase_firestore_request_type_create_field_index, - firebase_firestore_request_type_manage_database, - - firebase_firestore_request_type_get_doc = 300, - firebase_firestore_request_type_list_doc, - firebase_firestore_request_type_list_index, - firebase_firestore_request_type_get_index, - - firebase_firestore_request_type_patch_doc = 400, - - firebase_firestore_request_type_delete_doc = 500, - firebase_firestore_request_type_delete_index + cf_undefined, + cf_rollback = 200, + cf_begin_transaction, + cf_commit_document, + cf_batch_write_doc, + cf_run_query, + cf_list_collection, + cf_export_docs, + cf_import_docs, + cf_create_doc, + cf_batch_get_doc, + cf_create_composite_index, + cf_create_field_index, + cf_manage_database, + + cf_get_doc = 300, + cf_list_doc, + cf_list_index, + cf_get_index, + + cf_patch_doc = 400, + + cf_delete_doc = 500, + cf_delete_index }; /** @@ -555,11 +555,11 @@ namespace Firestore enum firestore_database_mode { - firestore_database_mode_create, - firestore_database_mode_get, - firestore_database_mode_list, - firestore_database_mode_patch, - firestore_database_mode_delete + cf_mode_create, + cf_mode_get, + cf_mode_list, + cf_mode_patch, + cf_mode_delete }; const struct firebase::key_str_30 _ConcurrencyMode[ConcurrencyMode::OPTIMISTIC_WITH_ENTITY_GROUPS + 1] PROGMEM = {"CONCURRENCY_MODE_UNSPECIFIED", "OPTIMISTIC", "PESSIMISTIC", "OPTIMISTIC_WITH_ENTITY_GROUPS"}; @@ -1334,7 +1334,7 @@ namespace Firestore String documentId; String extras; String payload; - firebase_firestore_request_type requestType = firebase_firestore_request_type_undefined; + firebase_firestore_request_type requestType = cf_undefined; unsigned long requestTime = 0; void copy(const DataOptions &rhs) diff --git a/src/firestore/Databases.h b/src/firestore/Databases.h index 0f8eb7f1..e5452cb1 100644 --- a/src/firestore/Databases.h +++ b/src/firestore/Databases.h @@ -1,5 +1,5 @@ /** - * Created June 12, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -44,6 +44,9 @@ namespace Firestore friend class AppBase; public: + + using Parent = Firestore::Parent; + ~Databases() {} Databases() {} @@ -71,7 +74,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String exportDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &exportOptions) + String exportDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &exportOptions) { eximDocs(aClient, getResultBase(&aClient), NULL, "", parent, exportOptions, false, false); return getResultBase(&aClient)->c_str(); @@ -94,7 +97,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void exportDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &exportOptions, AsyncResult &aResult) + void exportDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &exportOptions, AsyncResult &aResult) { eximDocs(aClient, &aResult, NULL, "", parent, exportOptions, false, true); } @@ -117,7 +120,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void exportDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &exportOptions, AsyncResultCallback cb, const String &uid = "") + void exportDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &exportOptions, AsyncResultCallback cb, const String &uid = "") { eximDocs(aClient, nullptr, cb, uid, parent, exportOptions, false, true); } @@ -140,7 +143,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String importDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &importOptions) + String importDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &importOptions) { eximDocs(aClient, getResultBase(&aClient), NULL, "", parent, importOptions, true, false); return getResultBase(&aClient)->c_str(); @@ -163,7 +166,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void importDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &importOptions, AsyncResult &aResult) + void importDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &importOptions, AsyncResult &aResult) { eximDocs(aClient, &aResult, NULL, "", parent, importOptions, true, true); } @@ -186,7 +189,7 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void importDocuments(AsyncClientClass &aClient, const Firestore::Parent &parent, const EximDocumentOptions &importOptions, AsyncResultCallback cb, const String &uid = "") + void importDocuments(AsyncClientClass &aClient, const Parent &parent, const EximDocumentOptions &importOptions, AsyncResultCallback cb, const String &uid = "") { eximDocs(aClient, nullptr, cb, uid, parent, importOptions, true, true); } @@ -207,9 +210,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String create(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database) + String create(AsyncClientClass &aClient, const Parent &parent, const Database &database) { - manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, database.c_str(), "", Firestore::firestore_database_mode_create, false); + manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, database.c_str(), "", Firestore::cf_mode_create, false); return getResultBase(&aClient)->c_str(); } @@ -228,9 +231,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database, AsyncResult &aResult) + void create(AsyncClientClass &aClient, const Parent &parent, const Database &database, AsyncResult &aResult) { - manageDatabase(aClient, &aResult, NULL, "", parent, database.c_str(), "", Firestore::firestore_database_mode_create, true); + manageDatabase(aClient, &aResult, NULL, "", parent, database.c_str(), "", Firestore::cf_mode_create, true); } /** Create a database. @@ -249,9 +252,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database, AsyncResultCallback cb, const String &uid = "") + void create(AsyncClientClass &aClient, const Parent &parent, const Database &database, AsyncResultCallback cb, const String &uid = "") { - manageDatabase(aClient, nullptr, cb, uid, parent, database.c_str(), "", Firestore::firestore_database_mode_create, true); + manageDatabase(aClient, nullptr, cb, uid, parent, database.c_str(), "", Firestore::cf_mode_create, true); } /** Deletes a database. @@ -268,9 +271,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String deleteDatabase(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &etag) + String deleteDatabase(AsyncClientClass &aClient, const Parent &parent, const String &etag) { - manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", etag, Firestore::firestore_database_mode_delete, false); + manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", etag, Firestore::cf_mode_delete, false); return getResultBase(&aClient)->c_str(); } @@ -287,9 +290,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void deleteDatabase(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &etag, AsyncResult &aResult) + void deleteDatabase(AsyncClientClass &aClient, const Parent &parent, const String &etag, AsyncResult &aResult) { - manageDatabase(aClient, &aResult, NULL, "", parent, "", etag, Firestore::firestore_database_mode_delete, true); + manageDatabase(aClient, &aResult, NULL, "", parent, "", etag, Firestore::cf_mode_delete, true); } /** Deletes a database. @@ -306,9 +309,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void deleteDatabase(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &etag, AsyncResultCallback cb, const String &uid = "") + void deleteDatabase(AsyncClientClass &aClient, const Parent &parent, const String &etag, AsyncResultCallback cb, const String &uid = "") { - manageDatabase(aClient, nullptr, cb, uid, parent, "", etag, Firestore::firestore_database_mode_delete, true); + manageDatabase(aClient, nullptr, cb, uid, parent, "", etag, Firestore::cf_mode_delete, true); } /** Gets information about a database. @@ -323,9 +326,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String get(AsyncClientClass &aClient, const Firestore::Parent &parent) + String get(AsyncClientClass &aClient, const Parent &parent) { - manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", "", Firestore::firestore_database_mode_get, false); + manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", "", Firestore::cf_mode_get, false); return getResultBase(&aClient)->c_str(); } @@ -340,9 +343,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResult &aResult) + void get(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - manageDatabase(aClient, &aResult, NULL, "", parent, "", "", Firestore::firestore_database_mode_get, true); + manageDatabase(aClient, &aResult, NULL, "", parent, "", "", Firestore::cf_mode_get, true); } /** Gets information about a database. @@ -357,9 +360,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void get(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - manageDatabase(aClient, nullptr, cb, uid, parent, "", "", Firestore::firestore_database_mode_get, true); + manageDatabase(aClient, nullptr, cb, uid, parent, "", "", Firestore::cf_mode_get, true); } /** List all the databases in the project. @@ -374,9 +377,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String list(AsyncClientClass &aClient, const Firestore::Parent &parent) + String list(AsyncClientClass &aClient, const Parent &parent) { - manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", "", Firestore::firestore_database_mode_list, false); + manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, "", "", Firestore::cf_mode_list, false); return getResultBase(&aClient)->c_str(); } @@ -391,9 +394,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - manageDatabase(aClient, &aResult, NULL, "", parent, "", "", Firestore::firestore_database_mode_list, true); + manageDatabase(aClient, &aResult, NULL, "", parent, "", "", Firestore::cf_mode_list, true); } /** List all the databases in the project. @@ -408,9 +411,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - manageDatabase(aClient, nullptr, cb, uid, parent, "", "", Firestore::firestore_database_mode_list, true); + manageDatabase(aClient, nullptr, cb, uid, parent, "", "", Firestore::cf_mode_list, true); } /** Updates a database. @@ -431,9 +434,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - String patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database, const String &updateMask) + String patch(AsyncClientClass &aClient, const Parent &parent, const Database &database, const String &updateMask) { - manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, database.c_str(), updateMask, Firestore::firestore_database_mode_patch, false); + manageDatabase(aClient, getResultBase(&aClient), NULL, "", parent, database.c_str(), updateMask, Firestore::cf_mode_patch, false); return getResultBase(&aClient)->c_str(); } @@ -454,9 +457,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database, const String &updateMask, AsyncResult &aResult) + void patch(AsyncClientClass &aClient, const Parent &parent, const Database &database, const String &updateMask, AsyncResult &aResult) { - manageDatabase(aClient, &aResult, NULL, "", parent, database.c_str(), updateMask, Firestore::firestore_database_mode_patch, true); + manageDatabase(aClient, &aResult, NULL, "", parent, database.c_str(), updateMask, Firestore::cf_mode_patch, true); } /** Updates a database. @@ -477,9 +480,9 @@ namespace Firestore * This function requires ServiceAuth or AccessToken authentication. * */ - void patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const Database &database, const String &updateMask, AsyncResultCallback cb, const String &uid = "") + void patch(AsyncClientClass &aClient, const Parent &parent, const Database &database, const String &updateMask, AsyncResultCallback cb, const String &uid = "") { - manageDatabase(aClient, nullptr, cb, uid, parent, database.c_str(), updateMask, Firestore::firestore_database_mode_patch, true); + manageDatabase(aClient, nullptr, cb, uid, parent, database.c_str(), updateMask, Firestore::cf_mode_patch, true); } class Indexes : public FirestoreBase @@ -502,7 +505,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String create(AsyncClientClass &aClient, const Firestore::Parent &parent, const DatabaseIndex::Index &index) + String create(AsyncClientClass &aClient, const Parent &parent, const DatabaseIndex::Index &index) { databaseIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, index, "", false, false); return getResultBase(&aClient)->c_str(); @@ -522,7 +525,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const DatabaseIndex::Index &index, AsyncResult &aResult) + void create(AsyncClientClass &aClient, const Parent &parent, const DatabaseIndex::Index &index, AsyncResult &aResult) { databaseIndexManager(aClient, &aResult, NULL, "", parent, index, "", false, true); } @@ -542,7 +545,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void create(AsyncClientClass &aClient, const Firestore::Parent &parent, const DatabaseIndex::Index &index, AsyncResultCallback cb, const String &uid = "") + void create(AsyncClientClass &aClient, const Parent &parent, const DatabaseIndex::Index &index, AsyncResultCallback cb, const String &uid = "") { databaseIndexManager(aClient, nullptr, cb, uid, parent, index, "", false, true); } @@ -562,7 +565,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId) + String deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &indexId) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, indexId, true, false); @@ -583,7 +586,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId, AsyncResult &aResult) + void deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &indexId, AsyncResult &aResult) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, &aResult, NULL, "", parent, idx, indexId, true, true); @@ -604,7 +607,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void deleteIndex(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId, AsyncResultCallback cb, const String &uid = "") + void deleteIndex(AsyncClientClass &aClient, const Parent &parent, const String &indexId, AsyncResultCallback cb, const String &uid = "") { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, nullptr, cb, uid, parent, idx, indexId, true, true); @@ -625,7 +628,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId) + String get(AsyncClientClass &aClient, const Parent &parent, const String &indexId) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, indexId, false, false); @@ -646,7 +649,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId, AsyncResult &aResult) + void get(AsyncClientClass &aClient, const Parent &parent, const String &indexId, AsyncResult &aResult) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, &aResult, NULL, "", parent, idx, indexId, false, true); @@ -667,7 +670,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &indexId, AsyncResultCallback cb, const String &uid = "") + void get(AsyncClientClass &aClient, const Parent &parent, const String &indexId, AsyncResultCallback cb, const String &uid = "") { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, nullptr, cb, uid, parent, idx, indexId, false, true); @@ -687,7 +690,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String list(AsyncClientClass &aClient, const Firestore::Parent &parent) + String list(AsyncClientClass &aClient, const Parent &parent) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, getResultBase(&aClient), NULL, "", parent, idx, "", false, false); @@ -708,7 +711,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, &aResult, NULL, "", parent, idx, "", false, true); @@ -728,7 +731,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { DatabaseIndex::Index idx(""); databaseIndexManager(aClient, nullptr, cb, uid, parent, idx, "", false, true); diff --git a/src/firestore/Documents.h b/src/firestore/Documents.h index 0b1db28e..2b04df20 100644 --- a/src/firestore/Documents.h +++ b/src/firestore/Documents.h @@ -1,5 +1,5 @@ /** - * Created June 12, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -43,6 +43,8 @@ namespace Firestore friend class AppBase; public: + using Parent = Firestore::Parent; + /** Gets multiple documents. * * @param aClient The async client. @@ -73,7 +75,7 @@ namespace Firestore * For more detail, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchGet * */ - String batchGet(AsyncClientClass &aClient, const Firestore::Parent &parent, const BatchGetDocumentOptions &batchOptions) + String batchGet(AsyncClientClass &aClient, const Parent &parent, const BatchGetDocumentOptions &batchOptions) { batchGetDoc(aClient, getResultBase(&aClient), NULL, "", parent, batchOptions, false); return getResultBase(&aClient)->c_str(); @@ -109,7 +111,7 @@ namespace Firestore * For more detail, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchGet * */ - void batchGet(AsyncClientClass &aClient, const Firestore::Parent &parent, const BatchGetDocumentOptions &batchOptions, AsyncResult &aResult) + void batchGet(AsyncClientClass &aClient, const Parent &parent, const BatchGetDocumentOptions &batchOptions, AsyncResult &aResult) { batchGetDoc(aClient, &aResult, NULL, "", parent, batchOptions, true); } @@ -145,7 +147,7 @@ namespace Firestore * For more detail, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchGet * */ - void batchGet(AsyncClientClass &aClient, const Firestore::Parent &parent, const BatchGetDocumentOptions &batchOptions, AsyncResultCallback cb, const String &uid = "") + void batchGet(AsyncClientClass &aClient, const Parent &parent, const BatchGetDocumentOptions &batchOptions, AsyncResultCallback cb, const String &uid = "") { batchGetDoc(aClient, nullptr, cb, uid, parent, batchOptions, true); } @@ -167,7 +169,7 @@ namespace Firestore * For more description, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchWrite * */ - String batchWrite(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes) + String batchWrite(AsyncClientClass &aClient, const Parent &parent, Writes &writes) { batchWriteDoc(aClient, getResultBase(&aClient), NULL, "", parent, writes, false); return getResultBase(&aClient)->c_str(); @@ -188,7 +190,7 @@ namespace Firestore * For more description, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchWrite * */ - void batchWrite(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes, AsyncResult &aResult) + void batchWrite(AsyncClientClass &aClient, const Parent &parent, Writes &writes, AsyncResult &aResult) { batchWriteDoc(aClient, &aResult, NULL, "", parent, writes, true); } @@ -209,7 +211,7 @@ namespace Firestore * For more description, see https://cloud.google.com/firestore/docs/reference/rest/v1/projects.databases.documents/batchWrite * */ - void batchWrite(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes, AsyncResultCallback cb, const String &uid = "") + void batchWrite(AsyncClientClass &aClient, const Parent &parent, Writes &writes, AsyncResultCallback cb, const String &uid = "") { batchWriteDoc(aClient, nullptr, cb, uid, parent, writes, true); } @@ -239,7 +241,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - String beginTransaction(AsyncClientClass &aClient, const Firestore::Parent &parent, const TransactionOptions &transOptions) + String beginTransaction(AsyncClientClass &aClient, const Parent &parent, const TransactionOptions &transOptions) { beginTrans(aClient, getResultBase(&aClient), NULL, "", parent, transOptions, false); return getResultBase(&aClient)->c_str(); @@ -270,7 +272,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - void beginTransaction(AsyncClientClass &aClient, const Firestore::Parent &parent, const TransactionOptions &transOptions, AsyncResult &aResult) + void beginTransaction(AsyncClientClass &aClient, const Parent &parent, const TransactionOptions &transOptions, AsyncResult &aResult) { beginTrans(aClient, &aResult, NULL, "", parent, transOptions, true); } @@ -301,7 +303,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - void beginTransaction(AsyncClientClass &aClient, const Firestore::Parent &parent, const TransactionOptions &transOptions, AsyncResultCallback cb, const String &uid = "") + void beginTransaction(AsyncClientClass &aClient, const Parent &parent, const TransactionOptions &transOptions, AsyncResultCallback cb, const String &uid = "") { beginTrans(aClient, nullptr, cb, uid, parent, transOptions, true); } @@ -321,7 +323,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - String commit(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes) + String commit(AsyncClientClass &aClient, const Parent &parent, Writes &writes) { commitDoc(aClient, getResultBase(&aClient), NULL, "", parent, writes, false); return getResultBase(&aClient)->c_str(); @@ -341,7 +343,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void commit(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes, AsyncResult &aResult) + void commit(AsyncClientClass &aClient, const Parent &parent, Writes &writes, AsyncResult &aResult) { commitDoc(aClient, &aResult, NULL, "", parent, writes, true); } @@ -361,7 +363,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void commit(AsyncClientClass &aClient, const Firestore::Parent &parent, Writes &writes, AsyncResultCallback cb, const String &uid = "") + void commit(AsyncClientClass &aClient, const Parent &parent, Writes &writes, AsyncResultCallback cb, const String &uid = "") { commitDoc(aClient, nullptr, cb, uid, parent, writes, true); } @@ -528,7 +530,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - String deleteDoc(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const Precondition ¤tDocument) + String deleteDoc(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const Precondition ¤tDocument) { deleteDocBase(aClient, getResultBase(&aClient), NULL, "", parent, documentPath, currentDocument, false); return getResultBase(&aClient)->c_str(); @@ -556,7 +558,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void deleteDoc(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const Precondition ¤tDocument, AsyncResult &aResult) + void deleteDoc(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const Precondition ¤tDocument, AsyncResult &aResult) { deleteDocBase(aClient, &aResult, NULL, "", parent, documentPath, currentDocument, true); } @@ -584,7 +586,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void deleteDoc(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const Precondition ¤tDocument, AsyncResultCallback cb, const String &uid = "") + void deleteDoc(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const Precondition ¤tDocument, AsyncResultCallback cb, const String &uid = "") { deleteDocBase(aClient, nullptr, cb, uid, parent, documentPath, currentDocument, true); } @@ -609,7 +611,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - String get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const GetDocumentOptions &options) + String get(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const GetDocumentOptions &options) { getDoc(aClient, getResultBase(&aClient), NULL, "", parent, documentPath, options, false); return getResultBase(&aClient)->c_str(); @@ -634,7 +636,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const GetDocumentOptions &options, AsyncResult &aResult) + void get(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const GetDocumentOptions &options, AsyncResult &aResult) { getDoc(aClient, &aResult, NULL, "", parent, documentPath, options, true); } @@ -659,7 +661,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void get(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const GetDocumentOptions &options, AsyncResultCallback cb, const String &uid = "") + void get(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const GetDocumentOptions &options, AsyncResultCallback cb, const String &uid = "") { getDoc(aClient, nullptr, cb, uid, parent, documentPath, options, true); } @@ -686,7 +688,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - String list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, ListDocumentsOptions listDocsOptions) + String list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, ListDocumentsOptions listDocsOptions) { listDocs(aClient, getResultBase(&aClient), NULL, "", parent, collectionId, listDocsOptions, false); return getResultBase(&aClient)->c_str(); @@ -714,7 +716,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, ListDocumentsOptions listDocsOptions, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, ListDocumentsOptions listDocsOptions, AsyncResult &aResult) { listDocs(aClient, &aResult, NULL, "", parent, collectionId, listDocsOptions, true); } @@ -742,7 +744,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void list(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &collectionId, const ListDocumentsOptions &listDocsOptions, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, const String &collectionId, const ListDocumentsOptions &listDocsOptions, AsyncResultCallback cb, const String &uid = "") { listDocs(aClient, nullptr, cb, uid, parent, collectionId, listDocsOptions, true); } @@ -767,7 +769,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - String listCollectionIds(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, ListCollectionIdsOptions listCollectionIdsOptions) + String listCollectionIds(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, ListCollectionIdsOptions listCollectionIdsOptions) { listCollIds(aClient, getResultBase(&aClient), NULL, "", parent, documentPath, listCollectionIdsOptions, false); return getResultBase(&aClient)->c_str(); @@ -793,7 +795,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void listCollectionIds(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, ListCollectionIdsOptions listCollectionIdsOptions, AsyncResult &aResult) + void listCollectionIds(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, ListCollectionIdsOptions listCollectionIdsOptions, AsyncResult &aResult) { listCollIds(aClient, &aResult, NULL, "", parent, documentPath, listCollectionIdsOptions, true); } @@ -819,7 +821,7 @@ namespace Firestore * This function requires ServiceAuth authentication. * */ - void listCollectionIds(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const ListCollectionIdsOptions &listCollectionIdsOptions, AsyncResultCallback cb, const String &uid = "") + void listCollectionIds(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const ListCollectionIdsOptions &listCollectionIdsOptions, AsyncResultCallback cb, const String &uid = "") { listCollIds(aClient, nullptr, cb, uid, parent, documentPath, listCollectionIdsOptions, true); } @@ -855,7 +857,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - String patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document) + String patch(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document) { patchDoc(aClient, getResultBase(&aClient), NULL, "", parent, documentPath, patchOptions, document, false); return getResultBase(&aClient)->c_str(); @@ -892,7 +894,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, AsyncResult &aResult) + void patch(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, AsyncResult &aResult) { patchDoc(aClient, &aResult, NULL, "", parent, documentPath, patchOptions, document, true); } @@ -929,7 +931,7 @@ namespace Firestore * This function requires ServiceAuth, CustomAuth, UserAuth, CustomToken or IDToken authentication. * */ - void patch(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, AsyncResultCallback cb, const String &uid = "") + void patch(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, AsyncResultCallback cb, const String &uid = "") { patchDoc(aClient, nullptr, cb, uid, parent, documentPath, patchOptions, document, true); } @@ -946,7 +948,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - String rollback(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &transaction) + String rollback(AsyncClientClass &aClient, const Parent &parent, const String &transaction) { transRollback(aClient, getResultBase(&aClient), NULL, "", parent, transaction, false); return getResultBase(&aClient)->c_str(); @@ -964,7 +966,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - void rollback(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &transaction, AsyncResult &aResult) + void rollback(AsyncClientClass &aClient, const Parent &parent, const String &transaction, AsyncResult &aResult) { transRollback(aClient, &aResult, NULL, "", parent, transaction, true); } @@ -982,7 +984,7 @@ namespace Firestore * * This function requires ServiceAuth authentication. */ - void rollback(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &transaction, AsyncResultCallback cb, const String &uid = "") + void rollback(AsyncClientClass &aClient, const Parent &parent, const String &transaction, AsyncResultCallback cb, const String &uid = "") { transRollback(aClient, nullptr, cb, uid, parent, transaction, true); } @@ -1015,7 +1017,7 @@ namespace Firestore * For more description, see https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery * */ - String runQuery(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, QueryOptions queryOptions) + String runQuery(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, QueryOptions queryOptions) { runQueryImpl(aClient, getResultBase(&aClient), NULL, "", parent, documentPath, queryOptions, false); return getResultBase(&aClient)->c_str(); @@ -1046,7 +1048,7 @@ namespace Firestore * For more description, see https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery * */ - void runQuery(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, QueryOptions queryOptions, AsyncResult &aResult) + void runQuery(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, QueryOptions queryOptions, AsyncResult &aResult) { runQueryImpl(aClient, &aResult, NULL, "", parent, documentPath, queryOptions, true); } @@ -1077,7 +1079,7 @@ namespace Firestore * For more description, see https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery * */ - void runQuery(AsyncClientClass &aClient, const Firestore::Parent &parent, const String &documentPath, const QueryOptions &queryOptions, AsyncResultCallback cb, const String &uid = "") + void runQuery(AsyncClientClass &aClient, const Parent &parent, const String &documentPath, const QueryOptions &queryOptions, AsyncResultCallback cb, const String &uid = "") { runQueryImpl(aClient, nullptr, cb, uid, parent, documentPath, queryOptions, true); } diff --git a/src/firestore/FirestoreBase.h b/src/firestore/FirestoreBase.h index ededbde1..a40cbee4 100644 --- a/src/firestore/FirestoreBase.h +++ b/src/firestore/FirestoreBase.h @@ -1,5 +1,5 @@ /** - * Created June 28, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -40,6 +40,8 @@ class FirestoreBase : public AppBase friend class AppBase; public: + using Parent = Firestore::Parent; + std::vector cVec; // AsyncClient vector ~FirestoreBase() {} @@ -85,27 +87,23 @@ class FirestoreBase : public AppBase } protected: - String service_url; - String path; - String uid; + String service_url, path, uid; // FirebaseApp address and FirebaseApp vector address - uint32_t app_addr = 0, avec_addr = 0; - uint32_t ul_dl_task_running_addr = 0; + uint32_t app_addr = 0, avec_addr = 0, ul_dl_task_running_addr = 0; app_token_t *app_token = nullptr; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; - String path; - String uid; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + String path, uid; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; Firestore::DataOptions *options = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - explicit async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, Firestore::DataOptions *options, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + explicit req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, Firestore::DataOptions *options, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; @@ -142,7 +140,7 @@ class FirestoreBase : public AppBase return nullptr; } - void asyncRequest(async_request_data_t &request, int beta = 0) + void asyncRequest(req_data &request, int beta = 0) { URLUtil uut; app_token_t *atoken = appToken(); @@ -169,7 +167,7 @@ class FirestoreBase : public AppBase url(FPSTR("firestore.googleapis.com")); - async_data_item_t *sData = createSlotBase(request.aClient, request.opt); + async_data *sData = createSlotBase(request.aClient, request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -178,7 +176,7 @@ class FirestoreBase : public AppBase if (request.options->payload.length()) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; + sData->request.val[reqns::payload] = request.options->payload; setContentLengthBase(request.aClient, sData, request.options->payload.length()); } @@ -190,20 +188,20 @@ class FirestoreBase : public AppBase if (request.aResult) sData->setRefResult(request.aResult, reinterpret_cast(&(getRVec(request.aClient)))); - sData->download = request.method == async_request_handler_t::http_get && sData->request.file_data.filename.length(); + sData->download = request.method == reqns::http_get && sData->request.file_data.filename.length(); processBase(request.aClient, sData->async); handleRemoveBase(request.aClient); } - void addParams(const async_request_data_t &request, String &extras) + void addParams(const req_data &request, String &extras) { extras += request.options->extras; extras.replace(" ", "%20"); extras.replace(",", "%2C"); } - void setClientError(async_request_data_t &request, int code) + void setClientError(req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -222,29 +220,29 @@ class FirestoreBase : public AppBase } } - void eximDocs(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const EximDocumentOptions &eximOptions, bool isImport, bool async) + void eximDocs(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const EximDocumentOptions &eximOptions, bool isImport, bool async) { Firestore::DataOptions options; - options.requestType = isImport ? firebase_firestore_request_type_import_docs : firebase_firestore_request_type_export_docs; + options.requestType = isImport ? cf_import_docs : cf_export_docs; options.parent = parent; options.payload = eximOptions.c_str(); if (!isImport) options.payload.replace("inputUriPrefix", "outputUriPrefix"); options.extras += isImport ? FPSTR(":importDocuments") : FPSTR(":exportDocuments"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void manageDatabase(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &database, const String &key, Firestore::firestore_database_mode mode, bool async) + void manageDatabase(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &database, const String &key, Firestore::firestore_database_mode mode, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_manage_database; + options.requestType = cf_manage_database; options.parent = parent; if (strlen(database.c_str())) { options.payload = database.c_str(); - if (mode == Firestore::firestore_database_mode_create) + if (mode == Firestore::cf_mode_create) { options.parent.setDatabaseIdParam(true); options.extras += FPSTR("?databaseId="); @@ -254,35 +252,35 @@ class FirestoreBase : public AppBase if (key.length()) { - if (mode == Firestore::firestore_database_mode_delete) + if (mode == Firestore::cf_mode_delete) options.extras += FPSTR("?etag="); - else if (mode == Firestore::firestore_database_mode_patch) + else if (mode == Firestore::cf_mode_patch) options.extras += FPSTR("?updateMask="); options.extras += key; } - if (mode == Firestore::firestore_database_mode_list) + if (mode == Firestore::cf_mode_list) options.parent.setDatabaseIdParam(true); - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + reqns::http_request_method method = reqns::http_undefined; - if (strlen(database.c_str()) > 0 && mode == Firestore::firestore_database_mode_create) - method = async_request_handler_t::http_post; // create - else if (options.parent.getDatabaseId().length() > 0 && (mode == Firestore::firestore_database_mode_delete || mode == Firestore::firestore_database_mode_get)) - method = mode == Firestore::firestore_database_mode_delete ? async_request_handler_t::http_delete : async_request_handler_t::http_get; // get index or delete by id - else if (strlen(database.c_str()) == 0 && mode == Firestore::firestore_database_mode_list) - method = async_request_handler_t::http_get; // list - else if (strlen(database.c_str()) > 0 && mode == Firestore::firestore_database_mode_patch) - method = async_request_handler_t::http_patch; // patch + if (strlen(database.c_str()) > 0 && mode == Firestore::cf_mode_create) + method = reqns::http_post; // create + else if (options.parent.getDatabaseId().length() > 0 && (mode == Firestore::cf_mode_delete || mode == Firestore::cf_mode_get)) + method = mode == Firestore::cf_mode_delete ? reqns::http_delete : reqns::http_get; // get index or delete by id + else if (strlen(database.c_str()) == 0 && mode == Firestore::cf_mode_list) + method = reqns::http_get; // list + else if (strlen(database.c_str()) > 0 && mode == Firestore::cf_mode_patch) + method = reqns::http_patch; // patch - async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void createDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &collectionId, const String &documentId, DocumentMask &mask, Document &document, bool async) + void createDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &collectionId, const String &documentId, DocumentMask &mask, Document &document, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_create_doc; + options.requestType = cf_create_doc; options.parent = parent; options.collectionId = collectionId; options.documentId = documentId; @@ -294,107 +292,107 @@ class FirestoreBase : public AppBase bool hasQueryParams = false; uut.addParam(options.extras, "documentId", options.documentId, hasQueryParams); options.extras += mask.getQuery("mask", hasQueryParams); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void patchDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, bool async) + void patchDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &documentPath, PatchDocumentOptions patchOptions, Document &document, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_patch_doc; + options.requestType = cf_patch_doc; options.parent = parent; options.payload = document.c_str(); addDocsPath(options.extras); options.extras += '/'; options.extras += documentPath; options.extras += patchOptions.c_str(); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_patch, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_patch, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void commitDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const Writes &writes, bool async) + void commitDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const Writes &writes, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_commit_document; + options.requestType = cf_commit_document; options.parent = parent; options.payload = writes.c_str(); addDocsPath(options.extras); options.extras += FPSTR(":commit"); options.payload.replace(reinterpret_cast(RESOURCE_PATH_BASE), makeResourcePath(parent)); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void batchWriteDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const Writes &writes, bool async) + void batchWriteDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const Writes &writes, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_batch_write_doc; + options.requestType = cf_batch_write_doc; options.parent = parent; options.payload = writes.c_str(); options.payload.replace(reinterpret_cast(RESOURCE_PATH_BASE), makeResourcePath(parent)); addDocsPath(options.extras); options.extras += FPSTR(":batchWrite"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void getDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &documentPath, GetDocumentOptions getOptions, bool async) + void getDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &documentPath, GetDocumentOptions getOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_get_doc; + options.requestType = cf_get_doc; options.parent = parent; addDocsPath(options.extras); options.extras += '/'; options.extras += documentPath; options.extras += getOptions.c_str(); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void batchGetDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const BatchGetDocumentOptions &batchOptions, bool async) + void batchGetDoc(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const BatchGetDocumentOptions &batchOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_batch_get_doc; + options.requestType = cf_batch_get_doc; options.parent = parent; options.payload = batchOptions.c_str(); options.payload.replace(reinterpret_cast(RESOURCE_PATH_BASE), makeResourcePath(parent)); addDocsPath(options.extras); options.extras += FPSTR(":batchGet"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void beginTrans(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, TransactionOptions transOptions, bool async) + void beginTrans(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, TransactionOptions transOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_begin_transaction; + options.requestType = cf_begin_transaction; options.parent = parent; JSONUtil jut; jut.addObject(options.payload, "options", transOptions.c_str(), false, true); addDocsPath(options.extras); options.extras += FPSTR(":beginTransaction"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void transRollback(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &transaction, bool async) + void transRollback(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &transaction, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_rollback; + options.requestType = cf_rollback; options.parent = parent; JSONUtil jut; jut.addObject(options.payload, "transaction", transaction, true, true); addDocsPath(options.extras); options.extras += FPSTR(":rollback"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } #if defined(ENABLE_FIRESTORE_QUERY) - void runQueryImpl(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &documentPath, const QueryOptions &queryOptions, bool async) + void runQueryImpl(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &documentPath, const QueryOptions &queryOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_run_query; + options.requestType = cf_run_query; options.parent = parent; options.parent.setDocPath(documentPath); options.payload = queryOptions.c_str(); @@ -403,15 +401,15 @@ class FirestoreBase : public AppBase URLUtil uut; uut.addPath(options.extras, documentPath); options.extras += FPSTR(":runQuery"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } #endif - void deleteDocBase(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &documentPath, Precondition currentDocument, bool async) + void deleteDocBase(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &documentPath, Precondition currentDocument, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_delete_doc; + options.requestType = cf_delete_doc; options.parent = parent; options.parent.setDocPath(documentPath); @@ -419,14 +417,14 @@ class FirestoreBase : public AppBase URLUtil uut; uut.addPath(options.extras, documentPath); options.extras += currentDocument.getQuery("currentDocument"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_delete, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_delete, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void listDocs(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &collectionId, const ListDocumentsOptions &listDocsOptions, bool async) + void listDocs(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &collectionId, const ListDocumentsOptions &listDocsOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_list_doc; + options.requestType = cf_list_doc; options.parent = parent; addDocsPath(options.extras); @@ -434,14 +432,14 @@ class FirestoreBase : public AppBase uut.addPath(options.extras, collectionId); options.extras += listDocsOptions.c_str(); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_get, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_get, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void listCollIds(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const String &documentPath, const ListCollectionIdsOptions &listCollectionIdsOptions, bool async) + void listCollIds(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const String &documentPath, const ListCollectionIdsOptions &listCollectionIdsOptions, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_list_collection; + options.requestType = cf_list_collection; options.parent = parent; options.parent.setDocPath(documentPath); options.payload = listCollectionIdsOptions.c_str(); @@ -451,14 +449,14 @@ class FirestoreBase : public AppBase uut.addPath(options.extras, documentPath); options.extras += FPSTR(":listCollectionIds"); - async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void databaseIndexManager(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const DatabaseIndex::Index &index, const String &indexId, bool deleteMode, bool async) + void databaseIndexManager(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const DatabaseIndex::Index &index, const String &indexId, bool deleteMode, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_create_field_index; + options.requestType = cf_create_field_index; options.parent = parent; options.payload = index.c_str(); options.extras += FPSTR("/indexes"); @@ -466,23 +464,23 @@ class FirestoreBase : public AppBase options.extras += '/'; options.extras += indexId; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + reqns::http_request_method method = reqns::http_undefined; if (strlen(index.c_str()) > 0) - method = async_request_handler_t::http_post; // create + method = reqns::http_post; // create else if (indexId.length() > 0) - method = deleteMode ? async_request_handler_t::http_delete : async_request_handler_t::http_get; // get index or delete by id + method = deleteMode ? reqns::http_delete : reqns::http_get; // get index or delete by id if (strlen(index.c_str()) == 0 && indexId.length() == 0) - method = async_request_handler_t::http_get; // list + method = reqns::http_get; // list - async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq, 1); } - void collectionGroupIndexManager(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Firestore::Parent &parent, const CollectionGroupsIndex::Index &index, const String &collectionId, const String &indexId, bool deleteMode, bool async) + void collectionGroupIndexManager(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, const CollectionGroupsIndex::Index &index, const String &collectionId, const String &indexId, bool deleteMode, bool async) { Firestore::DataOptions options; - options.requestType = firebase_firestore_request_type_create_composite_index; + options.requestType = cf_create_composite_index; options.parent = parent; options.payload = index.c_str(); options.extras += FPSTR("/collectionGroups"); @@ -497,20 +495,20 @@ class FirestoreBase : public AppBase options.extras += indexId; } - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + reqns::http_request_method method = reqns::http_undefined; if (strlen(index.c_str()) > 0) - method = async_request_handler_t::http_post; // create + method = reqns::http_post; // create else if (collectionId.length() > 0) - method = deleteMode ? async_request_handler_t::http_delete : async_request_handler_t::http_get; // get index or delete by id + method = deleteMode ? reqns::http_delete : reqns::http_get; // get index or delete by id if (strlen(index.c_str()) == 0 && indexId.length() == 0) - method = async_request_handler_t::http_get; // list + method = reqns::http_get; // list - async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - String makeResourcePath(const Firestore::Parent &parent) + String makeResourcePath(const Parent &parent) { String str = FPSTR("projects/"); str += parent.getProjectId(); @@ -520,6 +518,7 @@ class FirestoreBase : public AppBase addDocsPath(str); return str; } + void addDatabasePath(String &buf) { buf += FPSTR("/databases"); } void addDocsPath(String &buf) { buf += FPSTR("/documents"); } }; diff --git a/src/functions/DataOptions.h b/src/functions/DataOptions.h index a74cd26c..6c7d0661 100644 --- a/src/functions/DataOptions.h +++ b/src/functions/DataOptions.h @@ -1,5 +1,5 @@ /** - * Created October 29, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -41,20 +41,20 @@ namespace GoogleCloudFunctions { enum google_cloud_functions_request_type { - google_cloud_functions_request_type_undefined, - google_cloud_functions_request_type_create, - google_cloud_functions_request_type_delete, - google_cloud_functions_request_type_call, - google_cloud_functions_request_type_deploy, - google_cloud_functions_request_type_download, - google_cloud_functions_request_type_list, - google_cloud_functions_request_type_get, - google_cloud_functions_request_type_patch, - google_cloud_functions_request_type_gen_downloadUrl, - google_cloud_functions_request_type_gen_uploadUrl, - google_cloud_functions_request_type_get_iam_policy, - google_cloud_functions_request_type_set_iam_policy, - google_cloud_functions_request_type_test_iam_permission + fn_undefined, + fn_create, + fn_delete, + fn_call, + fn_deploy, + fn_download, + fn_list, + fn_get, + fn_patch, + fn_gen_downloadUrl, + fn_gen_uploadUrl, + fn_get_iam_policy, + fn_set_iam_policy, + fn_test_iam_permission }; // Severity of the state message. @@ -566,7 +566,7 @@ namespace GoogleCloudFunctions public: String payload, extras; GoogleCloudFunctions::Parent parent; - google_cloud_functions_request_type requestType = google_cloud_functions_request_type_undefined; + google_cloud_functions_request_type requestType = fn_undefined; unsigned long requestTime = 0; void copy(const DataOptions &rhs) { @@ -577,21 +577,21 @@ namespace GoogleCloudFunctions private: }; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; String path; String uid; String mime; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; DataOptions *options = nullptr; file_config_data *file = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - explicit async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + explicit req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; diff --git a/src/functions/Functions.h b/src/functions/Functions.h index ec949eb4..63f4f0f7 100644 --- a/src/functions/Functions.h +++ b/src/functions/Functions.h @@ -1,5 +1,5 @@ /** - * Created August 4, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -38,6 +38,10 @@ class CloudFunctions friend class AppBase; public: + using Parent = GoogleCloudFunctions::Parent; + using Function = GoogleCloudFunctions::Function; + using ListOptions = GoogleCloudFunctions::ListOptions; + std::vector cVec; // AsyncClient vector ~CloudFunctions() {} @@ -98,9 +102,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String create(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Function &function) + String create(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const Function &function) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_create, function.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_create, function.c_str(), false); return aClient.getResult()->c_str(); } @@ -121,9 +125,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void create(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Function &function, AsyncResult &aResult) + void create(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const Function &function, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_create, function.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_create, function.c_str(), true); } /** Creates a new function. @@ -144,9 +148,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void create(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, GoogleCloudFunctions::Function &function, AsyncResultCallback cb, const String &uid = "") + void create(AsyncClientClass &aClient, const Parent &parent, const String &functionId, GoogleCloudFunctions::Function &function, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_create, function.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_create, function.c_str(), true); } /** Creates a new function. @@ -168,9 +172,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String patch(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Function &function, const String &updateMask) + String patch(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const Function &function, const String &updateMask) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::google_cloud_functions_request_type_patch, function.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::fn_patch, function.c_str(), false); return aClient.getResult()->c_str(); } @@ -193,9 +197,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void patch(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Function &function, const String &updateMask, AsyncResult &aResult) + void patch(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const Function &function, const String &updateMask, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::google_cloud_functions_request_type_patch, function.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::fn_patch, function.c_str(), true); } /** Creates a new function. @@ -218,9 +222,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void patch(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, GoogleCloudFunctions::Function &function, const String &updateMask, AsyncResultCallback cb, const String &uid = "") + void patch(AsyncClientClass &aClient, const Parent &parent, const String &functionId, Function &function, const String &updateMask, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::google_cloud_functions_request_type_patch, function.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, updateMask, GoogleCloudFunctions::fn_patch, function.c_str(), true); } /** Deletes a function with the given name from the specified project. If the given function is used by some trigger, the trigger will be updated to remove this function. @@ -237,9 +241,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String deleteFunction(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId) + String deleteFunction(AsyncClientClass &aClient, const Parent &parent, const String &functionId) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_delete, "", false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_delete, "", false); return aClient.getResult()->c_str(); } @@ -257,9 +261,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void deleteFunction(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResult &aResult) + void deleteFunction(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_delete, "", true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_delete, "", true); } /** Deletes a function with the given name from the specified project. If the given function is used by some trigger, the trigger will be updated to remove this function. @@ -277,9 +281,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void deleteFunction(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") + void deleteFunction(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_delete, "", true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_delete, "", true); } /** Returns a list of functions that belong to the requested project. @@ -296,9 +300,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String list(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, GoogleCloudFunctions::ListOptions &listOptions) + String list(AsyncClientClass &aClient, const Parent &parent, ListOptions &listOptions) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::google_cloud_functions_request_type_list, "", false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::fn_list, "", false); return aClient.getResult()->c_str(); } @@ -316,9 +320,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void list(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, GoogleCloudFunctions::ListOptions &listOptions, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, ListOptions &listOptions, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::google_cloud_functions_request_type_list, "", true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::fn_list, "", true); } /** Returns a list of functions that belong to the requested project. @@ -336,9 +340,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void list(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, GoogleCloudFunctions::ListOptions &listOptions, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, ListOptions &listOptions, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::google_cloud_functions_request_type_list, "", true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", &listOptions, "", GoogleCloudFunctions::fn_list, "", true); } /** Returns a function with the given name from the requested project. @@ -355,9 +359,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String get(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId) + String get(AsyncClientClass &aClient, const Parent &parent, const String &functionId) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get, "", false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", nullptr, "", GoogleCloudFunctions::fn_get, "", false); return aClient.getResult()->c_str(); } @@ -375,9 +379,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void get(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResult &aResult) + void get(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get, "", true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", nullptr, "", GoogleCloudFunctions::fn_get, "", true); } /** Returns a function with the given name from the requested project. @@ -395,9 +399,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void get(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") + void get(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get, "", true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", nullptr, "", GoogleCloudFunctions::fn_get, "", true); } /** Synchronously invokes a deployed Cloud Function. To be used for testing purposes as very limited traffic is allowed. For more information on the actual limits, refer to Rate Limits (https://cloud.google.com/functions/quotas#rate_limits). @@ -415,9 +419,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String call(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const String &payload) + String call(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const String &payload) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_call, payload, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_call, payload, false); return aClient.getResult()->c_str(); } @@ -436,9 +440,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void call(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const String &payload, AsyncResult &aResult) + void call(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const String &payload, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_call, payload, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_call, payload, true); } /** Synchronously invokes a deployed Cloud Function. To be used for testing purposes as very limited traffic is allowed. For more information on the actual limits, refer to Rate Limits (https://cloud.google.com/functions/quotas#rate_limits). @@ -457,9 +461,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void call(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const String &payload, AsyncResultCallback cb, const String &uid = "") + void call(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const String &payload, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_call, payload, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_call, payload, true); } /** Returns a signed URL for downloading deployed function source code. The URL is only valid for a limited period and should be used within 30 minutes of generation. For more information about the signed URL usage see: https://cloud.google.com/storage/docs/access-control/signed-urls @@ -476,9 +480,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String generateDownloadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId) + String generateDownloadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl, "", false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_downloadUrl, "", false); return aClient.getResult()->c_str(); } @@ -496,9 +500,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void generateDownloadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResult &aResult) + void generateDownloadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl, "", true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_downloadUrl, "", true); } /** Returns a signed URL for downloading deployed function source code. The URL is only valid for a limited period and should be used within 30 minutes of generation. For more information about the signed URL usage see: https://cloud.google.com/storage/docs/access-control/signed-urls @@ -516,9 +520,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void generateDownloadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") + void generateDownloadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl, "", true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_downloadUrl, "", true); } /** RReturns a signed URL for uploading a function source code. For more information about the signed URL usage see: https://cloud.google.com/storage/docs/access-control/signed-urls. Once the function source code upload is complete, the used signed URL should be provided in functions.create or functions.patch request as a reference to the function source code. @@ -536,9 +540,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String generateUploadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options) + String generateUploadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_uploadUrl, options.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_uploadUrl, options.c_str(), false); return aClient.getResult()->c_str(); } @@ -557,9 +561,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void generateUploadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options, AsyncResult &aResult) + void generateUploadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_uploadUrl, options.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_uploadUrl, options.c_str(), true); } /** Returns a signed URL for uploading a function source code. For more information about the signed URL usage see: https://cloud.google.com/storage/docs/access-control/signed-urls. Once the function source code upload is complete, the used signed URL should be provided in functions.create or functions.patch request as a reference to the function source code. @@ -578,9 +582,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void generateUploadURL(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options, AsyncResultCallback cb, const String &uid = "") + void generateUploadURL(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::UploadURLOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_gen_uploadUrl, options.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_gen_uploadUrl, options.c_str(), true); } /** Gets the access control policy for a resource. Returns an empty policy if the resource exists and does not have a policy set. @@ -598,9 +602,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String getIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options) + String getIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy, options.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_get_iam_policy, options.c_str(), false); return aClient.getResult()->c_str(); } @@ -619,9 +623,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void getIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options, AsyncResult &aResult) + void getIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy, options.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_get_iam_policy, options.c_str(), true); } /** Gets the access control policy for a resource. Returns an empty policy if the resource exists and does not have a policy set. @@ -640,9 +644,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void getIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options, AsyncResultCallback cb, const String &uid = "") + void getIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, GoogleCloudFunctions::GetPolicyOptions options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy, options.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_get_iam_policy, options.c_str(), true); } /** Sets the access control policy on the specified resource. Replaces any existing policy. @@ -661,9 +665,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String setIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options) + String setIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_set_iam_policy, options.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_set_iam_policy, options.c_str(), false); return aClient.getResult()->c_str(); } @@ -683,9 +687,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void setIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options, AsyncResult &aResult) + void setIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_set_iam_policy, options.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_set_iam_policy, options.c_str(), true); } /** Sets the access control policy on the specified resource. Replaces any existing policy. @@ -705,9 +709,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void setIamPolicy(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options, AsyncResultCallback cb, const String &uid = "") + void setIamPolicy(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::SetPolicyOptions &options, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_set_iam_policy, options.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_set_iam_policy, options.c_str(), true); } /** Returns permissions that a caller has on the specified resource. If the resource does not exist, this will return an empty set of permissions, not a NOT_FOUND error. @@ -726,9 +730,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - String testIamPermissions(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions) + String testIamPermissions(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_test_iam_permission, permissions.c_str(), false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_test_iam_permission, permissions.c_str(), false); return aClient.getResult()->c_str(); } @@ -748,9 +752,9 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void testIamPermissions(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions, AsyncResult &aResult) + void testIamPermissions(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_test_iam_permission, permissions.c_str(), true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_test_iam_permission, permissions.c_str(), true); } /** Returns permissions that a caller has on the specified resource. If the resource does not exist, this will return an empty set of permissions, not a NOT_FOUND error. @@ -770,19 +774,15 @@ class CloudFunctions * This function requires OAuth2.0 authentication. * */ - void testIamPermissions(AsyncClientClass &aClient, const GoogleCloudFunctions::Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions, AsyncResultCallback cb, const String &uid = "") + void testIamPermissions(AsyncClientClass &aClient, const Parent &parent, const String &functionId, const GoogleCloudFunctions::Permissions &permissions, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::google_cloud_functions_request_type_test_iam_permission, permissions.c_str(), true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, functionId, nullptr, "", GoogleCloudFunctions::fn_test_iam_permission, permissions.c_str(), true); } private: - String service_url; - String path; - String uid; + String service_url, path, uid; // FirebaseApp address and FirebaseApp vector address - uint32_t app_addr = 0, avec_addr = 0; - // Not used but required. - uint32_t ul_dl_task_running_addr = 0; + uint32_t app_addr = 0, avec_addr = 0, ul_dl_task_running_addr = 0; app_token_t *app_token = nullptr; void url(const String &url) @@ -809,9 +809,10 @@ class CloudFunctions return nullptr; } - void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const GoogleCloudFunctions::Parent &parent, file_config_data *file, const String &functionId, const GoogleCloudFunctions::ListOptions *listOptions, const String &updateMask, GoogleCloudFunctions::google_cloud_functions_request_type requestType, const String &payload, bool async) + void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, file_config_data *file, const String &functionId, const ListOptions *listOptions, const String &updateMask, GoogleCloudFunctions::google_cloud_functions_request_type requestType, const String &payload, bool async) { - GoogleCloudFunctions::DataOptions options; + using namespace GoogleCloudFunctions; + DataOptions options; options.requestType = requestType; options.parent = parent; options.parent.setFunctionId(functionId); @@ -819,38 +820,43 @@ class CloudFunctions bool hasParam = false; URLUtil uut; - async_request_handler_t::http_request_method method = async_request_handler_t::http_post; + reqns::http_request_method method = reqns::http_post; if (payload.length()) { - if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_call) + if (requestType == fn_call) options.payload = "{\"data\":\"" + payload + "\"}"; - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy) + else if (requestType == fn_get_iam_policy) options.extras = "?" + payload; else options.payload = payload; } - if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_create) + switch (requestType) { + case fn_create: hasParam = true; - method = async_request_handler_t::http_post; + method = reqns::http_post; options.extras = "?functionId="; options.extras += functionId; - } - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_patch) - { - method = async_request_handler_t::http_patch; - } - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_delete) - { - method = async_request_handler_t::http_delete; - } - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_get || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_list || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy) - { - method = async_request_handler_t::http_get; + break; + + case fn_patch: + method = reqns::http_patch; + break; + + case fn_delete: + method = reqns::http_delete; + break; + + case fn_get: + case fn_list: + case fn_get_iam_policy: + method = reqns::http_get; + break; + + default: + break; } if (listOptions && strlen(listOptions->c_str())) @@ -863,13 +869,14 @@ class CloudFunctions if (updateMask.length()) uut.addParamsTokens(options.extras, "updateMask=", updateMask, hasParam); - GoogleCloudFunctions::async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, file, result, cb, uid); + req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, false, false), &options, file, result, cb, uid); asyncRequest(aReq, requestType); } - void asyncRequest(GoogleCloudFunctions::async_request_data_t &request, GoogleCloudFunctions::google_cloud_functions_request_type requestType) + void asyncRequest(GoogleCloudFunctions::req_data &request, GoogleCloudFunctions::google_cloud_functions_request_type requestType) { + using namespace GoogleCloudFunctions; app_token_t *atoken = appToken(); if (!atoken) @@ -879,45 +886,42 @@ class CloudFunctions String extras; request.path = "/v"; - request.path += requestType == GoogleCloudFunctions::google_cloud_functions_request_type_call ? 1 : 2; + request.path += requestType == fn_call ? 1 : 2; request.path += "/projects/"; request.path += request.options->parent.getProjectId(); request.path += "/locations/"; request.path += request.options->parent.getLocationId(); request.path += "/functions"; - if ((request.method == async_request_handler_t::http_get && request.options->parent.getFunctionId().length() > 0) || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_call || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl || - request.method == async_request_handler_t::http_patch || - request.method == async_request_handler_t::http_delete || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_set_iam_policy || - requestType == GoogleCloudFunctions::google_cloud_functions_request_type_test_iam_permission) + if ((request.method == reqns::http_get && request.options->parent.getFunctionId().length() > 0) || + requestType == fn_call || requestType == fn_gen_downloadUrl || request.method == reqns::http_patch || + request.method == reqns::http_delete || requestType == fn_get_iam_policy || requestType == fn_set_iam_policy || + requestType == fn_test_iam_permission) { URLUtil uut; request.path += "/"; request.path += uut.encode(request.options->parent.getFunctionId()); - if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_call) + + if (requestType == fn_call) request.path += ":call"; - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl) + else if (requestType == fn_gen_downloadUrl) request.path += ":generateDownloadUrl"; - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_get_iam_policy) + else if (requestType == fn_get_iam_policy) request.path += ":getIamPolicy"; - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_set_iam_policy) + else if (requestType == fn_set_iam_policy) request.path += ":setIamPolicy"; - else if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_test_iam_permission) + else if (requestType == fn_test_iam_permission) request.path += ":testIamPermissions"; } - if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_gen_uploadUrl) + if (requestType == fn_gen_uploadUrl) request.path += ":generateUploadUrl"; addParams(request, extras); url(FPSTR("cloudfunctions.googleapis.com")); - async_data_item_t *sData = request.aClient->createSlot(request.opt); + async_data *sData = request.aClient->createSlot(request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -943,12 +947,12 @@ class CloudFunctions } else if (request.options->payload.length()) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; + sData->request.val[reqns::payload] = request.options->payload; request.aClient->setContentLength(sData, request.options->payload.length()); } - if (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_gen_downloadUrl || - (requestType == GoogleCloudFunctions::google_cloud_functions_request_type_gen_uploadUrl && request.options->payload.length() == 0)) + if (requestType == fn_gen_downloadUrl || + (requestType == fn_gen_uploadUrl && request.options->payload.length() == 0)) request.aClient->setContentLength(sData, 0); if (request.cb) @@ -963,7 +967,7 @@ class CloudFunctions request.aClient->handleRemove(); } - void setClientError(GoogleCloudFunctions::async_request_data_t &request, int code) + void setClientError(GoogleCloudFunctions::req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -982,21 +986,20 @@ class CloudFunctions } } - void addParams(const GoogleCloudFunctions::async_request_data_t &request, String &extras) + void addParams(const GoogleCloudFunctions::req_data &request, String &extras) { extras += request.options->extras; extras.replace(" ", "%20"); extras.replace(",", "%2C"); } - void setFileStatus(async_data_item_t *sData, const GoogleCloudFunctions::async_request_data_t &request) + void setFileStatus(async_data *sData, const GoogleCloudFunctions::req_data &request) { + using namespace reqns; if ((request.file && request.file->filename.length()) || request.opt.ota) { - sData->download = request.method == async_request_handler_t::http_get; - sData->upload = request.method == async_request_handler_t::http_post || - request.method == async_request_handler_t::http_put || - request.method == async_request_handler_t::http_patch; + sData->download = request.method == http_get; + sData->upload = request.method == http_post || request.method == http_put || request.method == http_patch; } } }; diff --git a/src/messaging/DataOptions.h b/src/messaging/DataOptions.h index 2ba63d04..76c35e0a 100644 --- a/src/messaging/DataOptions.h +++ b/src/messaging/DataOptions.h @@ -1,5 +1,5 @@ /** - * Created June 12, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -38,8 +38,8 @@ namespace Messages { enum firebase_cloud_messaging_request_type { - firebase_cloud_messaging_request_type_undefined, - firebase_cloud_messaging_request_type_send + fcm_undefined, + fcm_send }; // Priority of a message to send to Android devices. Note this priority is an FCM concept that controls when the message is delivered. See FCM guides. Additionally, you can determine notification display priority on targeted Android devices using AndroidNotification.NotificationPriority. @@ -108,7 +108,7 @@ namespace Messages // This means that a value of 1.0 corresponds to a solid color, whereas a value of 0.0 corresponds to a completely transparent color. Color &alpha(float value) { return wr.set(*this, UnityRange().val(value), buf, bufSize, 4, FPSTR(__func__)); } }; - + /** * Settings to control notification LED. */ @@ -406,7 +406,7 @@ namespace Messages public: String payload, extras; Messages::Parent parent; - firebase_cloud_messaging_request_type requestType = firebase_cloud_messaging_request_type_undefined; + firebase_cloud_messaging_request_type requestType = fcm_undefined; unsigned long requestTime = 0; void copy(const DataOptions &rhs) @@ -418,19 +418,18 @@ namespace Messages private: }; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; - String path; - String uid; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + String path, uid; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; DataOptions *options = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - explicit async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, DataOptions *options, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + explicit req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, DataOptions *options, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; diff --git a/src/messaging/Messaging.h b/src/messaging/Messaging.h index e5484d0f..d2ea02d4 100644 --- a/src/messaging/Messaging.h +++ b/src/messaging/Messaging.h @@ -1,5 +1,5 @@ /** - * Created August 4, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -96,7 +96,7 @@ class Messaging */ String send(AsyncClientClass &aClient, const Messages::Parent &parent, const Messages::Message &message) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, message.c_str(), Messages::firebase_cloud_messaging_request_type_send, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, message.c_str(), Messages::fcm_send, false); return aClient.getResult()->c_str(); } @@ -114,7 +114,7 @@ class Messaging */ void send(AsyncClientClass &aClient, const Messages::Parent &parent, const Messages::Message &message, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, message.c_str(), Messages::firebase_cloud_messaging_request_type_send, true); + sendRequest(aClient, &aResult, NULL, "", parent, message.c_str(), Messages::fcm_send, true); } /** Send a message to specified target (a registration token, topic or condition) with HTTP v1 API. @@ -132,17 +132,13 @@ class Messaging */ void send(AsyncClientClass &aClient, const Messages::Parent &parent, const Messages::Message &message, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, message.c_str(), Messages::firebase_cloud_messaging_request_type_send, true); + sendRequest(aClient, nullptr, cb, uid, parent, message.c_str(), Messages::fcm_send, true); } private: - String service_url; - String path; - String uid; + String service_url, path, uid; // FirebaseApp address and FirebaseApp vector address - uint32_t app_addr = 0, avec_addr = 0; - // Not used but required. - uint32_t ul_dl_task_running_addr = 0; + uint32_t app_addr = 0, avec_addr = 0, ul_dl_task_running_addr = 0; app_token_t *app_token = nullptr; void url(const String &url) @@ -171,21 +167,22 @@ class Messaging void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Messages::Parent &parent, const String &payload, Messages::firebase_cloud_messaging_request_type requestType, bool async) { - Messages::DataOptions options; + using namespace Messages; + DataOptions options; options.requestType = requestType; options.parent = parent; - if (requestType == Messages::firebase_cloud_messaging_request_type_send) + if (requestType == fcm_send) { JSONUtil jut; jut.addObject(options.payload, "message", payload, false, true); options.extras += FPSTR("/messages:send"); } - Messages::async_request_data_t aReq(&aClient, path, async_request_handler_t::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); + req_data aReq(&aClient, path, reqns::http_post, slot_options_t(false, false, async, false, false, false), &options, result, cb, uid); asyncRequest(aReq); } - void asyncRequest(Messages::async_request_data_t &request, int beta = 0) + void asyncRequest(Messages::req_data &request, int beta = 0) { URLUtil uut; app_token_t *atoken = appToken(); @@ -209,7 +206,7 @@ class Messaging url(FPSTR("fcm.googleapis.com")); - async_data_item_t *sData = request.aClient->createSlot(request.opt); + async_data *sData = request.aClient->createSlot(request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -218,7 +215,7 @@ class Messaging if (request.options->payload.length()) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; + sData->request.val[reqns::payload] = request.options->payload; request.aClient->setContentLength(sData, request.options->payload.length()); } @@ -230,13 +227,13 @@ class Messaging if (request.aResult) sData->setRefResult(request.aResult, reinterpret_cast(&(request.aClient->rVec))); - sData->download = request.method == async_request_handler_t::http_get && sData->request.file_data.filename.length(); + sData->download = request.method == reqns::http_get && sData->request.file_data.filename.length(); request.aClient->process(sData->async); request.aClient->handleRemove(); } - void setClientError(Messages::async_request_data_t &request, int code) + void setClientError(Messages::req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -255,7 +252,7 @@ class Messaging } } - void addParams(const Messages::async_request_data_t &request, String &extras) + void addParams(const Messages::req_data &request, String &extras) { extras += request.options->extras; extras.replace(" ", "%20"); diff --git a/src/storage/DataOptions.h b/src/storage/DataOptions.h index 26447262..2df9c387 100644 --- a/src/storage/DataOptions.h +++ b/src/storage/DataOptions.h @@ -1,5 +1,5 @@ /** - * Created June 12, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -37,13 +37,13 @@ namespace FirebaseStorage { enum firebase_storage_request_type { - firebase_storage_request_type_undefined, - firebase_storage_request_type_upload, - firebase_storage_request_type_download, - firebase_storage_request_type_get_meta, - firebase_storage_request_type_delete, - firebase_storage_request_type_list, - firebase_storage_request_type_download_ota + fs_undefined, + fs_upload, + fs_download, + fs_get_meta, + fs_delete, + fs_list, + fs_download_ota }; class Parent @@ -88,7 +88,7 @@ namespace FirebaseStorage public: String payload, extras; FirebaseStorage::Parent parent; - firebase_storage_request_type requestType = firebase_storage_request_type_undefined; + firebase_storage_request_type requestType = fs_undefined; unsigned long requestTime = 0; void copy(const DataOptions &rhs) @@ -100,21 +100,19 @@ namespace FirebaseStorage private: }; - struct async_request_data_t + struct req_data { public: AsyncClientClass *aClient = nullptr; - String path; - String uid; - String mime; - async_request_handler_t::http_request_method method = async_request_handler_t::http_undefined; + String path, uid, mime; + reqns::http_request_method method = reqns::http_undefined; slot_options_t opt; DataOptions *options = nullptr; file_config_data *file = nullptr; AsyncResult *aResult = nullptr; AsyncResultCallback cb = NULL; - async_request_data_t() {} - explicit async_request_data_t(AsyncClientClass *aClient, const String &path, async_request_handler_t::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") + req_data() {} + explicit req_data(AsyncClientClass *aClient, const String &path, reqns::http_request_method method, slot_options_t opt, DataOptions *options, file_config_data *file, AsyncResult *aResult, AsyncResultCallback cb, const String &uid = "") { this->aClient = aClient; this->path = path; diff --git a/src/storage/Storage.h b/src/storage/Storage.h index 42672b98..8e8af061 100644 --- a/src/storage/Storage.h +++ b/src/storage/Storage.h @@ -1,5 +1,5 @@ /** - * Created August 4, 2024 + * Created January 20, 2025 * * The MIT License (MIT) * Copyright (c) 2025 K. Suwatchai (Mobizt) @@ -39,6 +39,9 @@ class Storage friend class AppBase; public: + + using Parent = FirebaseStorage::Parent; + std::vector cVec; // AsyncClient vector ~Storage() {} @@ -94,9 +97,9 @@ class Storage * @return Boolean value, indicates the success of the operation. * */ - bool download(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file) + bool download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, "", FirebaseStorage::firebase_storage_request_type_download, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, "", FirebaseStorage::fs_download, false); return aClient.getResult()->lastError.code() == 0; } @@ -111,9 +114,9 @@ class Storage * @param aResult The async result (AsyncResult). * */ - void download(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file, AsyncResult &aResult) + void download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, &file, "", FirebaseStorage::firebase_storage_request_type_download, true); + sendRequest(aClient, &aResult, NULL, "", parent, &file, "", FirebaseStorage::fs_download, true); } /** Download object from the Firebase Storage. @@ -128,9 +131,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void download(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file, AsyncResultCallback cb, const String &uid = "") + void download(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, &file, "", FirebaseStorage::firebase_storage_request_type_download, true); + sendRequest(aClient, nullptr, cb, uid, parent, &file, "", FirebaseStorage::fs_download, true); } /** Upload file to the Firebase Storage. @@ -145,9 +148,9 @@ class Storage * @return Boolean value, indicates the success of the operation. * */ - bool upload(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file, const String &mime) + bool upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, const String &mime) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, mime, FirebaseStorage::firebase_storage_request_type_upload, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, &file, mime, FirebaseStorage::fs_upload, false); return aClient.getResult()->lastError.code() == 0; } @@ -163,9 +166,9 @@ class Storage * * */ - void upload(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file, const String &mime, AsyncResult &aResult) + void upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, const String &mime, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, &file, mime, FirebaseStorage::firebase_storage_request_type_upload, true); + sendRequest(aClient, &aResult, NULL, "", parent, &file, mime, FirebaseStorage::fs_upload, true); } /** Upload file to the Firebase Storage. @@ -181,9 +184,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void upload(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, file_config_data &file, const String &mime, AsyncResultCallback cb, const String &uid = "") + void upload(AsyncClientClass &aClient, const Parent &parent, file_config_data &file, const String &mime, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, &file, mime, FirebaseStorage::firebase_storage_request_type_upload, true); + sendRequest(aClient, nullptr, cb, uid, parent, &file, mime, FirebaseStorage::fs_upload, true); } /** Perform OTA update using a firmware (object) from the Firebase Storage. @@ -197,9 +200,9 @@ class Storage * @return Boolean value, indicates the success of the operation. * */ - bool ota(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent) + bool ota(AsyncClientClass &aClient, const Parent &parent) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_download_ota, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::fs_download_ota, false); return aClient.getResult()->lastError.code() == 0; } @@ -213,15 +216,15 @@ class Storage * @param aResult The async result (AsyncResult). * */ - void ota(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResult &aResult) + void ota(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_download_ota, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::fs_download_ota, true); } /** Perform OTA update using a firmware (object) from the Firebase Storage. * * @param aClient The async client. - * @param parent The FirebaseStorage::Parent object included Storage bucket IdThe FirebaseStorage::Parent object included Storage bucket Id, object and/or access token in its constructor. + * @param parent The FirebaseStorage::Parent object included Storage bucket IdThe FirebaseStorage::Parent object included Storage bucket Id, object and/or access token in its constructor. * The bucketid is the Storage bucket Id of object to download. * The object is the object in Storage bucket to download. * The access token is the Firebase Storage's file access token which used only for priviledge file download access in non-authentication mode (NoAuth). @@ -229,9 +232,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void ota(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void ota(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_download_ota, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::fs_download_ota, true); } /** Get the metadata of object in Firebase Storage data bucket. @@ -244,9 +247,9 @@ class Storage * @return String The response payload. * */ - String getMetadata(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent) + String getMetadata(AsyncClientClass &aClient, const Parent &parent) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_get_meta, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::fs_get_meta, false); return aClient.getResult()->c_str(); } @@ -259,9 +262,9 @@ class Storage * @param aResult The async result (AsyncResult). * */ - void getMetadata(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResult &aResult) + void getMetadata(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_get_meta, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::fs_get_meta, true); } /** Get the metadata of object in Firebase Storage data bucket. @@ -274,9 +277,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void getMetadata(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void getMetadata(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_get_meta, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::fs_get_meta, true); } /** List all objects in Firebase Storage data bucket. @@ -288,9 +291,9 @@ class Storage * @return String The response payload. * */ - String list(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent) + String list(AsyncClientClass &aClient, const Parent &parent) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_list, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::fs_list, false); return aClient.getResult()->c_str(); } @@ -302,9 +305,9 @@ class Storage * @param aResult The async result (AsyncResult). * */ - void list(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResult &aResult) + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_list, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::fs_list, true); } /** List all objects in Firebase Storage data bucket. @@ -316,9 +319,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void list(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void list(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_list, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::fs_list, true); } /** Delete the object in Firebase Storage data bucket. @@ -331,9 +334,9 @@ class Storage * @return Boolean value, indicates the success of the operation. * */ - bool deleteObject(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent) + bool deleteObject(AsyncClientClass &aClient, const Parent &parent) { - sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_delete, false); + sendRequest(aClient, aClient.getResult(), NULL, "", parent, nullptr, "", FirebaseStorage::fs_delete, false); return aClient.getResult()->lastError.code() == 0; } @@ -346,9 +349,9 @@ class Storage * @param aResult The async result (AsyncResult). * */ - void deleteObject(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResult &aResult) + void deleteObject(AsyncClientClass &aClient, const Parent &parent, AsyncResult &aResult) { - sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_delete, true); + sendRequest(aClient, &aResult, NULL, "", parent, nullptr, "", FirebaseStorage::fs_delete, true); } /** Delete the object in Firebase Storage data bucket. @@ -361,9 +364,9 @@ class Storage * @param uid The user specified UID of async result (optional). * */ - void deleteObject(AsyncClientClass &aClient, const FirebaseStorage::Parent &parent, AsyncResultCallback cb, const String &uid = "") + void deleteObject(AsyncClientClass &aClient, const Parent &parent, AsyncResultCallback cb, const String &uid = "") { - sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::firebase_storage_request_type_delete, true); + sendRequest(aClient, nullptr, cb, uid, parent, nullptr, "", FirebaseStorage::fs_delete, true); } #if defined(FIREBASE_OTA_STORAGE) @@ -375,13 +378,9 @@ class Storage #endif private: - String service_url; - String path; - String uid; + String service_url, path, uid; // FirebaseApp address and FirebaseApp vector address - uint32_t app_addr = 0, avec_addr = 0; - uint32_t ul_dl_task_running_addr = 0; - uint32_t ota_storage_addr = 0; + uint32_t app_addr = 0, avec_addr = 0, ul_dl_task_running_addr = 0, ota_storage_addr = 0; app_token_t *app_token = nullptr; void url(const String &url) @@ -408,22 +407,20 @@ class Storage return nullptr; } - void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const FirebaseStorage::Parent &parent, file_config_data *file, const String &mime, FirebaseStorage::firebase_storage_request_type requestType, bool async) + void sendRequest(AsyncClientClass &aClient, AsyncResult *result, AsyncResultCallback cb, const String &uid, const Parent &parent, file_config_data *file, const String &mime, FirebaseStorage::firebase_storage_request_type requestType, bool async) { - FirebaseStorage::DataOptions options; + using namespace FirebaseStorage; + + DataOptions options; options.requestType = requestType; options.parent = parent; - async_request_handler_t::http_request_method method = async_request_handler_t::http_post; + reqns::http_request_method method = reqns::http_post; - if (requestType == FirebaseStorage::firebase_storage_request_type_download || - requestType == FirebaseStorage::firebase_storage_request_type_download_ota || - requestType == FirebaseStorage::firebase_storage_request_type_list || - requestType == FirebaseStorage::firebase_storage_request_type_get_meta) + if (requestType == fs_download || requestType == fs_download_ota || requestType == fs_list || requestType == fs_get_meta) { - method = async_request_handler_t::http_get; - if (requestType == FirebaseStorage::firebase_storage_request_type_download || - requestType == FirebaseStorage::firebase_storage_request_type_download_ota) + method = reqns::http_get; + if (requestType == fs_download || requestType == fs_download_ota) { options.extras += "?alt=media"; if (options.parent.getAccessToken().length()) @@ -433,25 +430,25 @@ class Storage } } } - else if (requestType == FirebaseStorage::firebase_storage_request_type_upload || requestType == FirebaseStorage::firebase_storage_request_type_delete) + else if (requestType == fs_upload || requestType == fs_delete) { URLUtil uut; options.extras += "?name="; options.extras += uut.encode(parent.getObject()); - if (requestType == FirebaseStorage::firebase_storage_request_type_delete) - method = async_request_handler_t::http_delete; + if (requestType == fs_delete) + method = reqns::http_delete; } - FirebaseStorage::async_request_data_t aReq(&aClient, path, method, slot_options_t(false, false, async, false, requestType == FirebaseStorage::firebase_storage_request_type_download_ota, false), &options, file, result, cb, uid); + req_data aReq(&aClient, path, method, slot_options_t(false, false, async, false, requestType == fs_download_ota, false), &options, file, result, cb, uid); - if (mime.length() && requestType == FirebaseStorage::firebase_storage_request_type_upload) + if (mime.length() && requestType == fs_upload) aReq.mime = mime; asyncRequest(aReq); } - void asyncRequest(FirebaseStorage::async_request_data_t &request, int beta = 0) + void asyncRequest(FirebaseStorage::req_data &request, int beta = 0) { app_token_t *atoken = appToken(); @@ -465,7 +462,7 @@ class Storage request.path += request.options->parent.getBucketId(); request.path += "/o"; - if (request.method == async_request_handler_t::http_get) + if (request.method == reqns::http_get) { URLUtil uut; request.path += "/"; @@ -476,7 +473,7 @@ class Storage url(FPSTR("firebasestorage.googleapis.com")); - async_data_item_t *sData = request.aClient->createSlot(request.opt); + async_data *sData = request.aClient->createSlot(request.opt); if (!sData) return setClientError(request, FIREBASE_ERROR_OPERATION_CANCELLED); @@ -514,7 +511,7 @@ class Storage } else if (request.options->payload.length()) { - sData->request.val[req_hndlr_ns::payload] = request.options->payload; + sData->request.val[reqns::payload] = request.options->payload; request.aClient->setContentLength(sData, request.options->payload.length()); } @@ -530,7 +527,7 @@ class Storage request.aClient->handleRemove(); } - void setClientError(FirebaseStorage::async_request_data_t &request, int code) + void setClientError(FirebaseStorage::req_data &request, int code) { AsyncResult *aResult = request.aResult; @@ -549,21 +546,19 @@ class Storage } } - void addParams(const FirebaseStorage::async_request_data_t &request, String &extras) + void addParams(const FirebaseStorage::req_data &request, String &extras) { extras += request.options->extras; extras.replace(" ", "%20"); extras.replace(",", "%2C"); } - void setFileStatus(async_data_item_t *sData, const FirebaseStorage::async_request_data_t &request) + void setFileStatus(async_data *sData, const FirebaseStorage::req_data &request) { if ((request.file && (request.file->filename.length() || request.file->data_size)) || request.opt.ota) { - sData->download = request.method == async_request_handler_t::http_get; - sData->upload = request.method == async_request_handler_t::http_post || - request.method == async_request_handler_t::http_put || - request.method == async_request_handler_t::http_patch; + sData->download = request.method == reqns::http_get; + sData->upload = request.method == reqns::http_post || request.method == reqns::http_put || request.method == reqns::http_patch; } } };