Skip to content

Commit

Permalink
Add an argument to use ephemeral URLSession to send WP.com API reques…
Browse files Browse the repository at this point in the history
…ts (#828)
  • Loading branch information
crazytonyli authored Jan 29, 2025
2 parents a9a057e + 253b695 commit cd1458b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ let package = Package(
targets: [
.binaryTarget(
name: "WordPressKit",
url: "https://github.com/user-attachments/files/18379301/WordPressKit.zip",
checksum: "afd882de3a6a672c32c6cc7e6e1c1e68ff4e1366e7613af4eab59415ed7abb59"
url: "https://github.com/user-attachments/files/18570063/WordPressKit.zip",
checksum: "fc25d3065e80af713dac970db7ed89ff37e4cc98afc98b6a2ecf7b47b2ddd0c1"
),
]
)
15 changes: 13 additions & 2 deletions Sources/CoreAPI/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ open class WordPressComRestApi: NSObject {

private var invalidTokenHandler: (() -> Void)?

private var useEphemeralSession: Bool

/**
Configure whether or not the user's preferred language locale should be appended. Defaults to true.
*/
Expand Down Expand Up @@ -139,14 +141,16 @@ open class WordPressComRestApi: NSObject {
backgroundSessionIdentifier: String = WordPressComRestApi.defaultBackgroundSessionIdentifier,
sharedContainerIdentifier: String? = nil,
localeKey: String = WordPressComRestApi.LocaleKeyDefault,
baseURL: URL = WordPressComRestApi.apiBaseURL) {
baseURL: URL = WordPressComRestApi.apiBaseURL,
useEphemeralSession: Bool = false) {
self.oAuthToken = oAuthToken
self.userAgent = userAgent
self.backgroundUploads = backgroundUploads
self.backgroundSessionIdentifier = backgroundSessionIdentifier
self.sharedContainerIdentifier = sharedContainerIdentifier
self.localeKey = localeKey
self.baseURL = baseURL
self.useEphemeralSession = useEphemeralSession

super.init()
}
Expand Down Expand Up @@ -347,7 +351,14 @@ open class WordPressComRestApi: NSObject {
}()

private func sessionConfiguration(background: Bool) -> URLSessionConfiguration {
let configuration = background ? URLSessionConfiguration.background(withIdentifier: self.backgroundSessionIdentifier) : URLSessionConfiguration.default
let configuration: URLSessionConfiguration
if background {
configuration = .background(withIdentifier: self.backgroundSessionIdentifier)
} else if useEphemeralSession {
configuration = .ephemeral
} else {
configuration = .default
}

var additionalHeaders: [String: AnyObject] = [:]
if let oAuthToken = self.oAuthToken {
Expand Down

0 comments on commit cd1458b

Please sign in to comment.