Skip to content

Commit

Permalink
fix(GiniBankSDK): Adjust to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-gini committed Jan 14, 2025
1 parent f5083e7 commit 54e23e9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ extension Document {
deviceOrientation: String,
rotation: String
) -> String {
var comment = "\(userCommentPlatform)=iOS"
let data = [
(userCommentPlatform, "iOS"),
(userCommentOSVer, osVersion),
(userCommentGiniVersionVer, giniVersion),
(userCommentContentId, contentId),
Expand All @@ -74,23 +74,47 @@ extension Document {
(userCommentDeviceOrientation, deviceOrientation),
(userCommentRotation, rotation)
]
return Self.userComment(createFrom: data)
}

static func userComment(createFrom data: [(key: String, value: String)]) -> String {
var comment = ""
for (paramName, value) in data {
if !value.isEmpty {
comment += ",\(paramName)=\(value)"
guard !paramName.isEmpty, !value.isEmpty else { continue }
if !comment.isEmpty {
comment += ","
}
comment += "\(paramName)=\(value)"
}
return comment
}

static func userComment(_ existingComment: String, valueAtKey wantedKey: String) -> String? {
let keyValues = existingComment
.split(separator: ",")
.map { $0.split(separator: "=") }
.filter { $0.count == 2 }
.map { ($0[0], $0[1]) }
let keyValuesStrings = keyValues
.map { (String($0.0), String($0.1)) }
guard let value = keyValuesStrings.first(where: { $0.0 == wantedKey })?.1 else {
return nil
}

return value
}

static func userComment(_ existingComment: String, valuePresentAtKey value: String) -> Bool {
return userComment(existingComment, valueAtKey: value) != nil
}

static func userComment(_ existingComment: String?, addingIfNotPresent value: String, forKey key: String) -> String {
let newValueString = "\(key)=\(value)"
guard let existingComment, existingComment.isNotEmpty else {
return newValueString
}
// checking if the key doesn't exist already
guard !existingComment
.split(separator: ",").map(String.init)
.split(separator: "=").map(String.init)
.contains(key) else {

guard !userComment(existingComment, valuePresentAtKey: key) else {
return existingComment
}
return existingComment + "," + newValueString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension Document {
*/
public struct Metadata {
var headers: [String: String] = [:]
var giniBankVersion: String?
var giniBankSDKVersion: String?
static let headerKeyPrefix = "X-Document-Metadata-"
static let branchIdHeaderKey = "BranchId"
static let uploadHeaderKey = "Upload"
Expand All @@ -256,7 +256,7 @@ extension Document {
if let branchId = branchId {
headers[Document.Metadata.headerKeyPrefix + Document.Metadata.branchIdHeaderKey] = branchId
}
self.giniBankVersion = bankSDKVersion
self.giniBankSDKVersion = bankSDKVersion
var comment = uploadMetadata?.userComment
if let bankSDKVersion {
comment = UploadMetadata.userComment(comment, addingIfNotPresent: bankSDKVersion, forKey: "GiniBankVer")
Expand All @@ -271,25 +271,25 @@ extension Document {
/**
* Adds GiniBankSDK version to upload metadata
*
* - Parameter giniBankVersion: GiniBankSDKVersion
* - Parameter giniBankSDKVersion: GiniBankSDKVersion
*/
public mutating func addGiniBankSDKVersion(_ giniBankVersion: String) {
self.giniBankVersion = giniBankVersion
public mutating func addGiniBankSDKVersion(_ giniBankSDKVersion: String) {
self.giniBankSDKVersion = giniBankSDKVersion
let key = Document.Metadata.headerKeyPrefix + Document.Metadata.uploadHeaderKey
let existingValue = headers[key]

headers[Document.Metadata.headerKeyPrefix + Document.Metadata.uploadHeaderKey] = UploadMetadata.userComment(existingValue, addingIfNotPresent: giniBankVersion, forKey: "GiniBankVer")
headers[Document.Metadata.headerKeyPrefix + Document.Metadata.uploadHeaderKey] = UploadMetadata.userComment(existingValue, addingIfNotPresent: giniBankSDKVersion, forKey: "GiniBankVer")
}

/**
* Adds upload metadata
*
* - Parameter uploadMetadata: Upload datadata
* - Parameter uploadMetadata: Upload metadata
*/
public mutating func addUploadMetadata(_ uploadMetadata: UploadMetadata) {
var comment = uploadMetadata.userComment
if let giniBankVersion {
comment = UploadMetadata.userComment(comment, addingIfNotPresent: giniBankVersion, forKey: "GiniBankVer")
if let giniBankSDKVersion {
comment = UploadMetadata.userComment(comment, addingIfNotPresent: giniBankSDKVersion, forKey: "GiniBankVer")
}
headers[Document.Metadata.headerKeyPrefix + Document.Metadata.uploadHeaderKey] = comment
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,37 @@ final class GiniDocumentTests: XCTestCase {
"userComment should match; expected \"\(uploadMetadata.userComment)\", got \"\(metadataValue)\""
)
}

func testUploadMetadataUserCommentCreationTest() {
let data = [
("k1", "v1"),
("k2", "v2"),
("k3", ""), // invalid value
("", "v4"), // invalid key
("", "") // invalid everything
]
let expectedValue = "k1=v1,k2=v2"
XCTAssertEqual(Document.UploadMetadata.userComment(createFrom: data), expectedValue, "The string should be in correct format")
}

func testUploadMetadaUserCommentKeyExistence() {
let data = "k1=v1,k2=v2"
XCTAssertTrue(Document.UploadMetadata.userComment(data, valuePresentAtKey: "k1"), "The key should exist")
XCTAssertEqual(Document.UploadMetadata.userComment(data, valueAtKey: "k1"), "v1", "The values should be equal")

XCTAssertFalse(Document.UploadMetadata.userComment(data, valuePresentAtKey: "v1"), "The value should not be treated as key")
XCTAssertNil(Document.UploadMetadata.userComment(data, valueAtKey: "v1"), "The value should not be treated as key and shall not exist")

XCTAssertFalse(Document.UploadMetadata.userComment(data, valuePresentAtKey: "keyThatNeverDidDoesAndWillNotExist"), "The key should not exist")
XCTAssertNil(Document.UploadMetadata.userComment(data, valueAtKey: "keyThatNeverDidDoesAndWillNotExist"), "The value should not exist")
}

func testUploadMetadataUserCommentKeyAddingIfNotPresent() {
let data = "k1=v1,k2=v2"
XCTAssertEqual(Document.UploadMetadata.userComment(data, addingIfNotPresent: "v3", forKey: "k1"), data, "The data should not be changed")

let expectedData = "k1=v1,k2=v2,k3=v3"
XCTAssertEqual(Document.UploadMetadata.userComment(data, addingIfNotPresent: "v3", forKey: "k3"), expectedData, "The data should be changed")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,7 @@ open class GiniBankNetworkingScreenApiCoordinator: GiniScreenAPICoordinator, Gin
api: APIDomain,
trackingDelegate: GiniCaptureTrackingDelegate?,
lib: GiniBankAPI) {
let docMetadata: Document.Metadata = {
guard var documentMetadata else {
return .init(bankSDKVersion: GiniBankSDKVersion)
}
documentMetadata.addGiniBankSDKVersion(GiniBankSDKVersion)
return documentMetadata
}()
documentService = DocumentService(lib: lib, metadata: docMetadata)
documentService = DocumentService(lib: lib, metadata: Self.makeMetadata(with: documentMetadata))
configurationService = lib.configurationService()
let captureConfiguration = configuration.captureConfiguration()
super.init(withDelegate: nil, giniConfiguration: captureConfiguration)
Expand All @@ -163,14 +156,7 @@ open class GiniBankNetworkingScreenApiCoordinator: GiniScreenAPICoordinator, Gin
documentMetadata: Document.Metadata?,
trackingDelegate: GiniCaptureTrackingDelegate?,
lib: GiniBankAPI) {
let docMetadata: Document.Metadata = {
guard var documentMetadata else {
return .init(bankSDKVersion: GiniBankSDKVersion)
}
documentMetadata.addGiniBankSDKVersion(GiniBankSDKVersion)
return documentMetadata
}()
documentService = DocumentService(lib: lib, metadata: docMetadata)
documentService = DocumentService(lib: lib, metadata: Self.makeMetadata(with: documentMetadata))
configurationService = lib.configurationService()
let captureConfiguration = configuration.captureConfiguration()
super.init(withDelegate: nil, giniConfiguration: captureConfiguration)
Expand All @@ -189,15 +175,8 @@ open class GiniBankNetworkingScreenApiCoordinator: GiniScreenAPICoordinator, Gin
trackingDelegate: GiniCaptureTrackingDelegate?,
captureNetworkService: GiniCaptureNetworkService,
configurationService: ClientConfigurationServiceProtocol?) {
let docMetadata: Document.Metadata = {
guard var documentMetadata else {
return .init(bankSDKVersion: GiniBankSDKVersion)
}
documentMetadata.addGiniBankSDKVersion(GiniBankSDKVersion)
return documentMetadata
}()
documentService = DocumentService(giniCaptureNetworkService: captureNetworkService,
metadata: docMetadata)
metadata: Self.makeMetadata(with: documentMetadata))
self.configurationService = configurationService
let captureConfiguration = configuration.captureConfiguration()

Expand Down Expand Up @@ -832,3 +811,14 @@ extension GiniBankNetworkingScreenApiCoordinator {
.setTransactionDocsDocumentPagesViewModel(viewModel, for: documentId)
}
}

// MARK: - Document Metadata creation
extension GiniBankNetworkingScreenApiCoordinator {
private static func makeMetadata(with documentMetadata: Document.Metadata?) -> Document.Metadata {
guard var documentMetadata else {
return .init(bankSDKVersion: GiniBankSDKVersion)
}
documentMetadata.addGiniBankSDKVersion(GiniBankSDKVersion)
return documentMetadata
}
}

0 comments on commit 54e23e9

Please sign in to comment.