|
| 1 | +// |
| 2 | +// FileRequest.swift |
| 3 | +// BoxSDK-iOS |
| 4 | +// |
| 5 | +// Created by Artur Jankowski on 09/08/2022. |
| 6 | +// Copyright © Box. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// The status of the file request. |
| 12 | +public enum FileRequestStatus: BoxEnum { |
| 13 | + /// The file request can accept new submissions. |
| 14 | + case active |
| 15 | + /// The file request can't accept new submissions, and any visitor to the file request URL will receive a `HTTP 404` status code. |
| 16 | + case inactive |
| 17 | + /// Custom value for enum values not yet implemented in the SDK |
| 18 | + case customValue(String) |
| 19 | + |
| 20 | + public init(_ value: String) { |
| 21 | + switch value { |
| 22 | + case "active": |
| 23 | + self = .active |
| 24 | + case "inactive": |
| 25 | + self = .inactive |
| 26 | + default: |
| 27 | + self = .customValue(value) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public var description: String { |
| 32 | + switch self { |
| 33 | + case .active: |
| 34 | + return "active" |
| 35 | + case .inactive: |
| 36 | + return "inactive" |
| 37 | + case let .customValue(value): |
| 38 | + return value |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// A standard representation of a file request, as returned from any file request API endpoints by default. |
| 44 | +public final class FileRequest: BoxModel { |
| 45 | + |
| 46 | + // MARK: - BoxModel |
| 47 | + |
| 48 | + private static var resourceType: String = "file_request" |
| 49 | + /// Box item type |
| 50 | + public var type: String |
| 51 | + public private(set) var rawData: [String: Any] |
| 52 | + |
| 53 | + // MARK: - Properties |
| 54 | + |
| 55 | + /// The unique identifier for this file request. |
| 56 | + public let id: String |
| 57 | + /// The title of file request. This is shown in the Box UI to users uploading files. |
| 58 | + public let title: String? |
| 59 | + /// The optional description of this file request. This is shown in the Box UI to users uploading files. |
| 60 | + public let description: String? |
| 61 | + /// The status of the file request. |
| 62 | + public let status: FileRequestStatus? |
| 63 | + /// Whether a file request submitter is required to provide their email address. When this setting is set to true, the Box UI will show an email field on the file request form. |
| 64 | + public let isEmailRequired: Bool? |
| 65 | + /// Whether a file request submitter is required to provide a description of the files they are submitting. |
| 66 | + /// When this setting is set to true, the Box UI will show a description field on the file request form. |
| 67 | + public let isDescriptionRequired: Bool? |
| 68 | + /// The date after which a file request will no longer accept new submissions. After this date, the `status` will automatically be set to `inactive`. |
| 69 | + public let expiresAt: Date? |
| 70 | + /// The folder that this file request is associated with. Files submitted through the file request form will be uploaded to this folder. |
| 71 | + public let folder: Folder |
| 72 | + /// The generated URL for this file request. This URL can be shared with users to let them upload files to the associated folder. |
| 73 | + public let url: String? |
| 74 | + /// The HTTP `etag` of this file. This can be used in combination with the `If-Match` header when updating a file request. |
| 75 | + /// By providing that header, a change will only be performed on the file request if the `etag` on the file request still matches the `etag` provided in the `If-Match` header. |
| 76 | + public let etag: String? |
| 77 | + /// The user who created this file request. |
| 78 | + public let createdBy: User? |
| 79 | + /// The date and time when the file request was created. |
| 80 | + public let createdAt: Date |
| 81 | + /// The user who last modified this file request. |
| 82 | + public let updatedBy: User? |
| 83 | + /// The date and time when the file request was last updated. |
| 84 | + public let updatedAt: Date |
| 85 | + |
| 86 | + /// Initializer. |
| 87 | + /// |
| 88 | + /// - Parameter json: JSON dictionary. |
| 89 | + /// - Throws: Decoding error. |
| 90 | + public required init(json: [String: Any]) throws { |
| 91 | + guard let itemType = json["type"] as? String else { |
| 92 | + throw BoxCodingError(message: .typeMismatch(key: "type")) |
| 93 | + } |
| 94 | + |
| 95 | + guard itemType == FileRequest.resourceType else { |
| 96 | + throw BoxCodingError(message: .valueMismatch(key: "type", value: itemType, acceptedValues: [FileRequest.resourceType])) |
| 97 | + } |
| 98 | + |
| 99 | + type = itemType |
| 100 | + rawData = json |
| 101 | + id = try BoxJSONDecoder.decode(json: json, forKey: "id") |
| 102 | + title = try BoxJSONDecoder.optionalDecode(json: json, forKey: "title") |
| 103 | + description = try BoxJSONDecoder.optionalDecode(json: json, forKey: "description") |
| 104 | + status = try BoxJSONDecoder.optionalDecodeEnum(json: json, forKey: "status") |
| 105 | + isEmailRequired = try BoxJSONDecoder.optionalDecode(json: json, forKey: "is_email_required") |
| 106 | + isDescriptionRequired = try BoxJSONDecoder.optionalDecode(json: json, forKey: "is_description_required") |
| 107 | + expiresAt = try BoxJSONDecoder.optionalDecodeDate(json: json, forKey: "expires_at") |
| 108 | + folder = try BoxJSONDecoder.decode(json: json, forKey: "folder") |
| 109 | + url = try BoxJSONDecoder.optionalDecode(json: json, forKey: "url") |
| 110 | + etag = try BoxJSONDecoder.optionalDecode(json: json, forKey: "etag") |
| 111 | + createdBy = try BoxJSONDecoder.optionalDecode(json: json, forKey: "created_by") |
| 112 | + createdAt = try BoxJSONDecoder.decodeDate(json: json, forKey: "created_at") |
| 113 | + updatedBy = try BoxJSONDecoder.optionalDecode(json: json, forKey: "updated_by") |
| 114 | + updatedAt = try BoxJSONDecoder.decodeDate(json: json, forKey: "updated_at") |
| 115 | + } |
| 116 | +} |
0 commit comments