Skip to content

Commit

Permalink
LP-958: Add w2 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Poveda committed Oct 25, 2024
1 parent 657bacd commit de1771d
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 1,033 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let package = Package(
.copy("Resources/receipt.jpeg"),
.copy("Resources/driver_license.png"),
.copy("Resources/bankstatement.pdf"),
.copy("Resources/w2.png"),
.copy("Resources/deleteDocument.json"),
.copy("Resources/getDocument.json"),
.copy("Resources/getDocuments.json"),
Expand Down
4 changes: 2 additions & 2 deletions Sources/VeryfiSDK/Client/GetW2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import Foundation

extension Client {
/// Get single document by ID from Veryfi inbox.
/// Get single w2 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 getW2(documentId: String, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItem: documentId, completion: completion)
self.request(method: .GET, route: .w2s, queryItem: documentId, completion: completion)
}
}
4 changes: 2 additions & 2 deletions Sources/VeryfiSDK/Client/GetW2s.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import Foundation

extension Client {
/// Get all documents from Veryfi inbox.
/// Get all w2 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 getW2s(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
self.request(method: .GET, route: .documents, queryItems: queryItems, completion: completion)
self.request(method: .GET, route: .w2s, queryItems: queryItems, completion: completion)
}
}
3 changes: 1 addition & 2 deletions Sources/VeryfiSDK/Client/ProcessBankStatement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ extension Client {
/// - 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.
/// - confidenceDetails: Confidence details object
/// - params: Additional parameters.
/// - completion: Function called after request completes.
/// - detail: Response from server.
Expand Down
6 changes: 1 addition & 5 deletions Sources/VeryfiSDK/Client/ProcessBankStatementUrl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ extension Client {
/// - 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.
/// - confidenceDetails: Confidence details object
/// - completion: Block executed after request.
/// - detail: Response from server.
/// - error: Error from server.
Expand Down
9 changes: 2 additions & 7 deletions Sources/VeryfiSDK/Client/ProcessW2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

extension Client {
/// Upload an image for the Veryfi API to process.
/// Upload a w2 image for the Veryfi API to process.
/// - Parameters:
/// - fileName: Name of the file to upload to the Veryfi API.
/// - fileData: UTF8 encoded file data
Expand All @@ -19,14 +19,9 @@ extension Client {
/// - error: Error from server.
public func processW2(fileName: String,
fileData: Data,
categories: [String]? = nil,
deleteAfterProcessing: Bool = false,
params: [String: Any]? = nil,
withCompletion completion: @escaping (Result<Data, APIError>) -> 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

Expand All @@ -35,6 +30,6 @@ extension Client {
return
}

self.request(method: .POST, route: .documents, uploadData: jsonData, completion: completion)
self.request(method: .POST, route: .w2s, uploadData: jsonData, completion: completion)
}
}
20 changes: 6 additions & 14 deletions Sources/VeryfiSDK/Client/ProcessW2Url.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

extension Client {
/// Upload document to Veryfi API with URL.
/// Upload w2 document to Veryfi API with URL.
/// - Parameters:
/// - fileUrl: Publicly available URL.
/// - fileUrls: List of publicly available URLs.
Expand All @@ -21,20 +21,12 @@ extension Client {
/// - error: Error from server.
public func processW2URL(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<Data, APIError>) -> 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 params: [String: Any] = [
"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: .documents, body: jsonData, completion: completion)
self.request(method: .POST, route: .w2s, body: jsonData, completion: completion)
}
}
1 change: 1 addition & 0 deletions Sources/VeryfiSDK/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum Endpoint: String {
case documents = "/partner/documents/"
case anyDocuments = "/partner/any-documents/"
case bankStatements = "/partner/bank-statements/"
case w2s = "/partner/w2s/"
}

public class NetworkManager {
Expand Down
4 changes: 4 additions & 0 deletions Tests/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ extension VeryfiSDKTests {
("testGetBankStatement", testGetBankStatement),
("testProcessBankStatement" testProcessBankStatement),
("testProcessBankStatementURL", testProcessBankStatementURL),
("testGetW2s", testGetW2s),
("testGetW2", testGetW2),
("testProcessW2", testProcessW2),
("testProcessW2URL", testProcessW2URL),
("testAddTag", testAddTag),
("testAddTags", testAddTags),
("testUpdateDocument", testUpdateDocument),
Expand Down
14 changes: 7 additions & 7 deletions Tests/VeryfiSDKTests/GetW2Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import FoundationNetworking

extension VeryfiSDKTests {
func testGetW2() {
let expectation = XCTestExpectation(description: "Get a document by id in a JSON")
let expectation = XCTestExpectation(description: "Get a w2 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
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getW2")
let id = Int64(4609400)
client.getW2(documentId: String(id), withCompletion: { result in
switch result {
case .success(let data):
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
Expand All @@ -27,15 +27,15 @@ extension VeryfiSDKTests {
expectation.fulfill()
})
} else {
client.getDocuments(queryItems: [URLQueryItem(name: "page_size", value: "1")], withCompletion: { result in
client.getW2s(queryItems: [URLQueryItem(name: "page_size", value: "1")], 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 {
guard let id = ((jsonDocuments?["results"] as? NSArray)?[0] as? NSDictionary)?["id"] as? Int64 else {
XCTFail()
return
}
client.getDocument(documentId: String(id), withCompletion: { result in
client.getW2(documentId: String(id), withCompletion: { result in
switch result {
case .success(let data):
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
Expand Down
6 changes: 3 additions & 3 deletions Tests/VeryfiSDKTests/GetW2sTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import FoundationNetworking
extension VeryfiSDKTests {
func testGetW2s() {
if (mockResponses) {
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments")
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getW2s")
}

let expectation = XCTestExpectation(description: "Get all documents in a JSON array")
let expectation = XCTestExpectation(description: "Get all w2 documents in a JSON array")

client.getDocuments(withCompletion: { result in
client.getW2s(withCompletion: { result in
switch result {
case .success(let data):
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
Expand Down
14 changes: 7 additions & 7 deletions Tests/VeryfiSDKTests/ProcessW2Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import FoundationNetworking
extension VeryfiSDKTests {
func testProcessW2() {
if (mockResponses) {
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument")
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processW2")
}

let expectation = XCTestExpectation(description: "Get data from processing document")
let expectation = XCTestExpectation(description: "Get data from processing a w2 document")

let url = Bundle.module.url(forResource: "receipt", withExtension: "jpeg")!
let url = Bundle.module.url(forResource: "w2", withExtension: "png")!
let fileData = try? Data(contentsOf: url)
let categories = ["Advertising & Marketing", "Automotive"]
client.processDocument(fileName: file, fileData: fileData!, categories: categories, deleteAfterProcessing: true, withCompletion: { result in
client.processW2(fileName: file, fileData: fileData!, 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 {
guard let employerName = jsonResponse?["employer_name"] as? String else {
XCTFail()
break
}
XCTAssertEqual("Walgreens", vendor)

XCTAssertEqual("The Big Company", employerName)
case .failure(let error):
print(error)
XCTFail()
Expand Down
12 changes: 6 additions & 6 deletions Tests/VeryfiSDKTests/ProcessW2UrlTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import FoundationNetworking
extension VeryfiSDKTests {
func testProcessW2URL() {
if (mockResponses) {
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processDocument")
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "processW2")
}

let expectation = XCTestExpectation(description: "Get a JSON response from process document")
let expectation = XCTestExpectation(description: "Get a JSON response from process a w2 document")

let categories = ["Advertising & Marketing", "Automotive"]
client.processDocumentURL(fileUrl: url, categories: categories, deleteAfterProcessing: true, boostMode: 1, withCompletion: { result in
client.processW2URL(fileUrl: w2Url, 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 {
guard let employerName = jsonResponse?["employer_name"] as? String else {
XCTFail()
break
}
XCTAssertEqual("Walgreens", vendor)

XCTAssertEqual("The Big Company", employerName)
case .failure(let error):
print(error)
XCTFail()
Expand Down
Loading

0 comments on commit de1771d

Please sign in to comment.