Skip to content

Commit

Permalink
Add an argument to use ephemeral URLSession to send WP.com API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Jan 28, 2025
1 parent a9a057e commit fe83ee7
Showing 1 changed file with 13 additions and 2 deletions.
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 fe83ee7

Please sign in to comment.