|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public struct ListFolder: Sendable { |
| 4 | + public struct Params: Sendable, Equatable, Encodable { |
| 5 | + public init( |
| 6 | + path: String, |
| 7 | + recursive: Bool? = nil, |
| 8 | + includeDeleted: Bool? = nil, |
| 9 | + limit: Int? = nil, |
| 10 | + includeNonDownloadableFiles: Bool? = nil |
| 11 | + ) { |
| 12 | + self.path = path |
| 13 | + self.recursive = recursive |
| 14 | + self.includeDeleted = includeDeleted |
| 15 | + self.limit = limit |
| 16 | + self.includeNonDownloadableFiles = includeNonDownloadableFiles |
| 17 | + } |
| 18 | + |
| 19 | + public var path: String |
| 20 | + public var recursive: Bool? |
| 21 | + public var includeDeleted: Bool? |
| 22 | + public var limit: Int? |
| 23 | + public var includeNonDownloadableFiles: Bool? |
| 24 | + } |
| 25 | + |
| 26 | + public struct Result: Sendable, Equatable, Decodable { |
| 27 | + public init(cursor: String, entries: [Metadata], hasMore: Bool) { |
| 28 | + self.cursor = cursor |
| 29 | + self.entries = entries |
| 30 | + self.hasMore = hasMore |
| 31 | + } |
| 32 | + |
| 33 | + public var cursor: String |
| 34 | + public var entries: [Metadata] |
| 35 | + public var hasMore: Bool |
| 36 | + } |
| 37 | + |
| 38 | + public enum Error: Swift.Error, Sendable, Equatable { |
| 39 | + case notAuthorized |
| 40 | + case response(statusCode: Int?, data: Data) |
| 41 | + } |
| 42 | + |
| 43 | + public typealias Run = @Sendable (Params) async throws -> Result |
| 44 | + |
| 45 | + public init(run: @escaping Run) { |
| 46 | + self.run = run |
| 47 | + } |
| 48 | + |
| 49 | + public var run: Run |
| 50 | + |
| 51 | + public func callAsFunction(_ params: Params) async throws -> Result { |
| 52 | + try await run(params) |
| 53 | + } |
| 54 | + |
| 55 | + public func callAsFunction( |
| 56 | + path: String, |
| 57 | + recursive: Bool? = nil, |
| 58 | + includeDeleted: Bool? = nil, |
| 59 | + limit: Int? = nil, |
| 60 | + includeNonDownloadableFiles: Bool? = nil |
| 61 | + ) async throws -> Result { |
| 62 | + try await run(.init( |
| 63 | + path: path, |
| 64 | + recursive: recursive, |
| 65 | + includeDeleted: includeDeleted, |
| 66 | + limit: limit, |
| 67 | + includeNonDownloadableFiles: includeNonDownloadableFiles |
| 68 | + )) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension ListFolder { |
| 73 | + public static func live( |
| 74 | + keychain: Keychain, |
| 75 | + httpClient: HTTPClient |
| 76 | + ) -> ListFolder { |
| 77 | + ListFolder { params in |
| 78 | + guard let credentials = await keychain.loadCredentials() else { |
| 79 | + throw Error.notAuthorized |
| 80 | + } |
| 81 | + |
| 82 | + let request: URLRequest = try { |
| 83 | + var components = URLComponents() |
| 84 | + components.scheme = "https" |
| 85 | + components.host = "api.dropboxapi.com" |
| 86 | + components.path = "/2/files/list_folder" |
| 87 | + |
| 88 | + var request = URLRequest(url: components.url!) |
| 89 | + request.httpMethod = "POST" |
| 90 | + request.setValue( |
| 91 | + "\(credentials.tokenType) \(credentials.accessToken)", |
| 92 | + forHTTPHeaderField: "Authorization" |
| 93 | + ) |
| 94 | + request.setValue( |
| 95 | + "application/json", |
| 96 | + forHTTPHeaderField: "Content-Type" |
| 97 | + ) |
| 98 | + request.httpBody = try JSONEncoder.api.encode(params) |
| 99 | + |
| 100 | + return request |
| 101 | + }() |
| 102 | + |
| 103 | + let (responseData, response) = try await httpClient.data(for: request) |
| 104 | + let statusCode = (response as? HTTPURLResponse)?.statusCode |
| 105 | + |
| 106 | + guard let statusCode, (200..<300).contains(statusCode) else { |
| 107 | + throw Error.response(statusCode: statusCode, data: responseData) |
| 108 | + } |
| 109 | + |
| 110 | + return try JSONDecoder.api.decode(Result.self, from: responseData) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments