@@ -11,6 +11,7 @@ import Result
11
11
public protocol Request {
12
12
/// The response type associated with the request type.
13
13
associatedtype Response
14
+ associatedtype Parser : DataParser
14
15
15
16
/// The base URL.
16
17
var baseURL : URL { get }
@@ -41,7 +42,7 @@ public protocol Request {
41
42
var headerFields : [ String : String ] { get }
42
43
43
44
/// The parser object that states `Content-Type` to accept and parses response body.
44
- var dataParser : DataParser { get }
45
+ var dataParser : Parser { get }
45
46
46
47
/// Intercepts `URLRequest` which is created by `Request.buildURLRequest()`. If an error is
47
48
/// thrown in this method, the result of `Session.send()` turns `.failure(.requestError(error))`.
@@ -53,15 +54,16 @@ public protocol Request {
53
54
/// The default implementation of this method is provided to throw `RequestError.unacceptableStatusCode`
54
55
/// if the HTTP status code is not in `200..<300`.
55
56
/// - Throws: `Error`
56
- func intercept( object: Any , urlResponse: HTTPURLResponse ) throws -> Any
57
+ func intercept( object: Parser . Parsed , urlResponse: HTTPURLResponse ) throws -> Parser . Parsed
57
58
58
59
/// Build `Response` instance from raw response object. This method is called after
59
60
/// `intercept(object:urlResponse:)` if it does not throw any error.
60
61
/// - Throws: `Error`
61
- func response( from object: Any , urlResponse: HTTPURLResponse ) throws -> Response
62
+ func response( from object: Parser . Parsed , urlResponse: HTTPURLResponse ) throws -> Response
62
63
}
63
64
64
65
public extension Request {
66
+
65
67
public var parameters : Any ? {
66
68
return nil
67
69
}
@@ -86,15 +88,11 @@ public extension Request {
86
88
return [ : ]
87
89
}
88
90
89
- public var dataParser : DataParser {
90
- return JSONDataParser ( readingOptions: [ ] )
91
- }
92
-
93
91
public func intercept( urlRequest: URLRequest ) throws -> URLRequest {
94
92
return urlRequest
95
93
}
96
94
97
- public func intercept( object: Any , urlResponse: HTTPURLResponse ) throws -> Any {
95
+ public func intercept( object: Parser . Parsed , urlResponse: HTTPURLResponse ) throws -> Parser . Parsed {
98
96
guard 200 ..< 300 ~= urlResponse. statusCode else {
99
97
throw ResponseError . unacceptableStatusCode ( urlResponse. statusCode)
100
98
}
@@ -146,3 +144,13 @@ public extension Request {
146
144
return try response ( from: passedObject, urlResponse: urlResponse)
147
145
}
148
146
}
147
+
148
+ public protocol JSONRequest : Request { }
149
+
150
+ public extension JSONRequest {
151
+
152
+ public var dataParser : JSONDataParser {
153
+ return JSONDataParser ( readingOptions: [ ] )
154
+ }
155
+
156
+ }
0 commit comments