NCNetworking+AsyncAwait.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // NCNetworking+AsyncAwait.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/06/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. import NextcloudKit
  26. import Alamofire
  27. extension NCNetworking {
  28. func getServerStatus(serverUrl: String,
  29. options: NKRequestOptions = NKRequestOptions()) async -> NextcloudKit.ServerInfoResult {
  30. await withUnsafeContinuation({ continuation in
  31. NextcloudKit.shared.getServerStatus(serverUrl: serverUrl) { _, serverInfoResult in
  32. continuation.resume(returning: serverInfoResult)
  33. }
  34. })
  35. }
  36. func setLivephoto(serverUrlfileNamePath: String,
  37. livePhotoFile: String,
  38. account: String,
  39. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  40. await withUnsafeContinuation({ continuation in
  41. NextcloudKit.shared.setLivephoto(serverUrlfileNamePath: serverUrlfileNamePath, livePhotoFile: livePhotoFile, account: account, options: options) { account, responseData, error in
  42. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  43. }
  44. })
  45. }
  46. func getUserProfile(account: String,
  47. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, userProfile: NKUserProfile?, responseData: AFDataResponse<Data>?, error: NKError) {
  48. await withUnsafeContinuation({ continuation in
  49. NextcloudKit.shared.getUserProfile(account: account, options: options) { account, userProfile, responseData, error in
  50. continuation.resume(returning: (account: account, userProfile: userProfile, responseData: responseData, error: error))
  51. }
  52. })
  53. }
  54. func sendClientDiagnosticsRemoteOperation(data: Data,
  55. account: String,
  56. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  57. await withUnsafeContinuation({ continuation in
  58. NextcloudKit.shared.sendClientDiagnosticsRemoteOperation(data: data, account: account, options: options) { account, responseData, error in
  59. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  60. }
  61. })
  62. }
  63. func downloadPreview(url: URL,
  64. account: String,
  65. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  66. await withUnsafeContinuation({ continuation in
  67. NextcloudKit.shared.downloadPreview(url: url, account: account, options: options) { account, responseData, error in
  68. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  69. }
  70. })
  71. }
  72. func downloadPreview(fileId: String,
  73. etag: String? = nil,
  74. account: String,
  75. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, width: Int, height: Int, etag: String?, responseData: AFDataResponse<Data?>?, error: NKError) {
  76. await withUnsafeContinuation({ continuation in
  77. NextcloudKit.shared.downloadPreview(fileId: fileId, etag: etag, account: account, options: options) { account, width, height, etag, responseData, error in
  78. continuation.resume(returning: (account: account, width: width, height: height, etag: etag, responseData: responseData, error: error))
  79. }
  80. })
  81. }
  82. func deleteFileOrFolder(serverUrlFileName: String,
  83. account: String,
  84. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  85. await withUnsafeContinuation({ continuation in
  86. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: account, options: options) { account, responseData, error in
  87. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  88. }
  89. })
  90. }
  91. func moveFileOrFolder(serverUrlFileNameSource: String,
  92. serverUrlFileNameDestination: String,
  93. overwrite: Bool,
  94. account: String,
  95. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  96. await withUnsafeContinuation({ continuation in
  97. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: overwrite, account: account, options: options) { account, responseData, error in
  98. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  99. }
  100. })
  101. }
  102. func copyFileOrFolder(serverUrlFileNameSource: String,
  103. serverUrlFileNameDestination: String,
  104. overwrite: Bool,
  105. account: String,
  106. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  107. await withUnsafeContinuation({ continuation in
  108. NextcloudKit.shared.copyFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: overwrite, account: account, options: options) { account, responseData, error in
  109. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  110. }
  111. })
  112. }
  113. func createFolder(serverUrlFileName: String,
  114. account: String,
  115. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, ocId: String?, date: Date?, responseData: AFDataResponse<Data?>?, error: NKError) {
  116. await withUnsafeContinuation({ continuation in
  117. NextcloudKit.shared.createFolder(serverUrlFileName: serverUrlFileName, account: account, options: options) { account, ocId, date, responseData, error in
  118. continuation.resume(returning: (account: account, ocId: ocId, date: date, responseData: responseData, error: error))
  119. }
  120. })
  121. }
  122. func createFolder(metadata: tableMetadata) async -> NKError {
  123. await withUnsafeContinuation({ continuation in
  124. self.createFolder(fileName: metadata.fileName, serverUrl: metadata.serverUrl, overwrite: true, withPush: false, metadata: metadata, sceneIdentifier: nil, session: NCSession.shared.getSession(account: metadata.account)) { error in
  125. continuation.resume(returning: error)
  126. }
  127. })
  128. }
  129. func setFavorite(fileName: String, favorite: Bool, account: String) async -> NKError {
  130. await withUnsafeContinuation({ continuation in
  131. NextcloudKit.shared.setFavorite(fileName: fileName, favorite: favorite, account: account) { _, _, error in
  132. continuation.resume(returning: error)
  133. }
  134. })
  135. }
  136. func readFileOrFolder(serverUrlFileName: String,
  137. depth: String,
  138. showHiddenFiles: Bool = true,
  139. requestBody: Data? = nil,
  140. account: String,
  141. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, files: [NKFile]?, responseData: AFDataResponse<Data>?, error: NKError) {
  142. await withUnsafeContinuation({ continuation in
  143. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: depth, showHiddenFiles: showHiddenFiles, requestBody: requestBody, account: account, options: options) { account, files, responseData, error in
  144. continuation.resume(returning: (account: account, files: files, responseData: responseData, error: error))
  145. }
  146. })
  147. }
  148. func searchMedia(path: String = "",
  149. lessDate: Any,
  150. greaterDate: Any,
  151. elementDate: String,
  152. limit: Int,
  153. showHiddenFiles: Bool,
  154. account: String,
  155. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, files: [NKFile]?, responseData: AFDataResponse<Data>?, error: NKError) {
  156. await withUnsafeContinuation({ continuation in
  157. NextcloudKit.shared.searchMedia(path: path, lessDate: lessDate, greaterDate: greaterDate, elementDate: elementDate, limit: limit, showHiddenFiles: showHiddenFiles, account: account, options: options) { account, files, responseData, error in
  158. continuation.resume(returning: (account, files, responseData, error))
  159. }
  160. })
  161. }
  162. func markE2EEFolder(fileId: String,
  163. delete: Bool,
  164. account: String,
  165. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data>?, error: NKError) {
  166. await withUnsafeContinuation({ continuation in
  167. NextcloudKit.shared.markE2EEFolder(fileId: fileId, delete: delete, account: account, options: options) { account, responseData, error in
  168. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  169. }
  170. })
  171. }
  172. func lockE2EEFolder(fileId: String,
  173. e2eToken: String?,
  174. e2eCounter: String?,
  175. method: String,
  176. account: String,
  177. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, e2eToken: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  178. await withUnsafeContinuation({ continuation in
  179. NextcloudKit.shared.lockE2EEFolder(fileId: fileId, e2eToken: e2eToken, e2eCounter: e2eCounter, method: method, account: account, options: options) { account, e2eToken, responseData, error in
  180. continuation.resume(returning: (account: account, e2eToken: e2eToken, responseData: responseData, error: error))
  181. }
  182. })
  183. }
  184. func getE2EEMetadata(fileId: String,
  185. e2eToken: String?,
  186. account: String,
  187. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, e2eMetadata: String?, signature: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  188. await withUnsafeContinuation({ continuation in
  189. NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken, account: account, options: options) { account, e2eMetadata, signature, responseData, error in
  190. continuation.resume(returning: (account: account, e2eMetadata: e2eMetadata, signature: signature, responseData: responseData, error: error))
  191. }
  192. })
  193. }
  194. func putE2EEMetadata(fileId: String,
  195. e2eToken: String,
  196. e2eMetadata: String?,
  197. signature: String?,
  198. method: String,
  199. account: String,
  200. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, metadata: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  201. await withUnsafeContinuation({ continuation in
  202. NextcloudKit.shared.putE2EEMetadata(fileId: fileId, e2eToken: e2eToken, e2eMetadata: e2eMetadata, signature: signature, method: method, account: account, options: options) { account, metadata, responseData, error in
  203. continuation.resume(returning: (account: account, metadata: metadata, responseData: responseData, error: error))
  204. }
  205. })
  206. }
  207. func getE2EECertificate(user: String? = nil,
  208. account: String,
  209. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, certificate: String?, certificateUser: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  210. await withUnsafeContinuation({ continuation in
  211. NextcloudKit.shared.getE2EECertificate(user: user, account: account, options: options) { account, certificate, certificateUser, responseData, error in
  212. continuation.resume(returning: (account: account, certificate: certificate, certificateUser: certificateUser, responseData: responseData, error: error))
  213. }
  214. })
  215. }
  216. func getE2EEPrivateKey(account: String,
  217. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, privateKey: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  218. await withUnsafeContinuation({ continuation in
  219. NextcloudKit.shared.getE2EEPrivateKey(account: account, options: options) { account, privateKey, responseData, error in
  220. continuation.resume(returning: (account: account, privateKey: privateKey, responseData: responseData, error: error))
  221. }
  222. })
  223. }
  224. func getE2EEPublicKey(account: String,
  225. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, publicKey: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  226. await withUnsafeContinuation({ continuation in
  227. NextcloudKit.shared.getE2EEPublicKey(account: account, options: options) { account, publicKey, responseData, error in
  228. continuation.resume(returning: (account: account, publicKey: publicKey, responseData: responseData, error: error))
  229. }
  230. })
  231. }
  232. func signE2EECertificate(certificate: String,
  233. account: String,
  234. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, certificate: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  235. await withUnsafeContinuation({ continuation in
  236. NextcloudKit.shared.signE2EECertificate(certificate: certificate, account: account, options: options) { account, certificate, responseData, error in
  237. continuation.resume(returning: (account: account, certificate: certificate, responseData: responseData, error: error))
  238. }
  239. })
  240. }
  241. func storeE2EEPrivateKey(privateKey: String,
  242. account: String,
  243. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, privateKey: String?, responseData: AFDataResponse<Data>?, error: NKError) {
  244. await withUnsafeContinuation({ continuation in
  245. NextcloudKit.shared.storeE2EEPrivateKey(privateKey: privateKey, account: account, options: options) { account, privateKey, responseData, error in
  246. continuation.resume(returning: (account: account, privateKey: privateKey, responseData: responseData, error: error))
  247. }
  248. })
  249. }
  250. func deleteE2EECertificate(account: String,
  251. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  252. await withUnsafeContinuation({ continuation in
  253. NextcloudKit.shared.deleteE2EECertificate(account: account, options: options) { account, responseData, error in
  254. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  255. }
  256. })
  257. }
  258. func deleteE2EEPrivateKey(account: String,
  259. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data?>?, error: NKError) {
  260. await withUnsafeContinuation({ continuation in
  261. NextcloudKit.shared.deleteE2EEPrivateKey(account: account, options: options) { account, responseData, error in
  262. continuation.resume(returning: (account: account, responseData: responseData, error: error))
  263. }
  264. })
  265. }
  266. }