@@ -4,12 +4,17 @@ import APIKit
4
4
let DEFAULT_API_BASE_URL = " https://api-sandbox.pokepay.jp "
5
5
6
6
public protocol BankRequest : Request {
7
+ var responseContentType : String { get }
7
8
}
8
9
9
10
public extension BankRequest {
10
11
var baseURL : URL {
11
12
return URL ( string: DEFAULT_API_BASE_URL) !
12
13
}
14
+
15
+ var responseContentType : String {
16
+ return " application/json "
17
+ }
13
18
}
14
19
15
20
public extension BankRequest {
@@ -23,17 +28,33 @@ public extension BankRequest {
23
28
24
29
extension BankRequest where Response: Decodable {
25
30
public var dataParser : DataParser {
26
- return DecodableDataParser ( )
31
+ switch responseContentType {
32
+ case " text/html " :
33
+ return HTMLDataParser ( )
34
+ default :
35
+ return DecodableDataParser ( )
36
+ }
27
37
}
28
38
29
39
public func response( from object: Any , urlResponse: HTTPURLResponse ) throws -> Response {
30
40
guard let data = object as? Data else {
31
41
throw BankAPIError ( statusCode: urlResponse. statusCode, object: Data ( ) )
32
42
}
43
+
33
44
guard data. count != 0 else {
34
45
let emptyJson = " {} "
35
46
return try BankAPIJSONDecoder ( ) . decode ( Response . self, from: emptyJson. data ( using: . utf8) !)
36
47
}
37
- return try BankAPIJSONDecoder ( ) . decode ( Response . self, from: data)
48
+
49
+ switch responseContentType {
50
+ case " text/html " :
51
+ if let htmlString = String ( data: data, encoding: . utf8) as? Response {
52
+ return htmlString
53
+ } else {
54
+ throw BankAPIError ( statusCode: urlResponse. statusCode, object: data)
55
+ }
56
+ default :
57
+ return try BankAPIJSONDecoder ( ) . decode ( Response . self, from: data)
58
+ }
38
59
}
39
60
}
0 commit comments