-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Encodable
POST types
#1151
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
01d4b9a
Remove redundant APIClient.post() and APIClient.get() methods
scannillo d1cd680
WIP - send POST to v1/payment_methods/apple_payment_tokens via Encoda…
scannillo 23176f7
WIP - move metadata encoding into custom GWEncodable class
scannillo 1f97b5d
Add comment for custom encode() implementation reference
scannillo b6537fa
Conform BTClientMetadata to Encodable
scannillo 20c9142
Cleanup - rename classes & file organization
scannillo c60265c
WIP + TODO for updating tests
scannillo a695b59
Merge branch 'main' into use-codable-api-client
scannillo d2c3d54
Cleanup unit tests
scannillo 09be532
Delete Braintree.xcworkspace/xcshareddata/swiftpm/Package.resolved
scannillo ede168f
Cleanup - remove newline
scannillo 0580f96
Merge branch 'main' into use-codable-api-client
scannillo 3d8caab
Delete Braintree.xcworkspace/xcshareddata/swiftpm/Package.resolved
scannillo 262b886
Fixup - address copy-paste typo in test
scannillo e2d52c4
PR Feedback - prefer switch to if-let syntax on enum
scannillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/BraintreeApplePay/BTApplePaymentTokensRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import PassKit | ||
|
||
/// The POST body for `v1/payment_methods/apple_payment_tokens` | ||
struct BTApplePaymentTokensRequest: Encodable { | ||
|
||
private let applePaymentToken: ApplePaymentToken | ||
|
||
init(token: PKPaymentToken) { | ||
self.applePaymentToken = ApplePaymentToken( | ||
paymentData: token.paymentData.base64EncodedString(), | ||
transactionIdentifier: token.transactionIdentifier, | ||
paymentInstrumentName: token.paymentMethod.displayName, | ||
paymentNetwork: token.paymentMethod.network?.rawValue | ||
) | ||
} | ||
|
||
private struct ApplePaymentToken: Encodable { | ||
|
||
let paymentData: String | ||
let transactionIdentifier: String | ||
let paymentInstrumentName: String? | ||
let paymentNetwork: String? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
/// An `Encodable` type containing POST body details & metadata params formatted for the BT Gateway & BT GraphQL API | ||
struct BTAPIRequest: Encodable { | ||
|
||
private let requestBody: Encodable | ||
private let metadata: BTClientMetadata | ||
private let httpType: BTAPIClientHTTPService | ||
|
||
private enum MetadataKeys: String, CodingKey { | ||
case gatewayMetadataKey = "_meta" | ||
case graphQLMetadataKey = "clientSdkMetadata" | ||
} | ||
|
||
/// Initialize a `BTAPIRequest` to format a POST body with metadata params for BT APIs. | ||
/// - Parameters: | ||
/// - requestBody: The actual POST body details. | ||
/// - metadata: The metadata details to append into the POST body. | ||
/// - httpType: The Braintree API type for this request. | ||
init(requestBody: Encodable, metadata: BTClientMetadata, httpType: BTAPIClientHTTPService) { | ||
self.requestBody = requestBody | ||
self.metadata = metadata | ||
self.httpType = httpType | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try requestBody.encode(to: encoder) | ||
|
||
var metadataContainer = encoder.container(keyedBy: MetadataKeys.self) | ||
switch httpType { | ||
case .gateway: | ||
let metadataEncoder = metadataContainer.superEncoder(forKey: .gatewayMetadataKey) | ||
try self.metadata.encode(to: metadataEncoder) | ||
case .graphQLAPI: | ||
let metadataEncoder = metadataContainer.superEncoder(forKey: .graphQLMetadataKey) | ||
try self.metadata.encode(to: metadataEncoder) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about
superEncoder
- super cool!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, it was a little tricky to figure out how to do this, but this stack overflow post is what helped me - https://stackoverflow.com/questions/50461744/swift-codable-how-to-encode-top-level-data-into-nested-container