Skip to content

Commit

Permalink
Merge pull request #38 from allaboutapps/add-time-out-for-resources
Browse files Browse the repository at this point in the history
add timeout for resource & individual requests
  • Loading branch information
draskovits authored Mar 18, 2024
2 parents 5797aef + 03cdb81 commit 3d3adac
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/Fetch/Network/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ open class Resource<T: Decodable>: CacheableResource {
public let stubKey: ResourceStubKey
public let decode: DecodingClosure
public let encode: EncodingClosure

/// If not set the timeout of the APIClient.Config is used
public let timeout: TimeInterval?

/// The final `URL` used in a network request, using the base URL from the resource or from the `APIClient` and the path
/// If path is an absolute URL, this URL is used without a base url
Expand Down Expand Up @@ -101,7 +104,9 @@ open class Resource<T: Decodable>: CacheableResource {
customValidation: DataRequest.Validation? = nil,
stubKey: ResourceStubKey? = nil,
decode: DecodingClosure? = nil,
encode: EncodingClosure? = nil) {
encode: EncodingClosure? = nil,
timeout: TimeInterval? = nil
) {
self.apiClient = apiClient
self.headers = headers
self.method = method
Expand All @@ -117,6 +122,7 @@ open class Resource<T: Decodable>: CacheableResource {
self.cacheExpiration = cacheExpiration
self.multipartFormData = multipartFormData
self.customValidation = customValidation
self.timeout = timeout

if let decode = decode {
self.decode = decode
Expand Down Expand Up @@ -154,6 +160,10 @@ open class Resource<T: Decodable>: CacheableResource {
urlRequest.httpMethod = method.rawValue
urlRequest.allHTTPHeaderFields = headers

if let timeout {
urlRequest.timeoutInterval = timeout
}

// Body
if let body = body {
let (data, contentType) = try encode(body: body)
Expand Down

0 comments on commit 3d3adac

Please sign in to comment.