diff --git a/Package.swift b/Package.swift index 5cbe837..e943c31 100644 --- a/Package.swift +++ b/Package.swift @@ -42,7 +42,7 @@ let package = Package( .copy("Resources/processBankStatement.json"), .copy("Resources/addTag.json"), .copy("Resources/addTags.json"), - .copy("Resources/deleteTags.json"), + .copy("Resources/replaceTags.json"), .copy("Resources/updateDocument.json"), .copy("Resources/addLineItem.json"), .copy("Resources/deleteDocumentLineItems.json"), diff --git a/Sources/VeryfiSDK/Client.swift b/Sources/VeryfiSDK/Client.swift deleted file mode 100644 index cf8ee55..0000000 --- a/Sources/VeryfiSDK/Client.swift +++ /dev/null @@ -1,367 +0,0 @@ -import Foundation -#if canImport(FoundationNetworking) -import FoundationNetworking -#endif - -struct VeryfiCredentials { - let clientId: String - let clientSecret: String - let username: String - let apiKey: String -} - -public class Client: NetworkManager { - - /// Init Client. - /// - Parameters: - /// - clientId: Your client id from veryfi-hub. - /// - clientSecret: Your client secret from veryfi-hub. - /// - username: Your username from veryfi-hub. - /// - apiKey: Your api key from veryfi-hub. - /// - apiVersion: Api version to use, by default "v8". - public init(clientId: String, clientSecret: String, username: String, apiKey: String, apiVersion: String = "v8") { - let credentials = VeryfiCredentials(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey) - super.init(credentials: credentials, apiVersion: apiVersion) - } - - /// Get all documents from Veryfi inbox. - /// - Parameters: - /// - queryItems: Query items to apply to the get request. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getDocuments(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .documents, queryItems: queryItems, completion: completion) - } - - /// Get single document by ID from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to retreive - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .documents, queryItem: documentId, completion: completion) - } - - /// Upload an image for the Veryfi API to process. - /// - Parameters: - /// - fileName: Name of the file to upload to the Veryfi API. - /// - fileData: UTF8 encoded file data - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - params: Additional parameters. - /// - completion: Function called after request completes. - /// - detail: Response from server. - /// - error: Error from server. - public func processDocument(fileName: String, - fileData: Data, - categories: [String]? = nil, - deleteAfterProcessing: Bool = false, - params: [String: Any]? = nil, - withCompletion completion: @escaping (Result) -> Void) { - let requestCats = categories ?? [] - var requestParams = params ?? [String: Any]() - requestParams["categories"] = requestCats - requestParams["auto_delete"] = deleteAfterProcessing - requestParams["file_data"] = fileData.base64EncodedString() - requestParams["file_name"] = fileName - - guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { - completion(.failure(.parsingError)) - return - } - - self.request(method: .POST, route: .documents, uploadData: jsonData, completion: completion) - } - - /// Upload document to Veryfi API with URL. - /// - Parameters: - /// - fileUrl: Publicly available URL. - /// - fileUrls: List of publicly available URLs. - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - boostMode: Skip data enrichment but process document faster. - /// - externalId: Existing ID to assign to document. - /// - maxPagesToProcess: Number of pages to process. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func processDocumentURL(fileUrl: String? = nil, - fileUrls: [String]? = nil, - categories: [String]? = nil, - deleteAfterProcessing: Bool = false, - boostMode: Int = 0, - externalId: String? = nil, - maxPagesToProcess: Int? = 1, - withCompletion completion: @escaping (Result) -> Void) { - let params: [String: Any] = ["auto_delete": deleteAfterProcessing, - "boost_mode": boostMode, - "categories": categories ?? [], - "external_id": externalId as Any, //implicit coerce - "file_url": fileUrl as Any, //implicit coerce - "file_urls": fileUrls as Any, //implicit coerce - "max_pages_to_process": maxPagesToProcess as Any] //implicit coerce - let jsonData = try? JSONSerialization.data(withJSONObject: params) - self.request(method: .POST, route: .documents, body: jsonData, completion: completion) - } - - /// Get all any documents from Veryfi inbox. - /// - Parameters: - /// - queryItems: Query items to apply to the get request. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getAnyDocuments(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .anyDocuments, queryItems: queryItems, completion: completion) - } - - /// Get single any document by ID from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to retreive - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getAnyDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .anyDocuments, queryItem: documentId, completion: completion) - } - - /// Upload any document for the Veryfi API to process. - /// - Parameters: - /// - fileName: Name of the file to upload to the Veryfi API. - /// - fileData: UTF8 encoded file data - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - params: Additional parameters. - /// - completion: Function called after request completes. - /// - detail: Response from server. - /// - error: Error from server. - public func processAnyDocument(fileName: String, - fileData: Data, - params: [String: Any]? = nil, - templateName: String, - withCompletion completion: @escaping (Result) -> Void) { - var requestParams = params ?? [String: Any]() - requestParams["file_data"] = fileData.base64EncodedString() - requestParams["file_name"] = fileName - requestParams["template_name"] = templateName - - guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { - completion(.failure(.parsingError)) - return - } - - self.request(method: .POST, route: .anyDocuments, uploadData: jsonData, completion: completion) - } - - /// Upload any document to Veryfi API with URL. - /// - Parameters: - /// - fileUrl: Publicly available URL. - /// - fileUrls: List of publicly available URLs. - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - boostMode: Skip data enrichment but process document faster. - /// - externalId: Existing ID to assign to document. - /// - maxPagesToProcess: Number of pages to process. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func processAnyDocumentURL(fileUrl: String? = nil, - fileUrls: [String]? = nil, - categories: [String]? = nil, - deleteAfterProcessing: Bool = false, - boostMode: Int = 0, - externalId: String? = nil, - maxPagesToProcess: Int? = 1, - templateName: String, - withCompletion completion: @escaping (Result) -> Void) { - let params: [String: Any] = ["file_url": fileUrl as Any, //implicit coerce - "file_urls": fileUrls as Any, //implicit coerce - "template_name": templateName, - "max_pages_to_process": maxPagesToProcess as Any] //implicit coerce - let jsonData = try? JSONSerialization.data(withJSONObject: params) - self.request(method: .POST, route: .anyDocuments, body: jsonData, completion: completion) - } - - /// Get all bank statements from Veryfi inbox. - /// - Parameters: - /// - queryItems: Query items to apply to the get request. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getBankStatements(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .bankStatements, queryItems: queryItems, completion: completion) - } - - /// Get single bank statements by ID from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to retreive - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getBankStatement(id: String, queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .bankStatements, queryItems: queryItems, queryItem: id, completion: completion) - } - - /// Upload a bank statement for the Veryfi API to process. - /// - Parameters: - /// - fileName: Name of the file to upload to the Veryfi API. - /// - fileData: UTF8 encoded file data - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - params: Additional parameters. - /// - completion: Function called after request completes. - /// - detail: Response from server. - /// - error: Error from server. - public func processBankStatement(fileName: String, - fileData: Data, - params: [String: Any]? = nil, - confidenceDetails: Bool = false, - withCompletion completion: @escaping (Result) -> Void) { - var requestParams = params ?? [String: Any]() - requestParams["confidence_details"] = confidenceDetails - requestParams["file_data"] = fileData.base64EncodedString() - requestParams["file_name"] = fileName - - guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { - completion(.failure(.parsingError)) - return - } - - self.request(method: .POST, route: .bankStatements, uploadData: jsonData, completion: completion) - } - - /// Upload bank statement to Veryfi API with URL. - /// - Parameters: - /// - fileUrl: Publicly available URL. - /// - fileUrls: List of publicly available URLs. - /// - categories: List of document categories. - /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. - /// - boostMode: Skip data enrichment but process document faster. - /// - externalId: Existing ID to assign to document. - /// - maxPagesToProcess: Number of pages to process. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func processBankStatementURL(fileUrl: String? = nil, - fileUrls: [String]? = nil, - boundingBoxes: Bool = false, - confidenceDetails: Bool = false, - withCompletion completion: @escaping (Result) -> Void) { - let params: [String: Any] = [ - "bounding_boxes": boundingBoxes, - "confidence_details": confidenceDetails, - "file_url": fileUrl as Any, //implicit coerce - "file_urls": fileUrls as Any, //implicit coerce - ] //implicit coerce - let jsonData = try? JSONSerialization.data(withJSONObject: params) - self.request(method: .POST, route: .bankStatements, body: jsonData, completion: completion) - } - - /// Add tag to document. - /// - Parameters: - /// - documentId: ID of document to add tag. - /// - params: Tag data. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func addTag(documentId: String, params: AddTag, withCompletion completion: @escaping (Result) -> Void) { - let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) - self.request(method: .PUT, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion) - } - - /// Add multiple tags in document. - /// - Parameters: - /// - documentId: ID of document to replace tags. - /// - params: Tags data. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func addTags(documentId: String, params: AddTags, withCompletion completion: @escaping (Result) -> Void) { - let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) - self.request(method: .POST, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion) - } - - /// Update information of document in Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to modify. - /// - params: Names and values to modify. - /// - completion: A block to execute - /// - detail: Response from server. - /// - error: Error from server. - public func updateDocument(documentId: String, params: [String: Any], withCompletion completion: @escaping (Result) -> Void) { - let jsonData = try? JSONSerialization.data(withJSONObject: params) - self.request(method: .PUT, route: .documents, body: jsonData, queryItem: documentId, completion: completion) - } - - /// Delete document from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to delete. - /// - completion: completion description - public func deleteDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .DELETE, route: .documents, queryItem: documentId, completion: completion) - } - - /// Get all line items from document. - /// - Parameters: - /// - documentId: ID of document to get line items. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getDocumentLineItems(documentId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion) - } - - /// Get single line item by document from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document. - /// - lineItemId: ID of line item. - /// - completion: Block executed after request. - /// - detail: Response from server. - /// - error: Error from server. - public func getLineItem(documentId: String, lineItemId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) - } - - /// Create line item for document in Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document to modify. - /// - params: Line item data. - /// - completion: A block to execute - /// - detail: Response from server. - /// - error: Error from server. - public func addLineItem(documentId: String, params: AddLineItem, withCompletion completion: @escaping (Result) -> Void) { - let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) - self.request(method: .POST, route: .documents, body: jsonData, queryItem: String(format: "%@/line-items", documentId), completion: completion) - } - - /// Update line item for document in Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document. - /// - lineItemId: ID of line item to modify. - /// - params: Line item data. - /// - completion: A block to execute - /// - detail: Response from server. - /// - error: Error from server. - public func updateLineItem(documentId: String, lineItemId: String, params: UpdateLineItem, withCompletion completion: @escaping (Result) -> Void) { - let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) - self.request(method: .PUT, route: .documents, body: jsonData, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) - } - - /// Delete all line items from document from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document - /// - completion: completion description - public func deleteDocumentLineItems(documentId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion) - } - - /// Delete line item from document from Veryfi inbox. - /// - Parameters: - /// - documentId: ID of document - /// - lineItemId: ID of line item to delete. - /// - completion: completion description - public func deleteLineItem(documentId: String, lineItemId: String, withCompletion completion: @escaping (Result) -> Void) { - self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) - } -} diff --git a/Sources/VeryfiSDK/Client/AddLineItem.swift b/Sources/VeryfiSDK/Client/AddLineItem.swift new file mode 100644 index 0000000..9e18a3a --- /dev/null +++ b/Sources/VeryfiSDK/Client/AddLineItem.swift @@ -0,0 +1,21 @@ +// +// AddLineItem.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Create line item for document in Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to modify. + /// - params: Line item data. + /// - completion: A block to execute + /// - detail: Response from server. + /// - error: Error from server. + public func addLineItem(documentId: String, params: AddLineItem, withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) + self.request(method: .POST, route: .documents, body: jsonData, queryItem: String(format: "%@/line-items", documentId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/AddTag.swift b/Sources/VeryfiSDK/Client/AddTag.swift new file mode 100644 index 0000000..2121ccc --- /dev/null +++ b/Sources/VeryfiSDK/Client/AddTag.swift @@ -0,0 +1,21 @@ +// +// AddTag.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Add tag to document. + /// - Parameters: + /// - documentId: ID of document to add tag. + /// - params: Tag data. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func addTag(documentId: String, params: AddTag, withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) + self.request(method: .PUT, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/AddTags.swift b/Sources/VeryfiSDK/Client/AddTags.swift new file mode 100644 index 0000000..d521538 --- /dev/null +++ b/Sources/VeryfiSDK/Client/AddTags.swift @@ -0,0 +1,21 @@ +// +// AddTags.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Add multiple tags in document. + /// - Parameters: + /// - documentId: ID of document to replace tags. + /// - params: Tags data. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func addTags(documentId: String, params: AddTags, withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) + self.request(method: .POST, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/Client.swift b/Sources/VeryfiSDK/Client/Client.swift new file mode 100644 index 0000000..f0fa3a6 --- /dev/null +++ b/Sources/VeryfiSDK/Client/Client.swift @@ -0,0 +1,26 @@ +import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +struct VeryfiCredentials { + let clientId: String + let clientSecret: String + let username: String + let apiKey: String +} + +public class Client: NetworkManager { + + /// Init Client. + /// - Parameters: + /// - clientId: Your client id from veryfi-hub. + /// - clientSecret: Your client secret from veryfi-hub. + /// - username: Your username from veryfi-hub. + /// - apiKey: Your api key from veryfi-hub. + /// - apiVersion: Api version to use, by default "v8". + public init(clientId: String, clientSecret: String, username: String, apiKey: String, apiVersion: String = "v8") { + let credentials = VeryfiCredentials(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey) + super.init(credentials: credentials, apiVersion: apiVersion) + } +} diff --git a/Sources/VeryfiSDK/Client/DeleteDocument.swift b/Sources/VeryfiSDK/Client/DeleteDocument.swift new file mode 100644 index 0000000..9ab57c5 --- /dev/null +++ b/Sources/VeryfiSDK/Client/DeleteDocument.swift @@ -0,0 +1,17 @@ +// +// DeleteDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Delete document from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to delete. + /// - completion: completion description + public func deleteDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .DELETE, route: .documents, queryItem: documentId, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/DeleteLineItem.swift b/Sources/VeryfiSDK/Client/DeleteLineItem.swift new file mode 100644 index 0000000..9c8425d --- /dev/null +++ b/Sources/VeryfiSDK/Client/DeleteLineItem.swift @@ -0,0 +1,18 @@ +// +// DeleteLineItem.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Delete line item from document from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document + /// - lineItemId: ID of line item to delete. + /// - completion: completion description + public func deleteLineItem(documentId: String, lineItemId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/DeleteLineItems.swift b/Sources/VeryfiSDK/Client/DeleteLineItems.swift new file mode 100644 index 0000000..06b8556 --- /dev/null +++ b/Sources/VeryfiSDK/Client/DeleteLineItems.swift @@ -0,0 +1,17 @@ +// +// DeleteLineItems.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Delete all line items from document from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document + /// - completion: completion description + public func deleteDocumentLineItems(documentId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetAnyDocument.swift b/Sources/VeryfiSDK/Client/GetAnyDocument.swift new file mode 100644 index 0000000..68ddebb --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetAnyDocument.swift @@ -0,0 +1,19 @@ +// +// GetAnyDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get single any document by ID from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to retreive + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getAnyDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .anyDocuments, queryItem: documentId, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetAnyDocuments.swift b/Sources/VeryfiSDK/Client/GetAnyDocuments.swift new file mode 100644 index 0000000..fd85135 --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetAnyDocuments.swift @@ -0,0 +1,19 @@ +// +// GetAnyDocuments.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get all any documents from Veryfi inbox. + /// - Parameters: + /// - queryItems: Query items to apply to the get request. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getAnyDocuments(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .anyDocuments, queryItems: queryItems, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetBankStatement.swift b/Sources/VeryfiSDK/Client/GetBankStatement.swift new file mode 100644 index 0000000..388c47f --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetBankStatement.swift @@ -0,0 +1,19 @@ +// +// GetBankStatement.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get single bank statements by ID from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to retreive + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getBankStatement(id: String, queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .bankStatements, queryItems: queryItems, queryItem: id, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetBankStatements.swift b/Sources/VeryfiSDK/Client/GetBankStatements.swift new file mode 100644 index 0000000..251c98e --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetBankStatements.swift @@ -0,0 +1,19 @@ +// +// GetBankStatements.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get all bank statements from Veryfi inbox. + /// - Parameters: + /// - queryItems: Query items to apply to the get request. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getBankStatements(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .bankStatements, queryItems: queryItems, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetDocument.swift b/Sources/VeryfiSDK/Client/GetDocument.swift new file mode 100644 index 0000000..fa53d9b --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetDocument.swift @@ -0,0 +1,19 @@ +// +// GetDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get single document by ID from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to retreive + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getDocument(documentId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .documents, queryItem: documentId, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetDocuments.swift b/Sources/VeryfiSDK/Client/GetDocuments.swift new file mode 100644 index 0000000..baf0571 --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetDocuments.swift @@ -0,0 +1,19 @@ +// +// GetDocuments.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get all documents from Veryfi inbox. + /// - Parameters: + /// - queryItems: Query items to apply to the get request. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getDocuments(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .documents, queryItems: queryItems, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetLineItem.swift b/Sources/VeryfiSDK/Client/GetLineItem.swift new file mode 100644 index 0000000..db7570c --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetLineItem.swift @@ -0,0 +1,20 @@ +// +// GetLineItem.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get single line item by document from Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document. + /// - lineItemId: ID of line item. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getLineItem(documentId: String, lineItemId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/GetLineItems.swift b/Sources/VeryfiSDK/Client/GetLineItems.swift new file mode 100644 index 0000000..94e67d0 --- /dev/null +++ b/Sources/VeryfiSDK/Client/GetLineItems.swift @@ -0,0 +1,19 @@ +// +// GetLineItems.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Get all line items from document. + /// - Parameters: + /// - documentId: ID of document to get line items. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func getDocumentLineItems(documentId: String, withCompletion completion: @escaping (Result) -> Void) { + self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessAnyDocument.swift b/Sources/VeryfiSDK/Client/ProcessAnyDocument.swift new file mode 100644 index 0000000..6c8bb76 --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessAnyDocument.swift @@ -0,0 +1,37 @@ +// +// ProcessAnyDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload any document for the Veryfi API to process. + /// - Parameters: + /// - fileName: Name of the file to upload to the Veryfi API. + /// - fileData: UTF8 encoded file data + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - params: Additional parameters. + /// - completion: Function called after request completes. + /// - detail: Response from server. + /// - error: Error from server. + public func processAnyDocument(fileName: String, + fileData: Data, + params: [String: Any]? = nil, + templateName: String, + withCompletion completion: @escaping (Result) -> Void) { + var requestParams = params ?? [String: Any]() + requestParams["file_data"] = fileData.base64EncodedString() + requestParams["file_name"] = fileName + requestParams["template_name"] = templateName + + guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { + completion(.failure(.parsingError)) + return + } + + self.request(method: .POST, route: .anyDocuments, uploadData: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessAnyDocumentUrl.swift b/Sources/VeryfiSDK/Client/ProcessAnyDocumentUrl.swift new file mode 100644 index 0000000..5672370 --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessAnyDocumentUrl.swift @@ -0,0 +1,38 @@ +// +// ProcessAnyDocumentUrl.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload any document to Veryfi API with URL. + /// - Parameters: + /// - fileUrl: Publicly available URL. + /// - fileUrls: List of publicly available URLs. + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - boostMode: Skip data enrichment but process document faster. + /// - externalId: Existing ID to assign to document. + /// - maxPagesToProcess: Number of pages to process. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func processAnyDocumentURL(fileUrl: String? = nil, + fileUrls: [String]? = nil, + categories: [String]? = nil, + deleteAfterProcessing: Bool = false, + boostMode: Int = 0, + externalId: String? = nil, + maxPagesToProcess: Int? = 1, + templateName: String, + withCompletion completion: @escaping (Result) -> Void) { + let params: [String: Any] = ["file_url": fileUrl as Any, //implicit coerce + "file_urls": fileUrls as Any, //implicit coerce + "template_name": templateName, + "max_pages_to_process": maxPagesToProcess as Any] //implicit coerce + let jsonData = try? JSONSerialization.data(withJSONObject: params) + self.request(method: .POST, route: .anyDocuments, body: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessBankStatement.swift b/Sources/VeryfiSDK/Client/ProcessBankStatement.swift new file mode 100644 index 0000000..5fee008 --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessBankStatement.swift @@ -0,0 +1,37 @@ +// +// ProcessBankStatement.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload a bank statement for the Veryfi API to process. + /// - Parameters: + /// - fileName: Name of the file to upload to the Veryfi API. + /// - fileData: UTF8 encoded file data + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - params: Additional parameters. + /// - completion: Function called after request completes. + /// - detail: Response from server. + /// - error: Error from server. + public func processBankStatement(fileName: String, + fileData: Data, + params: [String: Any]? = nil, + confidenceDetails: Bool = false, + withCompletion completion: @escaping (Result) -> Void) { + var requestParams = params ?? [String: Any]() + requestParams["confidence_details"] = confidenceDetails + requestParams["file_data"] = fileData.base64EncodedString() + requestParams["file_name"] = fileName + + guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { + completion(.failure(.parsingError)) + return + } + + self.request(method: .POST, route: .bankStatements, uploadData: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessBankStatementUrl.swift b/Sources/VeryfiSDK/Client/ProcessBankStatementUrl.swift new file mode 100644 index 0000000..db70008 --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessBankStatementUrl.swift @@ -0,0 +1,36 @@ +// +// ProcessBankStatementUrl.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload bank statement to Veryfi API with URL. + /// - Parameters: + /// - fileUrl: Publicly available URL. + /// - fileUrls: List of publicly available URLs. + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - boostMode: Skip data enrichment but process document faster. + /// - externalId: Existing ID to assign to document. + /// - maxPagesToProcess: Number of pages to process. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func processBankStatementURL(fileUrl: String? = nil, + fileUrls: [String]? = nil, + boundingBoxes: Bool = false, + confidenceDetails: Bool = false, + withCompletion completion: @escaping (Result) -> Void) { + let params: [String: Any] = [ + "bounding_boxes": boundingBoxes, + "confidence_details": confidenceDetails, + "file_url": fileUrl as Any, //implicit coerce + "file_urls": fileUrls as Any, //implicit coerce + ] //implicit coerce + let jsonData = try? JSONSerialization.data(withJSONObject: params) + self.request(method: .POST, route: .bankStatements, body: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessDocument.swift b/Sources/VeryfiSDK/Client/ProcessDocument.swift new file mode 100644 index 0000000..787d51a --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessDocument.swift @@ -0,0 +1,40 @@ +// +// ProcessDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload an image for the Veryfi API to process. + /// - Parameters: + /// - fileName: Name of the file to upload to the Veryfi API. + /// - fileData: UTF8 encoded file data + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - params: Additional parameters. + /// - completion: Function called after request completes. + /// - detail: Response from server. + /// - error: Error from server. + public func processDocument(fileName: String, + fileData: Data, + categories: [String]? = nil, + deleteAfterProcessing: Bool = false, + params: [String: Any]? = nil, + withCompletion completion: @escaping (Result) -> Void) { + let requestCats = categories ?? [] + var requestParams = params ?? [String: Any]() + requestParams["categories"] = requestCats + requestParams["auto_delete"] = deleteAfterProcessing + requestParams["file_data"] = fileData.base64EncodedString() + requestParams["file_name"] = fileName + + guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else { + completion(.failure(.parsingError)) + return + } + + self.request(method: .POST, route: .documents, uploadData: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ProcessDocumentUrl.swift b/Sources/VeryfiSDK/Client/ProcessDocumentUrl.swift new file mode 100644 index 0000000..e346d5a --- /dev/null +++ b/Sources/VeryfiSDK/Client/ProcessDocumentUrl.swift @@ -0,0 +1,40 @@ +// +// ProcessDocumentUrl.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Upload document to Veryfi API with URL. + /// - Parameters: + /// - fileUrl: Publicly available URL. + /// - fileUrls: List of publicly available URLs. + /// - categories: List of document categories. + /// - deleteAfterProcessing: Do not store file in Veryfi's inbox. + /// - boostMode: Skip data enrichment but process document faster. + /// - externalId: Existing ID to assign to document. + /// - maxPagesToProcess: Number of pages to process. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func processDocumentURL(fileUrl: String? = nil, + fileUrls: [String]? = nil, + categories: [String]? = nil, + deleteAfterProcessing: Bool = false, + boostMode: Int = 0, + externalId: String? = nil, + maxPagesToProcess: Int? = 1, + withCompletion completion: @escaping (Result) -> Void) { + let params: [String: Any] = ["auto_delete": deleteAfterProcessing, + "boost_mode": boostMode, + "categories": categories ?? [], + "external_id": externalId as Any, //implicit coerce + "file_url": fileUrl as Any, //implicit coerce + "file_urls": fileUrls as Any, //implicit coerce + "max_pages_to_process": maxPagesToProcess as Any] //implicit coerce + let jsonData = try? JSONSerialization.data(withJSONObject: params) + self.request(method: .POST, route: .documents, body: jsonData, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/ReplaceTags.swift b/Sources/VeryfiSDK/Client/ReplaceTags.swift new file mode 100644 index 0000000..3897f64 --- /dev/null +++ b/Sources/VeryfiSDK/Client/ReplaceTags.swift @@ -0,0 +1,21 @@ +// +// ReplaceTags.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Replace tags in document. + /// - Parameters: + /// - documentId: ID of document to add tag. + /// - params: New tags to replace. + /// - completion: Block executed after request. + /// - detail: Response from server. + /// - error: Error from server. + public func replaceTags(documentId: String, params: [String], withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: ["tags": params]) + self.request(method: .PUT, route: .documents, body: jsonData, queryItem: documentId, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/UpdateDocument.swift b/Sources/VeryfiSDK/Client/UpdateDocument.swift new file mode 100644 index 0000000..0748700 --- /dev/null +++ b/Sources/VeryfiSDK/Client/UpdateDocument.swift @@ -0,0 +1,21 @@ +// +// UpdateDocument.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Update information of document in Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document to modify. + /// - params: Names and values to modify. + /// - completion: A block to execute + /// - detail: Response from server. + /// - error: Error from server. + public func updateDocument(documentId: String, params: [String: Any], withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: params) + self.request(method: .PUT, route: .documents, body: jsonData, queryItem: documentId, completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Client/UpdateLineItem.swift b/Sources/VeryfiSDK/Client/UpdateLineItem.swift new file mode 100644 index 0000000..2468254 --- /dev/null +++ b/Sources/VeryfiSDK/Client/UpdateLineItem.swift @@ -0,0 +1,22 @@ +// +// UpdateLineItem.swift +// VeryfiSDK +// +// Created by Veryfi on 25/10/24. +// +import Foundation + +extension Client { + /// Update line item for document in Veryfi inbox. + /// - Parameters: + /// - documentId: ID of document. + /// - lineItemId: ID of line item to modify. + /// - params: Line item data. + /// - completion: A block to execute + /// - detail: Response from server. + /// - error: Error from server. + public func updateLineItem(documentId: String, lineItemId: String, params: UpdateLineItem, withCompletion completion: @escaping (Result) -> Void) { + let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary) + self.request(method: .PUT, route: .documents, body: jsonData, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion) + } +} diff --git a/Sources/VeryfiSDK/Models/AddLineItem.swift b/Sources/VeryfiSDK/Models/AddLineItemModel.swift similarity index 100% rename from Sources/VeryfiSDK/Models/AddLineItem.swift rename to Sources/VeryfiSDK/Models/AddLineItemModel.swift diff --git a/Sources/VeryfiSDK/Models/AddTag.swift b/Sources/VeryfiSDK/Models/AddTagModel.swift similarity index 100% rename from Sources/VeryfiSDK/Models/AddTag.swift rename to Sources/VeryfiSDK/Models/AddTagModel.swift diff --git a/Sources/VeryfiSDK/Models/AddTags.swift b/Sources/VeryfiSDK/Models/AddTagsModel.swift similarity index 100% rename from Sources/VeryfiSDK/Models/AddTags.swift rename to Sources/VeryfiSDK/Models/AddTagsModel.swift diff --git a/Sources/VeryfiSDK/Models/LineItem.swift b/Sources/VeryfiSDK/Models/LineItemModel.swift similarity index 100% rename from Sources/VeryfiSDK/Models/LineItem.swift rename to Sources/VeryfiSDK/Models/LineItemModel.swift diff --git a/Sources/VeryfiSDK/Models/UpdateLineItem.swift b/Sources/VeryfiSDK/Models/UpdateLineItemModel.swift similarity index 100% rename from Sources/VeryfiSDK/Models/UpdateLineItem.swift rename to Sources/VeryfiSDK/Models/UpdateLineItemModel.swift diff --git a/Tests/VeryfiSDKTests/AddLineItemTest.swift b/Tests/VeryfiSDKTests/AddLineItemTest.swift new file mode 100644 index 0000000..f9eb57d --- /dev/null +++ b/Tests/VeryfiSDKTests/AddLineItemTest.swift @@ -0,0 +1,36 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testAddLineItem() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addLineItem") + } + + let expectation = XCTestExpectation(description: "Add line item to document") + let documentId = 63480993 + let params = AddLineItem(order: 20, description: "Test", total: 44.0) + params.sku = "testsku" + client.addLineItem(documentId: String(documentId), params: params, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + if mockResponses { + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + } else { + XCTAssertEqual(jsonResponse!["total"] as? Float, params.total) + XCTAssertEqual(jsonResponse!["description"] as? String, params.description) + } + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/AddTagTest.swift b/Tests/VeryfiSDKTests/AddTagTest.swift new file mode 100644 index 0000000..3f2322f --- /dev/null +++ b/Tests/VeryfiSDKTests/AddTagTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testAddTag() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addTag") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from add tag") + + client.addTag(documentId: "31727276", params: AddTag(name: "test_tag"), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertTrue((jsonResponse?["name"] as? String)?.lowercased() == "test_tag") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/AddTagsTest.swift b/Tests/VeryfiSDKTests/AddTagsTest.swift new file mode 100644 index 0000000..8f6734c --- /dev/null +++ b/Tests/VeryfiSDKTests/AddTagsTest.swift @@ -0,0 +1,30 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testAddTags() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addTags") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from add tags") + + client.addTags(documentId: "31727276", params: AddTags(tags: ["tag_1", "tag_2"]), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + let results = jsonResponse?["tags"] as? [[String: Any]] + XCTAssertEqual(results?.count ?? 0, 2) + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/BadCredentialsTest.swift b/Tests/VeryfiSDKTests/BadCredentialsTest.swift new file mode 100644 index 0000000..fa2c9a6 --- /dev/null +++ b/Tests/VeryfiSDKTests/BadCredentialsTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testBadCredentials() { + let expectation = XCTestExpectation(description: "Get response to a bad credential case") + let badClient = Client(clientId: "badClientId", clientSecret: "badClientSecret", username: "badUsername", apiKey: "badApiKey") + badClient.getDocuments(withCompletion: { result in + switch result{ + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonResponse?["status"] as? String, let message = jsonResponse?["message"] as? String else { + XCTFail() + return + } + XCTAssertEqual("fail", status) + XCTAssertEqual("Not Authorized", message) + case .failure(let error): + print(error) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/DeleteDocumentTest.swift b/Tests/VeryfiSDKTests/DeleteDocumentTest.swift new file mode 100644 index 0000000..5b6162a --- /dev/null +++ b/Tests/VeryfiSDKTests/DeleteDocumentTest.swift @@ -0,0 +1,64 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testDeleteDocument() { + let expectationDocuments = XCTestExpectation(description: "Get documents") + let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteDocument") + let documentId = 31727276 + expectationDocuments.fulfill() + client.deleteDocument(documentId: String(documentId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + } else { + client.processDocumentURL(fileUrl: url, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let documentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + client.deleteDocument(documentId: String(documentId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + case .failure(let error): + print(error) + } + expectationDocuments.fulfill() + }) + } + + wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/DeleteLineItemTest.swift b/Tests/VeryfiSDKTests/DeleteLineItemTest.swift new file mode 100644 index 0000000..7657e45 --- /dev/null +++ b/Tests/VeryfiSDKTests/DeleteLineItemTest.swift @@ -0,0 +1,68 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testDeleteLineItem() { + let expectationDocuments = XCTestExpectation(description: "Get documents") + let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteLineItem") + let documentId = 63480993 + let lineItemId = 190399931 + expectationDocuments.fulfill() + client.deleteLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + } else { + let documentId = 63480993 + let params = AddLineItem(order: 20, description: "Test", total: 44.4) + params.sku = "testsku" + client.addLineItem(documentId: String(documentId), params: params, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let lineItemId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + client.deleteLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + case .failure(let error): + print(error) + } + expectationDocuments.fulfill() + }) + } + + wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/DeleteLineItemsTest.swift b/Tests/VeryfiSDKTests/DeleteLineItemsTest.swift new file mode 100644 index 0000000..9a011cb --- /dev/null +++ b/Tests/VeryfiSDKTests/DeleteLineItemsTest.swift @@ -0,0 +1,64 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testDeleteDocumentLineItems() { + let expectationDocuments = XCTestExpectation(description: "Get documents") + let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteDocumentLineItems") + let documentId = 63480993 + expectationDocuments.fulfill() + client.deleteDocumentLineItems(documentId: String(documentId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + } else { + client.processDocumentURL(fileUrl: url, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let documentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + client.deleteDocumentLineItems(documentId: String(documentId), withCompletion: { result in + switch result { + case .success(let data): + let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let status = jsonStringDeleteResponse?["status"] as? String else { + XCTFail() + return + } + XCTAssertEqual("ok", status) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectationDelete.fulfill() + }) + case .failure(let error): + print(error) + } + expectationDocuments.fulfill() + }) + } + + wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetAnyDocumentTest.swift b/Tests/VeryfiSDKTests/GetAnyDocumentTest.swift new file mode 100644 index 0000000..882ee76 --- /dev/null +++ b/Tests/VeryfiSDKTests/GetAnyDocumentTest.swift @@ -0,0 +1,62 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetAnyDocument() { + let expectation = XCTestExpectation(description: "Get a any document by id in a JSON") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getAnyDocument") + let id = Int64(4560114) + client.getAnyDocument(documentId: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } else { + client.getAnyDocuments(withCompletion: { result in + switch result { + case .success(let data): + let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let id = ((jsonDocuments?["results"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { + XCTFail() + return + } + client.getAnyDocument(documentId: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + }) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } + + wait(for: [expectation], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetAnyDocumentsTest.swift b/Tests/VeryfiSDKTests/GetAnyDocumentsTest.swift new file mode 100644 index 0000000..54f2ecd --- /dev/null +++ b/Tests/VeryfiSDKTests/GetAnyDocumentsTest.swift @@ -0,0 +1,31 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetAnyDocuments() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getAnyDocuments") + } + + let expectation = XCTestExpectation(description: "Get all any documents in a JSON array") + + client.getAnyDocuments(withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + let results = jsonResponse?["results"] as? [[String: Any]] + XCTAssertGreaterThanOrEqual(results?.count ?? 0, 2) + XCTAssertTrue(results?.first?["template_name"] as? String == "us_driver_license") + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetBankStatementTest.swift b/Tests/VeryfiSDKTests/GetBankStatementTest.swift new file mode 100644 index 0000000..ab0e3dc --- /dev/null +++ b/Tests/VeryfiSDKTests/GetBankStatementTest.swift @@ -0,0 +1,62 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetBankStatement() { + let expectation = XCTestExpectation(description: "Get a bank statement by id in a JSON") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getBankStatement") + let id = Int64(4560116) + client.getBankStatement(id: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } else { + client.getBankStatements(withCompletion: { result in + switch result { + case .success(let data): + let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let id = ((jsonDocuments?["results"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { + XCTFail() + return + } + client.getBankStatement(id: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + }) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } + + wait(for: [expectation], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetBankStatementsTest.swift b/Tests/VeryfiSDKTests/GetBankStatementsTest.swift new file mode 100644 index 0000000..4e7b472 --- /dev/null +++ b/Tests/VeryfiSDKTests/GetBankStatementsTest.swift @@ -0,0 +1,32 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetBankStatements() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getBankStatements") + } + + let expectation = XCTestExpectation(description: "Get all bank statements in a JSON array") + + client.getBankStatements(withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + let results = jsonResponse?["results"] as? [[String: Any]] + XCTAssertGreaterThanOrEqual(results?.count ?? 0, 2) + XCTAssertTrue(results?.first?["bank_vat_number"] as? String == "SC327000") + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetDocumentTest.swift b/Tests/VeryfiSDKTests/GetDocumentTest.swift new file mode 100644 index 0000000..63ac2c4 --- /dev/null +++ b/Tests/VeryfiSDKTests/GetDocumentTest.swift @@ -0,0 +1,62 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetDocument() { + let expectation = XCTestExpectation(description: "Get a document by id in a JSON") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocument") + let id = Int64(31727276) + client.getDocument(documentId: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } else { + client.getDocuments(withCompletion: { result in + switch result { + case .success(let data): + let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let id = ((jsonDocuments?["documents"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { + XCTFail() + return + } + client.getDocument(documentId: String(id), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let getDocumentId = jsonResponse?["id"] as? Int64 else { + XCTFail() + return + } + XCTAssertEqual(getDocumentId, id) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + }) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + } + + wait(for: [expectation], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetDocumentWithQueryItemsTest.swift b/Tests/VeryfiSDKTests/GetDocumentWithQueryItemsTest.swift new file mode 100644 index 0000000..ea25605 --- /dev/null +++ b/Tests/VeryfiSDKTests/GetDocumentWithQueryItemsTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetDocumentWithQueryItems() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments") + } + + let expectation = XCTestExpectation(description: "Get all documents in a JSON array") + let queryItems = [URLQueryItem(name: "order_by", value: "created")] + client.getDocuments(queryItems: queryItems, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetDocumentsTest.swift b/Tests/VeryfiSDKTests/GetDocumentsTest.swift new file mode 100644 index 0000000..1e33efc --- /dev/null +++ b/Tests/VeryfiSDKTests/GetDocumentsTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetDocuments() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments") + } + + let expectation = XCTestExpectation(description: "Get all documents in a JSON array") + + client.getDocuments(withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 120.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetLineItemTest.swift b/Tests/VeryfiSDKTests/GetLineItemTest.swift new file mode 100644 index 0000000..3785697 --- /dev/null +++ b/Tests/VeryfiSDKTests/GetLineItemTest.swift @@ -0,0 +1,30 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetLineItem() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getLineItem") + } + + let expectation = XCTestExpectation(description: "Get line item from document") + let documentId = 63480993 + let lineItemId = 190399931 + client.getLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/GetLineItemsTest.swift b/Tests/VeryfiSDKTests/GetLineItemsTest.swift new file mode 100644 index 0000000..3dcb8ef --- /dev/null +++ b/Tests/VeryfiSDKTests/GetLineItemsTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testGetDocumentLineItems() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocumentLineItems") + } + + let expectation = XCTestExpectation(description: "Get all line items from document") + let documentId = 63480993 + client.getDocumentLineItems(documentId: String(documentId), withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertNotNil(jsonResponse!["line_items"]) + case .failure(let error): + print(error) + XCTAssertTrue(false) + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessAnyDocumentTest.swift b/Tests/VeryfiSDKTests/ProcessAnyDocumentTest.swift new file mode 100644 index 0000000..c78f3d3 --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessAnyDocumentTest.swift @@ -0,0 +1,31 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessAnyDocument() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processAnyDocument") + } + + let expectation = XCTestExpectation(description: "Get data from processing any document") + + let url = Bundle.module.url(forResource: "driver_license", withExtension: "png")! + let fileData = try! Data(contentsOf: url) + client.processAnyDocument(fileName: "driver_license.png", fileData: fileData, templateName: "us_driver_license", withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertTrue(jsonResponse?["last_name"] as? String == "McLovin") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 60.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessAnyDocumentUrlTest.swift b/Tests/VeryfiSDKTests/ProcessAnyDocumentUrlTest.swift new file mode 100644 index 0000000..7bd329d --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessAnyDocumentUrlTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessAnyDocumentURL() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processAnyDocument") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from process any document") + + client.processAnyDocumentURL(fileUrl: driverLicenseUrl, templateName: "us_driver_license", withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertTrue(jsonResponse?["last_name"] as? String == "McLovin") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessBankStatementTest.swift b/Tests/VeryfiSDKTests/ProcessBankStatementTest.swift new file mode 100644 index 0000000..0bd98d5 --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessBankStatementTest.swift @@ -0,0 +1,31 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessBankStatement() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processBankStatement") + } + + let expectation = XCTestExpectation(description: "Get data from processing bank statement") + + let url = Bundle.module.url(forResource: "bankstatement", withExtension: "pdf")! + let fileData = try? Data(contentsOf: url) + client.processBankStatement(fileName: "bankstatement.pdf", fileData: fileData!, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertTrue(jsonResponse?["account_holder_name"] as? String == "Mr Robot Roboto") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 60.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessBankStatementUrlTest.swift b/Tests/VeryfiSDKTests/ProcessBankStatementUrlTest.swift new file mode 100644 index 0000000..5be5025 --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessBankStatementUrlTest.swift @@ -0,0 +1,29 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessBankStatementURL() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processBankStatement") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from process bank statement") + + client.processBankStatementURL(fileUrl: bankStatementUrl, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + XCTAssertTrue(jsonResponse?["account_holder_name"] as? String == "Mr Robot Roboto") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessDocumentTest.swift b/Tests/VeryfiSDKTests/ProcessDocumentTest.swift new file mode 100644 index 0000000..17851eb --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessDocumentTest.swift @@ -0,0 +1,36 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessDocument() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument") + } + + let expectation = XCTestExpectation(description: "Get data from processing document") + + let url = Bundle.module.url(forResource: "receipt", withExtension: "jpeg")! + let fileData = try? Data(contentsOf: url) + let categories = ["Advertising & Marketing", "Automotive"] + client.processDocument(fileName: file, fileData: fileData!, categories: categories, deleteAfterProcessing: true, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let vendor = (jsonResponse?["vendor"] as? NSDictionary)?["name"] as? String else { + XCTFail() + break + } + XCTAssertEqual("Walgreens", vendor) + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 60.0) + } +} diff --git a/Tests/VeryfiSDKTests/ProcessDocumentUrlTest.swift b/Tests/VeryfiSDKTests/ProcessDocumentUrlTest.swift new file mode 100644 index 0000000..c116bc3 --- /dev/null +++ b/Tests/VeryfiSDKTests/ProcessDocumentUrlTest.swift @@ -0,0 +1,34 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testProcessDocumentURL() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from process document") + + let categories = ["Advertising & Marketing", "Automotive"] + client.processDocumentURL(fileUrl: url, categories: categories, deleteAfterProcessing: true, boostMode: 1, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let vendor = (jsonResponse?["vendor"] as? NSDictionary)?["name"] as? String else { + XCTFail() + break + } + XCTAssertEqual("Walgreens", vendor) + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/ReplaceTagsTest.swift b/Tests/VeryfiSDKTests/ReplaceTagsTest.swift new file mode 100644 index 0000000..e881fa0 --- /dev/null +++ b/Tests/VeryfiSDKTests/ReplaceTagsTest.swift @@ -0,0 +1,30 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testReplaceTags() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "replaceTags") + } + + let expectation = XCTestExpectation(description: "Get a JSON response from replace tags") + + client.replaceTags(documentId: "4587125", params: ["6673475"], withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + let tags = jsonResponse?["tags"] as? [[String: Any]] + XCTAssertTrue((tags?.first?["name"] as? String)?.lowercased() == "test_tag") + case .failure(let error): + print(error) + XCTFail() + } + expectation.fulfill() + }) + + wait(for: [expectation], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/Resources/deleteTags.json b/Tests/VeryfiSDKTests/Resources/deleteTags.json deleted file mode 100644 index c01eaf0..0000000 --- a/Tests/VeryfiSDKTests/Resources/deleteTags.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "date": "", - "description": "TEST", - "discount": 0.0, - "discount_rate": 0.0, - "end_date": "", - "full_description": "", - "hsn": "", - "id": 189951682, - "order": 20, - "price": 0.0, - "quantity": 1.0, - "section": "", - "sku": "aqw", - "start_date": "", - "tax": 0.0, - "total": 20.1, - "type": "", - "unit_of_measure": "", - "upc": null, - "weight": "" -} \ No newline at end of file diff --git a/Tests/VeryfiSDKTests/Resources/replaceTags.json b/Tests/VeryfiSDKTests/Resources/replaceTags.json new file mode 100644 index 0000000..e3cca59 --- /dev/null +++ b/Tests/VeryfiSDKTests/Resources/replaceTags.json @@ -0,0 +1,12 @@ +{ + "tags": [ + { + "id": 6862358, + "name": "TAG_1" + }, + { + "id": 6862359, + "name": "TAG_2" + } + ] +} diff --git a/Tests/VeryfiSDKTests/UpdateDocumentTest.swift b/Tests/VeryfiSDKTests/UpdateDocumentTest.swift new file mode 100644 index 0000000..13acbb9 --- /dev/null +++ b/Tests/VeryfiSDKTests/UpdateDocumentTest.swift @@ -0,0 +1,60 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testUpdateDocument() { + let expectation = XCTestExpectation(description: "Get data from update document") + + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "updateDocument") + let updateNotes = ["notes": "Note updated"] + let id = 31727276 + client.updateDocument(documentId: String(id), params: updateNotes, withCompletion: { result in + switch result { + case .success(let data): + print(data) + XCTAssertTrue(true) + case .failure(let error): + print(error) + XCTAssert(false) + } + expectation.fulfill() + }) + } else { + func generateRandomString() -> String { + let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + return String((0..<10).map{ _ in letters.randomElement()! }) + } + let notes = generateRandomString() + let updateNotes = ["notes": notes] + client.getDocuments(withCompletion: { result in + switch result { + case .success(let data): + let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + guard let id = ((jsonDocuments?["documents"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { + XCTFail() + return + } + client.updateDocument(documentId: String(id), params: updateNotes, withCompletion: { result in + switch result { + case .success(let data): + print(data) + XCTAssertTrue(true) + case .failure(let error): + print(error) + XCTAssert(false) + } + expectation.fulfill() + }) + case .failure(let error): + print(error) + } + }) + } + + wait(for: [expectation], timeout: 40.0) + } +} diff --git a/Tests/VeryfiSDKTests/UpdateLineItemTest.swift b/Tests/VeryfiSDKTests/UpdateLineItemTest.swift new file mode 100644 index 0000000..7b5727a --- /dev/null +++ b/Tests/VeryfiSDKTests/UpdateLineItemTest.swift @@ -0,0 +1,56 @@ +import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif +@testable import VeryfiSDK + +extension VeryfiSDKTests { + func testUpdateLineItem() { + if (mockResponses) { + client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addLineItem") + } + + let expectation1 = XCTestExpectation(description: "Add line item to document") + let documentId = 63480993 + var lineItemId = 0 + let params1 = AddLineItem(order: 20, description: "Test", total: 44.0) + params1.sku = "testsku" + client.addLineItem(documentId: String(documentId), params: params1, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + if mockResponses { + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + } else { + lineItemId = jsonResponse!["id"] as? Int ?? 0 + } + case .failure(let error): + print(error) + XCTFail() + } + expectation1.fulfill() + }) + wait(for: [expectation1], timeout: 20.0) + + let expectation2 = XCTestExpectation(description: "Update line item to document") + let params2 = UpdateLineItem() + params2.description = "Test" + client.updateLineItem(documentId: String(documentId), lineItemId: String(lineItemId), params: params2, withCompletion: { result in + switch result { + case .success(let data): + let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary + if mockResponses { + XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) + } else { + XCTAssertEqual(jsonResponse!["description"] as? String, params2.description) + } + case .failure(let error): + print(error) + XCTFail() + } + expectation2.fulfill() + }) + + wait(for: [expectation2], timeout: 20.0) + } +} diff --git a/Tests/VeryfiSDKTests/VeryfiSDKTests.swift b/Tests/VeryfiSDKTests/VeryfiSDKTests.swift index afe3233..0fde203 100644 --- a/Tests/VeryfiSDKTests/VeryfiSDKTests.swift +++ b/Tests/VeryfiSDKTests/VeryfiSDKTests.swift @@ -31,829 +31,4 @@ class ClientSpy: Client { } } -final class VeryfiSDKTests: XCTestCase { - - func testGetDocuments() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments") - } - - let expectation = XCTestExpectation(description: "Get all documents in a JSON array") - - client.getDocuments(withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetDocumentWithQueryItems() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments") - } - - let expectation = XCTestExpectation(description: "Get all documents in a JSON array") - let queryItems = [URLQueryItem(name: "order_by", value: "created")] - client.getDocuments(queryItems: queryItems, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetDocument() { - let expectation = XCTestExpectation(description: "Get a document by id in a JSON") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocument") - let id = Int64(31727276) - client.getDocument(documentId: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } else { - client.getDocuments(withCompletion: { result in - switch result { - case .success(let data): - let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let id = ((jsonDocuments?["documents"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { - XCTFail() - return - } - client.getDocument(documentId: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - }) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } - - wait(for: [expectation], timeout: 40.0) - } - - func testProcessDocument() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument") - } - - let expectation = XCTestExpectation(description: "Get data from processing document") - - let url = Bundle.module.url(forResource: "receipt", withExtension: "jpeg")! - let fileData = try? Data(contentsOf: url) - let categories = ["Advertising & Marketing", "Automotive"] - client.processDocument(fileName: file, fileData: fileData!, categories: categories, deleteAfterProcessing: true, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let vendor = (jsonResponse?["vendor"] as? NSDictionary)?["name"] as? String else { - XCTFail() - break - } - XCTAssertEqual("Walgreens", vendor) - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 60.0) - } - - func testProcessDocumentURL() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument") - } - - let expectation = XCTestExpectation(description: "Get a JSON response from process document") - - let categories = ["Advertising & Marketing", "Automotive"] - client.processDocumentURL(fileUrl: url, categories: categories, deleteAfterProcessing: true, boostMode: 1, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let vendor = (jsonResponse?["vendor"] as? NSDictionary)?["name"] as? String else { - XCTFail() - break - } - XCTAssertEqual("Walgreens", vendor) - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetAnyDocuments() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getAnyDocuments") - } - - let expectation = XCTestExpectation(description: "Get all any documents in a JSON array") - - client.getAnyDocuments(withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - let results = jsonResponse?["results"] as? [[String: Any]] - XCTAssertGreaterThanOrEqual(results?.count ?? 0, 2) - XCTAssertTrue(results?.first?["template_name"] as? String == "us_driver_license") - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetAnyDocument() { - let expectation = XCTestExpectation(description: "Get a any document by id in a JSON") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getAnyDocument") - let id = Int64(4560114) - client.getAnyDocument(documentId: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } else { - client.getAnyDocuments(withCompletion: { result in - switch result { - case .success(let data): - let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let id = ((jsonDocuments?["results"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { - XCTFail() - return - } - client.getAnyDocument(documentId: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - }) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } - - wait(for: [expectation], timeout: 40.0) - } - - func testProcessAnyDocument() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processAnyDocument") - } - - let expectation = XCTestExpectation(description: "Get data from processing any document") - - let url = Bundle.module.url(forResource: "driver_license", withExtension: "png")! - let fileData = try! Data(contentsOf: url) - client.processAnyDocument(fileName: "driver_license.png", fileData: fileData, templateName: "us_driver_license", withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertTrue(jsonResponse?["last_name"] as? String == "McLovin") - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 60.0) - } - - func testProcessAnyDocumentURL() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processAnyDocument") - } - - let expectation = XCTestExpectation(description: "Get a JSON response from process any document") - - client.processAnyDocumentURL(fileUrl: driverLicenseUrl, templateName: "us_driver_license", withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertTrue(jsonResponse?["last_name"] as? String == "McLovin") - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetBankStatements() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getBankStatements") - } - - let expectation = XCTestExpectation(description: "Get all bank statements in a JSON array") - - client.getBankStatements(withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - let results = jsonResponse?["results"] as? [[String: Any]] - XCTAssertGreaterThanOrEqual(results?.count ?? 0, 2) - XCTAssertTrue(results?.first?["bank_vat_number"] as? String == "SC327000") - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetBankStatement() { - let expectation = XCTestExpectation(description: "Get a bank statement by id in a JSON") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getBankStatement") - let id = Int64(4560116) - client.getBankStatement(id: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } else { - client.getBankStatements(withCompletion: { result in - switch result { - case .success(let data): - let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let id = ((jsonDocuments?["results"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { - XCTFail() - return - } - client.getBankStatement(id: String(id), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let getDocumentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - XCTAssertEqual(getDocumentId, id) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - }) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - } - - wait(for: [expectation], timeout: 40.0) - } - - func testProcessBankStatement() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processBankStatement") - } - - let expectation = XCTestExpectation(description: "Get data from processing bank statement") - - let url = Bundle.module.url(forResource: "bankstatement", withExtension: "pdf")! - let fileData = try? Data(contentsOf: url) - client.processBankStatement(fileName: "bankstatement.pdf", fileData: fileData!, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertTrue(jsonResponse?["account_holder_name"] as? String == "Mr Robot Roboto") - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 60.0) - } - - func testProcessBankStatementURL() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processBankStatement") - } - - let expectation = XCTestExpectation(description: "Get a JSON response from process bank statement") - - client.processBankStatementURL(fileUrl: bankStatementUrl, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertTrue(jsonResponse?["account_holder_name"] as? String == "Mr Robot Roboto") - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testAddTag() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addTag") - } - - let expectation = XCTestExpectation(description: "Get a JSON response from add tag") - - client.addTag(documentId: "31727276", params: AddTag(name: "test_tag"), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertTrue((jsonResponse?["name"] as? String)?.lowercased() == "test_tag") - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testAddTags() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addTags") - } - - let expectation = XCTestExpectation(description: "Get a JSON response from add tags") - - client.addTags(documentId: "31727276", params: AddTags(tags: ["tag_1", "tag_2"]), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - let results = jsonResponse?["tags"] as? [[String: Any]] - XCTAssertEqual(results?.count ?? 0, 2) - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testUpdateDocument() { - let expectation = XCTestExpectation(description: "Get data from update document") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "updateDocument") - let updateNotes = ["notes": "Note updated"] - let id = 31727276 - client.updateDocument(documentId: String(id), params: updateNotes, withCompletion: { result in - switch result { - case .success(let data): - print(data) - XCTAssertTrue(true) - case .failure(let error): - print(error) - XCTAssert(false) - } - expectation.fulfill() - }) - } else { - func generateRandomString() -> String { - let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - return String((0..<10).map{ _ in letters.randomElement()! }) - } - let notes = generateRandomString() - let updateNotes = ["notes": notes] - client.getDocuments(withCompletion: { result in - switch result { - case .success(let data): - let jsonDocuments = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let id = ((jsonDocuments?["documents"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else { - XCTFail() - return - } - client.updateDocument(documentId: String(id), params: updateNotes, withCompletion: { result in - switch result { - case .success(let data): - print(data) - XCTAssertTrue(true) - case .failure(let error): - print(error) - XCTAssert(false) - } - expectation.fulfill() - }) - case .failure(let error): - print(error) - } - }) - } - - wait(for: [expectation], timeout: 40.0) - } - - func testDeleteDocument() { - let expectationDocuments = XCTestExpectation(description: "Get documents") - let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteDocument") - let documentId = 31727276 - expectationDocuments.fulfill() - client.deleteDocument(documentId: String(documentId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - } else { - client.processDocumentURL(fileUrl: url, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let documentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - client.deleteDocument(documentId: String(documentId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - case .failure(let error): - print(error) - } - expectationDocuments.fulfill() - }) - } - - wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) - } - - func testGetDocumentLineItems() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocumentLineItems") - } - - let expectation = XCTestExpectation(description: "Get all line items from document") - let documentId = 63480993 - client.getDocumentLineItems(documentId: String(documentId), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertNotNil(jsonResponse!["line_items"]) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testGetLineItem() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getLineItem") - } - - let expectation = XCTestExpectation(description: "Get line item from document") - let documentId = 63480993 - let lineItemId = 190399931 - client.getLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testAddLineItem() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addLineItem") - } - - let expectation = XCTestExpectation(description: "Add line item to document") - let documentId = 63480993 - let params = AddLineItem(order: 20, description: "Test", total: 44.0) - params.sku = "testsku" - client.addLineItem(documentId: String(documentId), params: params, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - if mockResponses { - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - } else { - XCTAssertEqual(jsonResponse!["total"] as? Float, params.total) - XCTAssertEqual(jsonResponse!["description"] as? String, params.description) - } - case .failure(let error): - print(error) - XCTFail() - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } - - func testUpdateLineItem() { - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addLineItem") - } - - let expectation1 = XCTestExpectation(description: "Add line item to document") - let documentId = 63480993 - var lineItemId = 0 - let params1 = AddLineItem(order: 20, description: "Test", total: 44.0) - params1.sku = "testsku" - client.addLineItem(documentId: String(documentId), params: params1, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - if mockResponses { - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - } else { - lineItemId = jsonResponse!["id"] as? Int ?? 0 - } - case .failure(let error): - print(error) - XCTFail() - } - expectation1.fulfill() - }) - wait(for: [expectation1], timeout: 20.0) - - let expectation2 = XCTestExpectation(description: "Update line item to document") - let params2 = UpdateLineItem() - params2.description = "Test" - client.updateLineItem(documentId: String(documentId), lineItemId: String(lineItemId), params: params2, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - if mockResponses { - XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2) - } else { - XCTAssertEqual(jsonResponse!["description"] as? String, params2.description) - } - case .failure(let error): - print(error) - XCTFail() - } - expectation2.fulfill() - }) - - wait(for: [expectation2], timeout: 20.0) - } - - func testDeleteDocumentLineItems() { - let expectationDocuments = XCTestExpectation(description: "Get documents") - let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteDocumentLineItems") - let documentId = 63480993 - expectationDocuments.fulfill() - client.deleteDocumentLineItems(documentId: String(documentId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - } else { - client.processDocumentURL(fileUrl: url, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let documentId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - client.deleteDocumentLineItems(documentId: String(documentId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - case .failure(let error): - print(error) - } - expectationDocuments.fulfill() - }) - } - - wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) - } - - func testDeleteLineItem() { - let expectationDocuments = XCTestExpectation(description: "Get documents") - let expectationDelete = XCTestExpectation(description: "Get a JSON response from deleted document") - - if (mockResponses) { - client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "deleteLineItem") - let documentId = 63480993 - let lineItemId = 190399931 - expectationDocuments.fulfill() - client.deleteLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - } else { - let documentId = 63480993 - let params = AddLineItem(order: 20, description: "Test", total: 44.4) - params.sku = "testsku" - client.addLineItem(documentId: String(documentId), params: params, withCompletion: { result in - switch result { - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let lineItemId = jsonResponse?["id"] as? Int64 else { - XCTFail() - return - } - client.deleteLineItem(documentId: String(documentId), lineItemId: String(lineItemId), withCompletion: { result in - switch result { - case .success(let data): - let jsonStringDeleteResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonStringDeleteResponse?["status"] as? String else { - XCTFail() - return - } - XCTAssertEqual("ok", status) - case .failure(let error): - print(error) - XCTAssertTrue(false) - } - expectationDelete.fulfill() - }) - case .failure(let error): - print(error) - } - expectationDocuments.fulfill() - }) - } - - wait(for: [expectationDocuments, expectationDelete], timeout: 40.0) - } - - func testBadCredentials() { - let expectation = XCTestExpectation(description: "Get response to a bad credential case") - let badClient = Client(clientId: "badClientId", clientSecret: "badClientSecret", username: "badUsername", apiKey: "badApiKey") - badClient.getDocuments(withCompletion: { result in - switch result{ - case .success(let data): - let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - guard let status = jsonResponse?["status"] as? String, let message = jsonResponse?["message"] as? String else { - XCTFail() - return - } - XCTAssertEqual("fail", status) - XCTAssertEqual("Not Authorized", message) - case .failure(let error): - print(error) - } - expectation.fulfill() - }) - - wait(for: [expectation], timeout: 20.0) - } -} +final class VeryfiSDKTests: XCTestCase {}