Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 121 additions & 69 deletions BoxSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Sources/Client/BoxClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class BoxClient {
public private(set) lazy var storagePolicies = StoragePoliciesModule(boxClient: self)
/// Provides sign requests functionality.
public private(set) lazy var signRequests = SignRequestsModule(boxClient: self)
/// Provides file requests functionality.
public private(set) lazy var fileRequests = FileRequestsModule(boxClient: self)

/// Provides network communication with the Box APIs.
private var networkAgent: NetworkAgentProtocol
Expand Down
98 changes: 98 additions & 0 deletions Sources/Modules/FileRequestsModule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// FileRequestsModule.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © 2022 box. All rights reserved.
//

import Foundation

/// Provides management of FileRequests
public class FileRequestsModule {
/// Required for communicating with Box APIs.
weak var boxClient: BoxClient!
// swiftlint:disable:previous implicitly_unwrapped_optional

/// Initializer
///
/// - Parameter boxClient: Required for communicating with Box APIs.
init(boxClient: BoxClient) {
self.boxClient = boxClient
}

/// Retrieves the information about a file request by ID.
///
/// - Parameters:
/// - fileRequestId: The unique identifier that represent a file request.
/// - completion: Returns a FileRequest response object if successful otherwise a BoxSDKError.
public func get(
fileRequestId: String,
completion: @escaping Callback<FileRequest>
) {
boxClient.get(
url: URL.boxAPIEndpoint("/2.0/file_requests/\(fileRequestId)", configuration: boxClient.configuration),
completion: ResponseHandler.default(wrapping: completion)
)
}

/// Updates a file request. This can be used to activate or deactivate a file request.
///
/// - Parameters:
/// - fileRequestId: The unique identifier that represent a file request.
/// - ifMatch: Ensures this item hasn't recently changed before making changes.
/// Pass in the item's last observed `etag` value into this header and the endpoint will fail with a `412 Precondition Failed` if it has changed since. (optional)
/// - updateRequest: The `FileRequestUpdateRequest` object which provides the fields that can be updated.
/// - completion: Returns a FileRequest response object if successful otherwise a BoxSDKError.
public func update(
fileRequestId: String,
ifMatch: String? = nil,
updateRequest: FileRequestUpdateRequest,
completion: @escaping Callback<FileRequest>
) {
var headers: BoxHTTPHeaders = [:]
if let unwrappedIfMatch = ifMatch {
headers[BoxHTTPHeaderKey.ifMatch] = unwrappedIfMatch
}

boxClient.put(
url: URL.boxAPIEndpoint("/2.0/file_requests/\(fileRequestId)", configuration: boxClient.configuration),
httpHeaders: headers,
json: updateRequest.bodyDict,
completion: ResponseHandler.default(wrapping: completion)
)
}

/// Copies an existing file request that is already present on one folder, and applies it to another folder.
///
/// - Parameters:
/// - fileRequestId: The unique identifier that represent a file request.
/// - copyRequest: The `FileRequestCopyRequest` object which provides required and optional fields used when copying, like: folder(required), title(optional), etc.
/// - completion: Returns a FileRequest response object if successful otherwise a BoxSDKError.
public func copy(
fileRequestId: String,
copyRequest: FileRequestCopyRequest,
completion: @escaping Callback<FileRequest>
) {
boxClient.post(
url: URL.boxAPIEndpoint("/2.0/file_requests/\(fileRequestId)/copy", configuration: boxClient.configuration),
json: copyRequest.bodyDict,
completion: ResponseHandler.default(wrapping: completion)
)
}

/// Deletes a file request permanently.
///
/// - Parameters:
/// - fileRequestId: The unique identifier that represent a file request.
/// - completion: Returns response object if successful otherwise a BoxSDKError.
public func delete(
fileRequestId: String,
completion: @escaping Callback<Void>
) {
boxClient.delete(
url: URL.boxAPIEndpoint("/2.0/file_requests/\(fileRequestId)", configuration: boxClient.configuration),
completion: ResponseHandler.default(wrapping: completion)
)
}
}
63 changes: 63 additions & 0 deletions Sources/Requests/BodyData/FileRequestCopyRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// FileRequestCopyRequest.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © Box. All rights reserved.
//

import Foundation

// The request body to copy a file request.
public struct FileRequestCopyRequest: Encodable {

// MARK: - Properties

/// An optional new title for the file request.
/// This can be used to change the title of the file request.
public let title: String?
/// An optional new description for the file request.
/// This can be used to change the description of the file request.
public let description: String?
/// An optional new status of the file request.
public let status: FileRequestStatus?
/// 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.
public let isEmailRequired: Bool?
/// Whether a file request submitter is required to provide a description of the files they are submitting.
/// When this setting is set to true, the Box UI will show a description field on the file request form.
public let isDescriptionRequired: Bool?
/// The date after which a file request will no longer accept new submissions.
/// After this date, the `status` will automatically be set to `inactive`.
public let expiresAt: Date?
/// The folder to associate the new file request to.
public let folder: FolderEntity

/// Initializer.
///
/// - Parameters:
/// - title: An optional new title for the file request.
/// - description: An optional new description for the file request.
/// - status: An optional new status of the file request.
/// - isEmailRequired: Whether a file request submitter is required to provide their email address.
/// - isDescriptionRequired: Whether a file request submitter is required to provide a description of the files they are submitting.
/// - expiresAt: The date after which a file request will no longer accept new submissions.
/// - folder: The folder to associate the new file request to.
public init(
title: String? = nil,
description: String? = nil,
status: FileRequestStatus? = nil,
isEmailRequired: Bool? = nil,
isDescriptionRequired: Bool? = nil,
expiresAt: Date? = nil,
folder: FolderEntity
) {
self.title = title
self.description = description
self.status = status
self.isEmailRequired = isEmailRequired
self.isDescriptionRequired = isDescriptionRequired
self.expiresAt = expiresAt
self.folder = folder
}
}
58 changes: 58 additions & 0 deletions Sources/Requests/BodyData/FileRequestUpdateRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// FileRequestUpdateRequest.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © Box. All rights reserved.
//

import Foundation

// The request body to update a file request.
public struct FileRequestUpdateRequest: Encodable {

// MARK: - Properties

/// An optional new title for the file request.
/// This can be used to change the title of the file request.
public let title: String?
/// An optional new description for the file request.
/// This can be used to change the description of the file request.
public let description: String?
/// An optional new status of the file request.
public let status: FileRequestStatus?
/// 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.
public let isEmailRequired: Bool?
/// Whether a file request submitter is required to provide a description of the files they are submitting.
/// When this setting is set to true, the Box UI will show a description field on the file request form.
public let isDescriptionRequired: Bool?
/// The date after which a file request will no longer accept new submissions.
/// After this date, the `status` will automatically be set to `inactive`.
public let expiresAt: Date?

/// Initializer.
///
/// - Parameters:
/// - title: An optional new title for the file request.
/// - description: An optional new description for the file request.
/// - status: An optional new status of the file request.
/// - isEmailRequired: Whether a file request submitter is required to provide their email address.
/// - isDescriptionRequired: Whether a file request submitter is required to provide a description of the files they are submitting.
/// - expiresAt: The date after which a file request will no longer accept new submissions.
public init(
title: String? = nil,
description: String? = nil,
status: FileRequestStatus? = nil,
isEmailRequired: Bool? = nil,
isDescriptionRequired: Bool? = nil,
expiresAt: Date? = nil
) {
self.title = title
self.description = description
self.status = status
self.isEmailRequired = isEmailRequired
self.isDescriptionRequired = isDescriptionRequired
self.expiresAt = expiresAt
}
}
24 changes: 24 additions & 0 deletions Sources/Requests/BodyData/FolderEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// FolderEntity.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © 2022 box. All rights reserved.
//

import Foundation

/// Represents folder resource
public struct FolderEntity: ResourceTypeEntity {
public private(set) var type: String
public private(set) var id: String

/// Initializer.
///
/// - Parameters:
/// - id: Identifier of the folder.
public init(id: String) {
self.id = id
type = "folder"
}
}
18 changes: 18 additions & 0 deletions Sources/Requests/BodyData/ResourceTypeEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// ResourceTypeEntity.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © 2022 box. All rights reserved.
//

import Foundation

/// Represents simple resource type abstraction
public protocol ResourceTypeEntity: Encodable {
/// The resource type of the assiociated object.
var type: String { get }

/// The id of the assiociated object
var id: String { get }
}
116 changes: 116 additions & 0 deletions Sources/Responses/FileRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// FileRequest.swift
// BoxSDK-iOS
//
// Created by Artur Jankowski on 09/08/2022.
// Copyright © Box. All rights reserved.
//

import Foundation

/// The status of the file request.
public enum FileRequestStatus: BoxEnum {
/// The file request can accept new submissions.
case active
/// The file request can't accept new submissions, and any visitor to the file request URL will receive a `HTTP 404` status code.
case inactive
/// Custom value for enum values not yet implemented in the SDK
case customValue(String)

public init(_ value: String) {
switch value {
case "active":
self = .active
case "inactive":
self = .inactive
default:
self = .customValue(value)
}
}

public var description: String {
switch self {
case .active:
return "active"
case .inactive:
return "inactive"
case let .customValue(value):
return value
}
}
}

// A standard representation of a file request, as returned from any file request API endpoints by default.
public final class FileRequest: BoxModel {

// MARK: - BoxModel

private static var resourceType: String = "file_request"
/// Box item type
public var type: String
public private(set) var rawData: [String: Any]

// MARK: - Properties

/// The unique identifier for this file request.
public let id: String
/// The title of file request. This is shown in the Box UI to users uploading files.
public let title: String?
/// The optional description of this file request. This is shown in the Box UI to users uploading files.
public let description: String?
/// The status of the file request.
public let status: FileRequestStatus?
/// 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.
public let isEmailRequired: Bool?
/// Whether a file request submitter is required to provide a description of the files they are submitting.
/// When this setting is set to true, the Box UI will show a description field on the file request form.
public let isDescriptionRequired: Bool?
/// The date after which a file request will no longer accept new submissions. After this date, the `status` will automatically be set to `inactive`.
public let expiresAt: Date?
/// The folder that this file request is associated with. Files submitted through the file request form will be uploaded to this folder.
public let folder: Folder
/// The generated URL for this file request. This URL can be shared with users to let them upload files to the associated folder.
public let url: String?
/// The HTTP `etag` of this file. This can be used in combination with the `If-Match` header when updating a file request.
/// 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.
public let etag: String?
/// The user who created this file request.
public let createdBy: User?
/// The date and time when the file request was created.
public let createdAt: Date
/// The user who last modified this file request.
public let updatedBy: User?
/// The date and time when the file request was last updated.
public let updatedAt: Date

/// Initializer.
///
/// - Parameter json: JSON dictionary.
/// - Throws: Decoding error.
public required init(json: [String: Any]) throws {
guard let itemType = json["type"] as? String else {
throw BoxCodingError(message: .typeMismatch(key: "type"))
}

guard itemType == FileRequest.resourceType else {
throw BoxCodingError(message: .valueMismatch(key: "type", value: itemType, acceptedValues: [FileRequest.resourceType]))
}

type = itemType
rawData = json
id = try BoxJSONDecoder.decode(json: json, forKey: "id")
title = try BoxJSONDecoder.optionalDecode(json: json, forKey: "title")
description = try BoxJSONDecoder.optionalDecode(json: json, forKey: "description")
status = try BoxJSONDecoder.optionalDecodeEnum(json: json, forKey: "status")
isEmailRequired = try BoxJSONDecoder.optionalDecode(json: json, forKey: "is_email_required")
isDescriptionRequired = try BoxJSONDecoder.optionalDecode(json: json, forKey: "is_description_required")
expiresAt = try BoxJSONDecoder.optionalDecodeDate(json: json, forKey: "expires_at")
folder = try BoxJSONDecoder.decode(json: json, forKey: "folder")
url = try BoxJSONDecoder.optionalDecode(json: json, forKey: "url")
etag = try BoxJSONDecoder.optionalDecode(json: json, forKey: "etag")
createdBy = try BoxJSONDecoder.optionalDecode(json: json, forKey: "created_by")
createdAt = try BoxJSONDecoder.decodeDate(json: json, forKey: "created_at")
updatedBy = try BoxJSONDecoder.optionalDecode(json: json, forKey: "updated_by")
updatedAt = try BoxJSONDecoder.decodeDate(json: json, forKey: "updated_at")
}
}
Loading