Skip to content

Commit

Permalink
feat: add annotation RequestCachePolicy (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime authored Jan 30, 2025
1 parent a01ef75 commit 4132309
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/Postie/Configuration/RequestCachePolicy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

@propertyWrapper
public struct RequestCachePolicy {

public var wrappedValue: URLRequest.CachePolicy

public init(wrappedValue: URLRequest.CachePolicy = .useProtocolCachePolicy) {
self.wrappedValue = wrappedValue
}
}

// MARK: - Encodable

extension RequestCachePolicy: Encodable {
public func encode(to encoder: any Encoder) throws {
// This method needs to defined because `URLRequest.CachePolicy` does not conform to `Encodable`, but should never be called anyways
preconditionFailure("\(Self.self).encode(to encoder:) should not be called")
}
}
3 changes: 3 additions & 0 deletions Sources/Postie/Encoder/RequestEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public class RequestEncoder {
.requestHeaderFields(with: encoder.cookies)
// Merge the Cookie headers with the custom headers, where custom headers have precedence
.merging(encoder.headers, uniquingKeysWith: { $1 })
if let cachePolicy = encoder.cachePolicy {
request.cachePolicy = cachePolicy
}
return request
}
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/Postie/Encoder/RequestEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class RequestEncoding: Encoder {
private(set) var headers: [String: String] = [:]
private(set) var pathParameters: [String: RequestPathParameterValue] = [:]
private(set) var cookies: [HTTPCookie] = []
private(set) var cachePolicy: URLRequest.CachePolicy?

init(parent: RequestEncoding? = nil, codingPath: [CodingKey] = []) {
self.parent = parent
Expand Down Expand Up @@ -88,6 +89,14 @@ internal class RequestEncoding: Encoder {
}
}

func setCachePolicy(_ cachePolicy: URLRequest.CachePolicy) {
if let parent = parent {
parent.setCachePolicy(cachePolicy)
} else {
self.cachePolicy = cachePolicy
}
}

// MARK: - Accessors

func resolvedPath() throws -> String {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Postie/Encoder/RequestKeyedEncodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class RequestKeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where K
encoder.setCustomURL(url: customURL)
case let cookies as RequestCookies:
encoder.setCookies(cookies.wrappedValue)
case let cachePolicy as RequestCachePolicy:
encoder.setCachePolicy(cachePolicy.wrappedValue)
default:
// ignore any other values
break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// swiftlint:disable nesting
@testable import Postie
import XCTest

class RequestCachePolicyCodingTests: XCTestCase {
let baseURL = URL(string: "https://local.url")!

func testEncoding_cachePolicyIsNotDefined_shouldUseDefault() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .useProtocolCachePolicy)
}

func testEncoding_cachePolicyIsDefined_shouldUseDefault() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse

@RequestCachePolicy var cachePolicy
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .useProtocolCachePolicy)
}

func testEncoding_cachePolicyIsOverwritten_shouldBeSetInRequest() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse

@RequestCachePolicy var cachePolicy = .returnCacheDataDontLoad
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .returnCacheDataDontLoad)
}
}

// swiftlint:enable nesting

0 comments on commit 4132309

Please sign in to comment.