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

Add an argument to use ephemeral URLSession to send WP.com API requests #828

Merged
merged 2 commits into from
Jan 29, 2025
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
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
Loading