Skip to content

Commit de68db3

Browse files
authored
Merge pull request #14 from veryfi/feature/LP-136-query-orderby
Feature/lp 136 query orderby
2 parents a02e5d2 + 245bac7 commit de68db3

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

Sources/VeryfiSDK/Client.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public class Client: NetworkManager {
2626

2727
/// Get all documents from Veryfi inbox.
2828
/// - Parameters:
29+
/// - queryItems: Query items to apply to the get request.
2930
/// - completion: Block executed after request.
3031
/// - detail: Response from server.
3132
/// - error: Error from server.
32-
public func getDocuments(withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
33-
self.request(method: .GET, route: .documents, completion: completion)
33+
public func getDocuments(queryItems: [URLQueryItem]? = nil, withCompletion completion: @escaping (Result<Data, APIError>) -> Void) {
34+
self.request(method: .GET, route: .documents, queryItems: queryItems, completion: completion)
3435
}
3536

3637
/// Get single document by ID from Veryfi inbox.

Sources/VeryfiSDK/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
import Foundation
99

1010
struct Constants {
11-
static let packageVersion = "1.1.0"
11+
static let packageVersion = "1.2.0"
1212
}

Sources/VeryfiSDK/NetworkManager.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public class NetworkManager {
5252
if let queryItem = queryItem, queryItem != "" {
5353
apiUrl = apiUrl + queryItem + "/"
5454
}
55-
guard let url = URL(string: apiUrl) else {
55+
var urlComponents = URLComponents(string: apiUrl)
56+
if let queryItems = queryItems, !queryItems.isEmpty {
57+
urlComponents?.queryItems = queryItems
58+
}
59+
60+
guard let url = urlComponents?.url else {
5661
completion(.failure(.internalError))
5762
return
5863
}

Tests/VeryfiSDKTests/VeryfiSDKTests.swift

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ final class VeryfiSDKTests: XCTestCase {
5353
wait(for: [expectation], timeout: 20.0)
5454
}
5555

56+
func testGetDocumentWithQueryItems() {
57+
if (mockResponses) {
58+
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "getDocuments")
59+
}
60+
61+
let expectation = XCTestExpectation(description: "Get all documents in a JSON array")
62+
let queryItems = [URLQueryItem(name: "order_by", value: "created")]
63+
client.getDocuments(queryItems: queryItems, withCompletion: { result in
64+
switch result {
65+
case .success(let data):
66+
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
67+
XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2)
68+
case .failure(let error):
69+
print(error)
70+
XCTAssertTrue(false)
71+
}
72+
expectation.fulfill()
73+
})
74+
75+
wait(for: [expectation], timeout: 20.0)
76+
}
77+
5678
func testGetDocument() {
5779
let expectation = XCTestExpectation(description: "Get a document by id in a JSON")
5880

@@ -326,7 +348,7 @@ final class VeryfiSDKTests: XCTestCase {
326348

327349
let expectation = XCTestExpectation(description: "Add line item to document")
328350
let documentId = 63480993
329-
let params = AddLineItem(order: 20, description: "Test", total: 44.4)
351+
let params = AddLineItem(order: 20, description: "Test", total: 44.0)
330352
params.sku = "testsku"
331353
client.addLineItem(documentId: String(documentId), params: params, withCompletion: { result in
332354
switch result {
@@ -353,28 +375,48 @@ final class VeryfiSDKTests: XCTestCase {
353375
client = ClientSpy(clientId: clientId, clientSecret: clientSecret, username: username, apiKey: apiKey, resource: "addLineItem")
354376
}
355377

356-
let expectation = XCTestExpectation(description: "Add line item to document")
378+
let expectation1 = XCTestExpectation(description: "Add line item to document")
357379
let documentId = 63480993
358-
let lineItemId = 190399931
359-
let params = UpdateLineItem()
360-
params.description = "Test"
361-
client.updateLineItem(documentId: String(documentId), lineItemId: String(lineItemId), params: params, withCompletion: { result in
380+
var lineItemId = 0
381+
let params1 = AddLineItem(order: 20, description: "Test", total: 44.0)
382+
params1.sku = "testsku"
383+
client.addLineItem(documentId: String(documentId), params: params1, withCompletion: { result in
362384
switch result {
363385
case .success(let data):
364386
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
365387
if mockResponses {
366388
XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2)
367389
} else {
368-
XCTAssertEqual(jsonResponse!["description"] as? String, params.description)
390+
lineItemId = jsonResponse!["id"] as? Int ?? 0
369391
}
370392
case .failure(let error):
371393
print(error)
372394
XCTFail()
373395
}
374-
expectation.fulfill()
396+
expectation1.fulfill()
375397
})
398+
wait(for: [expectation1], timeout: 20.0)
376399

377-
wait(for: [expectation], timeout: 20.0)
400+
let expectation2 = XCTestExpectation(description: "Update line item to document")
401+
let params2 = UpdateLineItem()
402+
params2.description = "Test"
403+
client.updateLineItem(documentId: String(documentId), lineItemId: String(lineItemId), params: params2, withCompletion: { result in
404+
switch result {
405+
case .success(let data):
406+
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
407+
if mockResponses {
408+
XCTAssertGreaterThanOrEqual(jsonResponse!.count, 2)
409+
} else {
410+
XCTAssertEqual(jsonResponse!["description"] as? String, params2.description)
411+
}
412+
case .failure(let error):
413+
print(error)
414+
XCTFail()
415+
}
416+
expectation2.fulfill()
417+
})
418+
419+
wait(for: [expectation2], timeout: 20.0)
378420
}
379421

380422
func testDeleteDocumentLineItems() {

0 commit comments

Comments
 (0)