Skip to content

Commit 97239b1

Browse files
Merge pull request #19 from veryfi/LP-1230-add-contract-apis
LP-1230: Add contract apis
2 parents feda1d4 + 72fb2e4 commit 97239b1

27 files changed

+750
-38
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

Package.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let package = Package(
3333
.copy("Resources/w2.png"),
3434
.copy("Resources/split.pdf"),
3535
.copy("Resources/check.pdf"),
36+
.copy("Resources/contract.pdf"),
3637
.copy("Resources/deleteDocument.json"),
3738
.copy("Resources/getDocument.json"),
3839
.copy("Resources/getDocuments.json"),
@@ -63,7 +64,12 @@ let package = Package(
6364
.copy("Resources/processCheck.json"),
6465
.copy("Resources/getCheck.json"),
6566
.copy("Resources/getChecks.json"),
66-
.copy("Resources/deleteCheck.json")
67+
.copy("Resources/deleteCheck.json"),
68+
.copy("Resources/processContract.json"),
69+
.copy("Resources/getContract.json"),
70+
.copy("Resources/getContracts.json"),
71+
.copy("Resources/updateContract.json"),
72+
.copy("Resources/deleteContract.json")
6773
]
6874
)
6975
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// DeleteContract.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Delete contract from Veryfi inbox.
11+
/// https://docs.veryfi.com/api/contracts/delete-a-contract/
12+
/// - Parameters:
13+
/// - contractId: ID of contract to delete.
14+
/// - completion: completion description
15+
public func deleteContract(contractId: Int, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
16+
self.request(method: .DELETE, route: .contracts, queryItem: String(contractId), completion: completion)
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// GetContract.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Get a specific contract from Veryfi inbox.
11+
/// https://docs.veryfi.com/api/contracts/get-a-contract/
12+
/// - Parameters:
13+
/// - contractId: ID of contract to retrieve.
14+
/// - completion: Block executed after request.
15+
/// - detail: Response from server.
16+
/// - error: Error from server.
17+
public func getContract(contractId: Int, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
18+
self.request(method: .GET, route: .contracts, queryItem: String(contractId), completion: completion)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// GetContracts.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Get all contracts from Veryfi inbox.
11+
/// https://docs.veryfi.com/api/contracts/get-contracts/
12+
/// - Parameters:
13+
/// - queryItems: Query items to apply to the get request (e.g., page, page_size).
14+
/// - completion: Block executed after request.
15+
/// - detail: Response from server.
16+
/// - error: Error from server.
17+
public func getContracts(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
18+
self.request(method: .GET, route: .contracts, queryItems: queryItems, completion: completion)
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// ProcessContract.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Upload a contract for the Veryfi API to process.
11+
/// https://docs.veryfi.com/api/contracts/process-a-contract/
12+
/// - Parameters:
13+
/// - fileName: Name of the file to upload to the Veryfi API.
14+
/// - fileData: UTF8 encoded file data
15+
/// - deleteAfterProcessing: Do not store file in Veryfi's inbox.
16+
/// - maxPagesToProcess: Limit processing to number of pages (1-50, default 50).
17+
/// - params: Additional parameters.
18+
/// - completion: Function called after request completes.
19+
/// - detail: Response from server.
20+
/// - error: Error from server.
21+
public func processContract(fileName: String,
22+
fileData: Data,
23+
deleteAfterProcessing: Bool = false,
24+
maxPagesToProcess: Int = 50,
25+
params: [String: Any]? = nil,
26+
withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
27+
var requestParams = params ?? [String: Any]()
28+
requestParams["auto_delete"] = deleteAfterProcessing
29+
requestParams["max_pages_to_process"] = maxPagesToProcess
30+
requestParams["file_data"] = fileData.base64EncodedString()
31+
requestParams["file_name"] = fileName
32+
33+
guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else {
34+
completion(.failure(.parsingError))
35+
return
36+
}
37+
38+
self.request(method: .POST, route: .contracts, uploadData: jsonData, completion: completion)
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// ProcessContractUrl.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Upload a contract from URL for the Veryfi API to process.
11+
/// https://docs.veryfi.com/api/contracts/process-a-contract/
12+
/// - Parameters:
13+
/// - fileUrl: URL of the file to upload to the Veryfi API.
14+
/// - fileUrls: Array of URLs to publicly accessible documents.
15+
/// - deleteAfterProcessing: Do not store file in Veryfi's inbox.
16+
/// - maxPagesToProcess: Limit processing to number of pages (1-50, default 50).
17+
/// - externalId: External ID to associate with the document.
18+
/// - params: Additional parameters.
19+
/// - completion: Function called after request completes.
20+
/// - detail: Response from server.
21+
/// - error: Error from server.
22+
public func processContractURL(fileUrl: String? = nil,
23+
fileUrls: [String]? = nil,
24+
deleteAfterProcessing: Bool = false,
25+
maxPagesToProcess: Int = 50,
26+
externalId: String? = nil,
27+
params: [String: Any]? = nil,
28+
withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
29+
var requestParams = params ?? [String: Any]()
30+
requestParams["auto_delete"] = deleteAfterProcessing
31+
requestParams["max_pages_to_process"] = maxPagesToProcess
32+
33+
if let fileUrl = fileUrl {
34+
requestParams["file_url"] = fileUrl
35+
}
36+
37+
if let fileUrls = fileUrls {
38+
requestParams["file_urls"] = fileUrls
39+
}
40+
41+
if let externalId = externalId {
42+
requestParams["external_id"] = externalId
43+
}
44+
45+
guard let jsonData = try? JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted) else {
46+
completion(.failure(.parsingError))
47+
return
48+
}
49+
50+
self.request(method: .POST, route: .contracts, uploadData: jsonData, completion: completion)
51+
}
52+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// UpdateContract.swift
3+
// VeryfiSDK
4+
//
5+
// Created by Veryfi on 25/10/24.
6+
//
7+
import Foundation
8+
9+
extension Client {
10+
/// Update information of contract in Veryfi inbox.
11+
/// https://docs.veryfi.com/api/contracts/update-a-contract/
12+
/// - Parameters:
13+
/// - contractId: ID of contract to modify.
14+
/// - params: Names and values to modify.
15+
/// - completion: A block to execute
16+
/// - detail: Response from server.
17+
/// - error: Error from server.
18+
public func updateContract(contractId: Int, params: [String: Any], withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
19+
let jsonData = try? JSONSerialization.data(withJSONObject: params)
20+
self.request(method: .PUT, route: .contracts, body: jsonData, queryItem: String(contractId), completion: completion)
21+
}
22+
}

Sources/VeryfiSDK/NetworkManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum Endpoint: String {
2626
case classify = "/partner/classify/"
2727
case split = "/partner/documents-set/"
2828
case checks = "/partner/checks/"
29+
case contracts = "/partner/contracts/"
2930
}
3031

3132
public class NetworkManager {

Tests/Main.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ extension VeryfiSDKTests {
4646
("testProcessCheckURL", testProcessCheckURL),
4747
("testGetCheck", testGetCheck),
4848
("testGetChecks", testGetChecks),
49-
("testDeleteCheck", testDeleteCheck)
49+
("testDeleteCheck", testDeleteCheck),
50+
("testProcessContract", testProcessContract),
51+
("testProcessContractURL", testProcessContractURL),
52+
("testGetContracts", testGetContracts),
53+
("testGetContract", testGetContract),
54+
("testDeleteContract", testDeleteContract),
55+
("testUpdateContract", testUpdateContract)
5056
]
5157
}
5258
}

0 commit comments

Comments
 (0)