NCNetworking+AsyncAwait.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, error: NKError) {
  40. await withUnsafeContinuation({ continuation in
  41. NextcloudKit.shared.setLivephoto(serverUrlfileNamePath: serverUrlfileNamePath, livePhotoFile: livePhotoFile, account: account, options: options) { account, error in
  42. continuation.resume(returning: (account: account, error: error))
  43. }
  44. })
  45. }
  46. func getUserProfile(account: String,
  47. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, userProfile: NKUserProfile?, data: Data?, error: NKError) {
  48. await withUnsafeContinuation({ continuation in
  49. NextcloudKit.shared.getUserProfile(account: account, options: options) { account, userProfile, data, error in
  50. continuation.resume(returning: (account: account, userProfile: userProfile, data: data, error: error))
  51. }
  52. })
  53. }
  54. func sendClientDiagnosticsRemoteOperation(data: Data,
  55. account: String,
  56. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  57. await withUnsafeContinuation({ continuation in
  58. NextcloudKit.shared.sendClientDiagnosticsRemoteOperation(data: data, account: account, options: options) { account, error in
  59. continuation.resume(returning: (account: account, error: error))
  60. }
  61. })
  62. }
  63. func downloadPreview(url: URL,
  64. account: String,
  65. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, data: Data?, error: NKError) {
  66. await withUnsafeContinuation({ continuation in
  67. NextcloudKit.shared.downloadPreview(url: url, account: account, options: options) { account, data, error in
  68. continuation.resume(returning: (account: account, data: data, error: error))
  69. }
  70. })
  71. }
  72. func downloadPreview(fileId: String,
  73. fileNamePreviewLocalPath: String,
  74. fileNameIconLocalPath: String? = nil,
  75. widthPreview: Int = 512,
  76. heightPreview: Int = 512,
  77. sizeIcon: Int = 512,
  78. etag: String? = nil,
  79. account: String,
  80. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, imagePreview: UIImage?, imageIcon: UIImage?, imageOriginal: UIImage?, etag: String?, error: NKError) {
  81. await withUnsafeContinuation({ continuation in
  82. NextcloudKit.shared.downloadPreview(fileId: fileId, fileNamePreviewLocalPath: fileNamePreviewLocalPath, fileNameIconLocalPath: fileNameIconLocalPath, widthPreview: widthPreview, heightPreview: heightPreview, sizeIcon: sizeIcon, etag: etag, account: account, options: options) { account, imagePreview, imageIcon, imageOriginal, etag, error in
  83. continuation.resume(returning: (account: account, imagePreview: imagePreview, imageIcon: imageIcon, imageOriginal: imageOriginal, etag: etag, error: error))
  84. }
  85. })
  86. }
  87. func deleteFileOrFolder(serverUrlFileName: String,
  88. account: String,
  89. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  90. await withUnsafeContinuation({ continuation in
  91. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: account, options: options) { account, error in
  92. continuation.resume(returning: (account: account, error: error))
  93. }
  94. })
  95. }
  96. func moveFileOrFolder(serverUrlFileNameSource: String,
  97. serverUrlFileNameDestination: String,
  98. overwrite: Bool,
  99. account: String,
  100. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  101. await withUnsafeContinuation({ continuation in
  102. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: overwrite, account: account, options: options) { account, error in
  103. continuation.resume(returning: (account: account, error: error))
  104. }
  105. })
  106. }
  107. func copyFileOrFolder(serverUrlFileNameSource: String,
  108. serverUrlFileNameDestination: String,
  109. overwrite: Bool,
  110. account: String,
  111. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  112. await withUnsafeContinuation({ continuation in
  113. NextcloudKit.shared.copyFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: overwrite, account: account, options: options) { account, error in
  114. continuation.resume(returning: (account: account, error: error))
  115. }
  116. })
  117. }
  118. func createFolder(serverUrlFileName: String,
  119. account: String,
  120. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, ocId: String?, date: Date?, error: NKError) {
  121. await withUnsafeContinuation({ continuation in
  122. NextcloudKit.shared.createFolder(serverUrlFileName: serverUrlFileName, account: account, options: options) { account, ocId, date, error in
  123. continuation.resume(returning: (account: account, ocId: ocId, date: date, error: error))
  124. }
  125. })
  126. }
  127. func readFileOrFolder(serverUrlFileName: String,
  128. depth: String,
  129. showHiddenFiles: Bool = true,
  130. requestBody: Data? = nil,
  131. account: String,
  132. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, files: [NKFile], data: Data?, error: NKError) {
  133. await withUnsafeContinuation({ continuation in
  134. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: depth, showHiddenFiles: showHiddenFiles, requestBody: requestBody, account: account, options: options) { account, files, data, error in
  135. continuation.resume(returning: (account: account, files: files, data: data, error: error))
  136. }
  137. })
  138. }
  139. func searchMedia(path: String = "",
  140. lessDate: Any,
  141. greaterDate: Any,
  142. elementDate: String,
  143. limit: Int,
  144. showHiddenFiles: Bool,
  145. includeHiddenFiles: [String] = [],
  146. account: String,
  147. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, files: [NKFile], data: Data?, error: NKError) {
  148. await withUnsafeContinuation({ continuation in
  149. NextcloudKit.shared.searchMedia(path: path, lessDate: lessDate, greaterDate: greaterDate, elementDate: elementDate, limit: limit, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, account: account, options: options) { account, files, data, error in
  150. continuation.resume(returning: (account, files, data, error))
  151. }
  152. })
  153. }
  154. func markE2EEFolder(fileId: String,
  155. delete: Bool,
  156. account: String,
  157. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  158. await withUnsafeContinuation({ continuation in
  159. NextcloudKit.shared.markE2EEFolder(fileId: fileId, delete: delete, account: account, options: options) { account, error in
  160. continuation.resume(returning: (account: account, error: error))
  161. }
  162. })
  163. }
  164. func lockE2EEFolder(fileId: String,
  165. e2eToken: String?,
  166. e2eCounter: String?,
  167. method: String,
  168. account: String,
  169. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, e2eToken: String?, data: Data?, error: NKError) {
  170. await withUnsafeContinuation({ continuation in
  171. NextcloudKit.shared.lockE2EEFolder(fileId: fileId, e2eToken: e2eToken, e2eCounter: e2eCounter, method: method, account: account, options: options) { account, e2eToken, data, error in
  172. continuation.resume(returning: (account: account, e2eToken: e2eToken, data: data, error: error))
  173. }
  174. })
  175. }
  176. func getE2EEMetadata(fileId: String,
  177. e2eToken: String?,
  178. account: String,
  179. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, e2eMetadata: String?, signature: String?, data: Data?, error: NKError) {
  180. await withUnsafeContinuation({ continuation in
  181. NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken, account: account, options: options) { account, e2eMetadata, signature, data, error in
  182. continuation.resume(returning: (account: account, e2eMetadata: e2eMetadata, signature: signature, data: data, error: error))
  183. }
  184. })
  185. }
  186. func putE2EEMetadata(fileId: String,
  187. e2eToken: String,
  188. e2eMetadata: String?,
  189. signature: String?,
  190. method: String,
  191. account: String,
  192. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, metadata: String?, data: Data?, error: NKError) {
  193. await withUnsafeContinuation({ continuation in
  194. NextcloudKit.shared.putE2EEMetadata(fileId: fileId, e2eToken: e2eToken, e2eMetadata: e2eMetadata, signature: signature, method: method, account: account, options: options) { account, metadata, data, error in
  195. continuation.resume(returning: (account: account, metadata: metadata, data: data, error: error))
  196. }
  197. })
  198. }
  199. func getE2EECertificate(user: String? = nil,
  200. account: String,
  201. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, certificate: String?, certificateUser: String?, data: Data?, error: NKError) {
  202. await withUnsafeContinuation({ continuation in
  203. NextcloudKit.shared.getE2EECertificate(user: user, account: account, options: options) { account, certificate, certificateUser, data, error in
  204. continuation.resume(returning: (account: account, certificate: certificate, certificateUser: certificateUser, data: data, error: error))
  205. }
  206. })
  207. }
  208. func getE2EEPrivateKey(account: String,
  209. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, privateKey: String?, data: Data?, error: NKError) {
  210. await withUnsafeContinuation({ continuation in
  211. NextcloudKit.shared.getE2EEPrivateKey(account: account, options: options) { account, privateKey, data, error in
  212. continuation.resume(returning: (account: account, privateKey: privateKey, data: data, error: error))
  213. }
  214. })
  215. }
  216. func getE2EEPublicKey(account: String,
  217. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, publicKey: String?, data: Data?, error: NKError) {
  218. await withUnsafeContinuation({ continuation in
  219. NextcloudKit.shared.getE2EEPublicKey(account: account, options: options) { account, publicKey, data, error in
  220. continuation.resume(returning: (account: account, publicKey: publicKey, data: data, error: error))
  221. }
  222. })
  223. }
  224. func signE2EECertificate(certificate: String,
  225. account: String,
  226. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, certificate: String?, data: Data?, error: NKError) {
  227. await withUnsafeContinuation({ continuation in
  228. NextcloudKit.shared.signE2EECertificate(certificate: certificate, account: account, options: options) { account, certificate, data, error in
  229. continuation.resume(returning: (account: account, certificate: certificate, data: data, error: error))
  230. }
  231. })
  232. }
  233. func storeE2EEPrivateKey(privateKey: String,
  234. account: String,
  235. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, privateKey: String?, data: Data?, error: NKError) {
  236. await withUnsafeContinuation({ continuation in
  237. NextcloudKit.shared.storeE2EEPrivateKey(privateKey: privateKey, account: account, options: options) { account, privateKey, data, error in
  238. continuation.resume(returning: (account: account, privateKey: privateKey, data: data, error: error))
  239. }
  240. })
  241. }
  242. func deleteE2EECertificate(account: String,
  243. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  244. await withUnsafeContinuation({ continuation in
  245. NextcloudKit.shared.deleteE2EECertificate(account: account, options: options) { account, error in
  246. continuation.resume(returning: (account: account, error: error))
  247. }
  248. })
  249. }
  250. func deleteE2EEPrivateKey(account: String,
  251. options: NKRequestOptions = NKRequestOptions()) async -> (account: String, error: NKError) {
  252. await withUnsafeContinuation({ continuation in
  253. NextcloudKit.shared.deleteE2EEPrivateKey(account: account, options: options) { account, error in
  254. continuation.resume(returning: (account: account, error: error))
  255. }
  256. })
  257. }
  258. }