Skip to content

Commit

Permalink
A couple of minor api changes (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli authored Jan 23, 2024
2 parents b4ffba5 + 299bf23 commit 5673524
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 26 deletions.
2 changes: 1 addition & 1 deletion WordPressKit/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension URLSession {
func perform<E: LocalizedError>(
request builder: HTTPRequestBuilder,
acceptableStatusCodes: [ClosedRange<Int>] = [200...299],
fulfillingProgress parentProgress: Progress? = nil,
fulfilling parentProgress: Progress? = nil,
errorType: E.Type = E.self
) async -> WordPressAPIResult<HTTPAPIResponse<Data>, E> {
if let parentProgress {
Expand Down
22 changes: 0 additions & 22 deletions WordPressKit/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ open class WordPressComRestApi: NSObject {
@objc public static let LocaleKeyDefault = "locale" // locale is specified with this for v1 endpoints
@objc public static let LocaleKeyV2 = "_locale" // locale is prefixed with an underscore for v2

@objc public static let SessionTaskKey = "WordPressComRestAPI.sessionTask"

public typealias RequestEnqueuedBlock = (_ taskID: NSNumber) -> Void
public typealias SuccessResponseBlock = (_ responseObject: AnyObject, _ httpResponse: HTTPURLResponse?) -> Void
public typealias FailureReponseBlock = (_ error: NSError, _ httpResponse: HTTPURLResponse?) -> Void
Expand Down Expand Up @@ -225,7 +223,6 @@ open class WordPressComRestApi: NSObject {
failure(processedError ?? (error as NSError), response.response)
}
}).downloadProgress(closure: progressUpdater)
progress.sessionTask = dataRequest.task
progress.cancellationHandler = { [weak dataRequest] in
dataRequest?.cancel()
}
Expand Down Expand Up @@ -606,25 +603,6 @@ private extension WordPressComRestApi {
}
}

// MARK: - Progress

@objc extension Progress {

var sessionTask: URLSessionTask? {
get {
return userInfo[.sessionTaskKey] as? URLSessionTask
}

set {
self.setUserInfoObject(newValue, forKey: .sessionTaskKey)
}
}
}

extension ProgressUserInfoKey {
public static let sessionTaskKey = ProgressUserInfoKey(rawValue: WordPressComRestApi.SessionTaskKey)
}

// MARK: - POST encoding

private extension Dictionary {
Expand Down
1 change: 0 additions & 1 deletion WordPressKit/WordPressOrgRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ open class WordPressOrgRestApi: NSObject, WordPressRestApi {
}

}).downloadProgress(closure: progressUpdater)
progress.sessionTask = dataRequest.task
progress.cancellationHandler = {
dataRequest.cancel()
}
Expand Down
4 changes: 2 additions & 2 deletions WordPressKitTests/Utilities/URLSessionHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class URLSessionHelperTests: XCTestCase {
XCTAssertEqual(progress.completedUnitCount, 0)
XCTAssertEqual(progress.fractionCompleted, 0)

let _ = await URLSession.shared.perform(request: .init(url: URL(string: "https://wordpress.org/hello")!), fulfillingProgress: progress, errorType: TestError.self)
let _ = await URLSession.shared.perform(request: .init(url: URL(string: "https://wordpress.org/hello")!), fulfilling: progress, errorType: TestError.self)
XCTAssertEqual(progress.completedUnitCount, 20)
XCTAssertEqual(progress.fractionCompleted, 1)
}
Expand All @@ -136,7 +136,7 @@ class URLSessionHelperTests: XCTestCase {
}

// The result should be an cancellation result
let result = await URLSession.shared.perform(request: .init(url: URL(string: "https://wordpress.org/hello")!), fulfillingProgress: progress, errorType: TestError.self)
let result = await URLSession.shared.perform(request: .init(url: URL(string: "https://wordpress.org/hello")!), fulfilling: progress, errorType: TestError.self)
if case let .failure(.connection(urlError)) = result, urlError.code == .cancelled {
// Do nothing
} else {
Expand Down

0 comments on commit 5673524

Please sign in to comment.