Skip to content

Commit 948ee38

Browse files
committed
Return a Response<T> object instead of <T>
1 parent a4e64d8 commit 948ee38

5 files changed

Lines changed: 35 additions & 9 deletions

File tree

Sources/NetworkLayer/Classes/Core/Services/RequestProcessor/RequestProcessor.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ extension RequestProcessor: IRequestProcessor {
156156
strategy: RetryPolicyStrategy? = nil,
157157
delegate: URLSessionDelegate? = nil,
158158
configure: ((inout URLRequest) throws -> Void)? = nil
159-
) async throws -> M {
159+
) async throws -> Response<M> {
160160
let response = try await performRequest(request, strategy: strategy, delegate: delegate, configure: configure)
161-
let item = try configuration.jsonDecoder.decode(M.self, from: response.data)
162-
return item
161+
return try response.map { data in try self.configuration.jsonDecoder.decode(M.self, from: data) }
163162
}
164163
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// network-layer
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
import NetworkLayerInterfaces
8+
9+
extension Response {
10+
func map<U>(_ closure: @escaping (T) throws -> U) rethrows -> Response<U> {
11+
try Response<U>(data: closure(data), response: response, task: task)
12+
}
13+
}

Sources/NetworkLayerInterfaces/Classes/Core/Models/Response.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55

66
import Foundation
77

8+
/// A generic struct representing an HTTP response.
89
public struct Response<T> {
10+
/// The data associated with the response.
911
public let data: T
12+
13+
/// The URL response received.
1014
public let response: URLResponse
15+
16+
/// The URLSessionTask associated with the response.
1117
public let task: URLSessionTask
18+
19+
/// The HTTP status code of the response, if available.
1220
public let statusCode: Int?
1321

22+
/// Initializes a new instance of `Response`.
23+
///
24+
/// - Parameters:
25+
/// - data: The data associated with the response.
26+
/// - response: The URL response received.
27+
/// - task: The URLSessionTask associated with the response.
1428
public init(data: T, response: URLResponse, task: URLSessionTask) {
1529
self.data = data
1630
self.response = response

Sources/NetworkLayerInterfaces/Classes/Core/Services/IRequestProcessor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol IRequestProcessor {
2323
strategy: RetryPolicyStrategy?,
2424
delegate: URLSessionDelegate?,
2525
configure: ((inout URLRequest) throws -> Void)?
26-
) async throws -> M
26+
) async throws -> Response<M>
2727
}
2828

2929
extension IRequestProcessor {
@@ -34,7 +34,7 @@ extension IRequestProcessor {
3434
func send<T: IRequest, M: Decodable>(
3535
_ request: T,
3636
strategy: RetryPolicyStrategy?
37-
) async throws -> M {
37+
) async throws -> Response<M> {
3838
try await send(request, strategy: strategy, delegate: nil, configure: nil)
3939
}
4040
}

Tests/NetworkLayerTests/UnitTests/RequestProcessorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ final class RequestProcessorTests: XCTestCase {
7070

7171
// when
7272
do {
73-
let _ = try await sut.send(request) as Int
73+
let _ = try await sut.send(request) as Response<Int>
7474
} catch {}
7575

7676
// then
@@ -88,7 +88,7 @@ final class RequestProcessorTests: XCTestCase {
8888

8989
// when
9090
do {
91-
let _ = try await sut.send(request) as Int
91+
let _ = try await sut.send(request) as Response<Int>
9292
} catch {}
9393

9494
// then
@@ -107,7 +107,7 @@ final class RequestProcessorTests: XCTestCase {
107107

108108
// when
109109
do {
110-
let _ = try await sut.send(request) as Int
110+
let _ = try await sut.send(request) as Response<Int>
111111
} catch {}
112112

113113
// then
@@ -125,7 +125,7 @@ final class RequestProcessorTests: XCTestCase {
125125

126126
// when
127127
do {
128-
let _ = try await sut.send(request) as Int
128+
let _ = try await sut.send(request) as Response<Int>
129129
} catch {}
130130

131131
// then

0 commit comments

Comments
 (0)