Skip to content
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

Apply swift-format to StripeCore #2055

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 100,
"maximumBlankLines" : 1,
"prioritizeKeepingFunctionOutputTogether" : false,
"prioritizeKeepingFunctionOutputTogether" : true,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLowerCamelCase" : true,
"AlwaysUseLowerCamelCase" : false,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
Expand All @@ -39,7 +39,7 @@
"NoVoidReturnOnFunctionSignature" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OnlyOneTrailingClosureArgument" : false,
"OrderedImports" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseLetInEveryBoundCaseVariable" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import Foundation

/// This is an object representing an empty response from a request
/// This is an object representing an empty response from a request.
@_spi(STP) public struct EmptyResponse: UnknownFieldsDecodable {
public var _allResponseFieldsStorage: NonEncodableParameters?
}

30 changes: 18 additions & 12 deletions StripeCore/StripeCore/Source/API Bindings/Models/StripeFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@

import Foundation


/** This is an object representing a file hosted on Stripe's servers. The file may have been uploaded by yourself using the [create file](https://stripe.com/docs/api#create_file) request (for example, when uploading dispute evidence) or it may have been created by Stripe (for example, the results of a [Sigma scheduled query](#scheduled_queries)). Related guide: [File Upload Guide](https://stripe.com/docs/file-upload). */

/// This is an object representing a file hosted on Stripe's servers.
///
/// The file may have been uploaded by yourself using the
/// [create file](https://stripe.com/docs/api#create_file) request
/// (for example, when uploading dispute evidence) or it may have been created by Stripe
/// (for example, the results of a [Sigma scheduled query](#scheduled_queries)).
/// Related guide: [File Upload Guide](https://stripe.com/docs/file-upload).
@_spi(STP) public struct StripeFile: UnknownFieldsDecodable, Equatable {
@frozen public enum Purpose: String, SafeEnumCodable, Equatable {
// NOTE: If adding cases here that should also be available to the
// public API, please also add to `STPFilePurpose`. This is not
// necessary for cases that are only used internally.

/// Dispute evidence file
/// Dispute evidence file.
case disputeEvidence = "dispute_evidence"
/// Identity document file
/// Identity document file.
case identityDocument = "identity_document"
/// Identity document file used only internally
/// Identity document file used only internally.
case identityPrivate = "identity_private"
/// Not a valid purpose – only used for `SafeEnumCodable` conformance
/// Not a valid purpose – only used for `SafeEnumCodable` conformance.
case unparsable = ""
}
/** Time at which the object was created. Measured in seconds since the Unix epoch. */
/// Time at which the object was created.
///
/// Measured in seconds since the Unix epoch.
public let created: Date
/** Unique identifier for the object. */
/// Unique identifier for the object.
public let id: String
/** The [purpose](https://stripe.com/docs/file-upload#uploading-a-file) of the uploaded file. */
/// The [purpose](https://stripe.com/docs/file-upload#uploading-a-file) of the uploaded file.
public let purpose: Purpose
/** The size in bytes of the file object. */
/// The size in bytes of the file object.
public let size: Int
/** The type of the file returned (e.g., `csv`, `pdf`, `jpg`, or `png`). */
/// The type of the file returned (e.g., `csv`, `pdf`, `jpg`, or `png`).
public let type: String?
public var _allResponseFieldsStorage: NonEncodableParameters?
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import Foundation
import UIKit

extension StripeFile.Purpose {
/// See max purpose sizes https://stripe.com/docs/file-upload
/// See max purpose sizes https://stripe.com/docs/file-upload.
var maxBytes: Int? {
switch self {
case .identityDocument,
.identityPrivate:
.identityPrivate:
return 16_000_000
case .disputeEvidence:
return 5_000_000
Expand All @@ -31,7 +31,7 @@ extension STPAPIClient {
metrics: ImageUploadMetrics
)

/// Metrics returned in callback after image is uploaded to track performance
/// Metrics returned in callback after image is uploaded to track performance.
@_spi(STP) public struct ImageUploadMetrics {
public let timeToUpload: TimeInterval
public let fileSizeBytes: Int
Expand All @@ -52,31 +52,29 @@ extension STPAPIClient {
).imageData
}

/**
Uses the Stripe file upload API to upload a JPEG encoded image.

The image will be automatically resized down if:
1. The given purpose is recognized by the client.
2. It's larger than the maximum allowed file size for the given purpose.

- Parameters:
- image: The image to be uploaded.
- compressionQuality: The compression quality to use when encoding the jpeg.
- purpose: The purpose of this file.
- fileName: The name of the uploaded file. The "jpeg" extension will
automatically be appended to this name.
- ownedBy: A Stripe-internal property that sets the owner of the file.
- ephemeralKeySecret: Authorization key, if applicable.
- completion: The callback to run with the returned Stripe file (and any
errors that may have occurred).

- Note:
The provided `purpose` must match a supported Purpose by Stripe's File
Upload API, or the API will respond with an error. Generally, this should
match a value in `StripeFile.Purpose`, but can be specified by any string
when forwarding the value from a Stripe server response in situations
where the purpose is not yet encoded in the client SDK.
*/
/// Uses the Stripe file upload API to upload a JPEG encoded image.
///
/// The image will be automatically resized down if:
/// 1. The given purpose is recognized by the client.
/// 2. It's larger than the maximum allowed file size for the given purpose.
///
/// - Parameters:
/// - image: The image to be uploaded.
/// - compressionQuality: The compression quality to use when encoding the jpeg.
/// - purpose: The purpose of this file.
/// - fileName: The name of the uploaded file. The "jpeg" extension will
/// automatically be appended to this name.
/// - ownedBy: A Stripe-internal property that sets the owner of the file.
/// - ephemeralKeySecret: Authorization key, if applicable.
/// - completion: The callback to run with the returned Stripe file (and any
/// errors that may have occurred).
///
/// - Note:
/// The provided `purpose` must match a supported Purpose by Stripe's File
/// Upload API, or the API will respond with an error. Generally, this should
/// match a value in `StripeFile.Purpose`, but can be specified by any string
/// when forwarding the value from a Stripe server response in situations
/// where the purpose is not yet encoded in the client SDK.
@_spi(STP) public func uploadImage(
_ image: UIImage,
compressionQuality: CGFloat = UIImage.defaultCompressionQuality,
Expand Down Expand Up @@ -111,7 +109,7 @@ extension STPAPIClient {
purposePart.name = "purpose"
// `unparsable` is not a valid purpose
if purpose != StripeFile.Purpose.unparsable.rawValue,
let purposeData = purpose.data(using: .utf8)
let purposeData = purpose.data(using: .utf8)
{
purposePart.data = purposeData
}
Expand All @@ -126,7 +124,8 @@ extension STPAPIClient {
purpose: purpose
)

let ownedByPart: STPMultipartFormDataPart? = ownedBy?.data(using: .utf8).map { ownedByData in
let ownedByPart: STPMultipartFormDataPart? = ownedBy?.data(using: .utf8).map {
ownedByData in
let part = STPMultipartFormDataPart()
part.name = "owned_by"
part.data = ownedByData
Expand All @@ -136,7 +135,9 @@ extension STPAPIClient {
let boundary = STPMultipartFormDataEncoder.generateBoundary()
let parts = [purposePart, ownedByPart, imagePart].compactMap { $0 }
let data = STPMultipartFormDataEncoder.multipartFormData(
for: parts, boundary: boundary)
for: parts,
boundary: boundary
)

var request = configuredRequest(
for: URL(string: FileUploadURL)!,
Expand All @@ -146,44 +147,49 @@ extension STPAPIClient {
request.stp_setMultipartForm(data, boundary: boundary)

let requestStartTime = Date()
sendRequest(request: request, completion: { (result: Result<StripeFile, Error>) in
let timeToUpload = Date().timeIntervalSince(requestStartTime)
completion(result.map { (
file: $0,
metrics: .init(
timeToUpload: timeToUpload,
fileSizeBytes: imagePart.data?.count ?? 0
sendRequest(
request: request,
completion: { (result: Result<StripeFile, Error>) in
let timeToUpload = Date().timeIntervalSince(requestStartTime)
completion(
result.map {
(
file: $0,
metrics: .init(
timeToUpload: timeToUpload,
fileSizeBytes: imagePart.data?.count ?? 0
)
)
}
)
) })
})
}
)
}

/**
Uses the Stripe file upload API to upload a JPEG encoded image.

The image will be automatically resized down if:
1. The given purpose is recognized by the client.
2. It's larger than the maximum allowed file size for the given purpose.

- Parameters:
- image: The image to be uploaded.
- compressionQuality: The compression quality to use when encoding the jpeg.
- purpose: The purpose of this file.
- fileName: The name of the uploaded file. The "jpeg" extension will
automatically be appended to this name.
- ownedBy: A Stripe-internal property that sets the owner of the file.
- ephemeralKeySecret: Authorization key, if applicable.

- Returns: A promise that resolves to a Stripe file, if successful, or an
error that may have occurred.

- Note:
The provided `purpose` must match a supported Purpose by our API or the
API will return an error. Generally, this should match a value in
`StripeFile.Purpose`, but can be specified by any string for instances
where a Stripe endpoint needs to specify a newer purpose that the client
SDK does not recognize.
*/
/// Uses the Stripe file upload API to upload a JPEG encoded image.
///
/// The image will be automatically resized down if:
/// 1. The given purpose is recognized by the client.
/// 2. It's larger than the maximum allowed file size for the given purpose.
///
/// - Parameters:
/// - image: The image to be uploaded.
/// - compressionQuality: The compression quality to use when encoding the jpeg.
/// - purpose: The purpose of this file.
/// - fileName: The name of the uploaded file. The "jpeg" extension will
/// automatically be appended to this name.
/// - ownedBy: A Stripe-internal property that sets the owner of the file.
/// - ephemeralKeySecret: Authorization key, if applicable.
///
/// - Returns: A promise that resolves to a Stripe file, if successful, or an
/// error that may have occurred.
///
/// - Note:
/// The provided `purpose` must match a supported Purpose by our API or the
/// API will return an error. Generally, this should match a value in
/// `StripeFile.Purpose`, but can be specified by any string for instances
/// where a Stripe endpoint needs to specify a newer purpose that the client
/// SDK does not recognize.
@_spi(STP) public func uploadImage(
_ image: UIImage,
compressionQuality: CGFloat = UIImage.defaultCompressionQuality,
Expand Down
Loading