Skip to content

Commit

Permalink
LP-958: Refactor functions and tests to multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Poveda committed Oct 25, 2024
1 parent 0e90e27 commit 444ea8a
Show file tree
Hide file tree
Showing 59 changed files with 1,622 additions and 1,216 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
367 changes: 0 additions & 367 deletions Sources/VeryfiSDK/Client.swift

This file was deleted.

21 changes: 21 additions & 0 deletions Sources/VeryfiSDK/Client/AddLineItem.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> 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)
}
}
21 changes: 21 additions & 0 deletions Sources/VeryfiSDK/Client/AddTag.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary)
self.request(method: .PUT, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion)
}
}
21 changes: 21 additions & 0 deletions Sources/VeryfiSDK/Client/AddTags.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
let jsonData = try? JSONSerialization.data(withJSONObject: params.dictionary)
self.request(method: .POST, route: .documents, body: jsonData, queryItem: String(format: "%@/tags", documentId), completion: completion)
}
}
26 changes: 26 additions & 0 deletions Sources/VeryfiSDK/Client/Client.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions Sources/VeryfiSDK/Client/DeleteDocument.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .DELETE, route: .documents, queryItem: documentId, completion: completion)
}
}
18 changes: 18 additions & 0 deletions Sources/VeryfiSDK/Client/DeleteLineItem.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion)
}
}
17 changes: 17 additions & 0 deletions Sources/VeryfiSDK/Client/DeleteLineItems.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .DELETE, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetAnyDocument.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .anyDocuments, queryItem: documentId, completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetAnyDocuments.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .anyDocuments, queryItems: queryItems, completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetBankStatement.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .bankStatements, queryItems: queryItems, queryItem: id, completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetBankStatements.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .bankStatements, queryItems: queryItems, completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetDocument.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItem: documentId, completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetDocuments.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItems: queryItems, completion: completion)
}
}
20 changes: 20 additions & 0 deletions Sources/VeryfiSDK/Client/GetLineItem.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items/%@", documentId, lineItemId), completion: completion)
}
}
19 changes: 19 additions & 0 deletions Sources/VeryfiSDK/Client/GetLineItems.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItem: String(format: "%@/line-items", documentId), completion: completion)
}
}
37 changes: 37 additions & 0 deletions Sources/VeryfiSDK/Client/ProcessAnyDocument.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> 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)
}
}
38 changes: 38 additions & 0 deletions Sources/VeryfiSDK/Client/ProcessAnyDocumentUrl.swift
Original file line number Diff line number Diff line change
@@ -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<Data, APIError>) -> 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)
}
}
Loading

0 comments on commit 444ea8a

Please sign in to comment.