Skip to content

Commit 3c7b335

Browse files
committed
accept NSData instead of AnyObject in ResponseParseable protocol
1 parent 4fb8066 commit 3c7b335

12 files changed

+32
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TRON 1.0 is a major release with a lot of new features and breaking changes. To
1515
* RxSwift extension on `MultipartAPIRequest` reworked to return single Observable<ModelType>
1616
* `EventDispatcher` class and corresponding `TRON.dispatcher`, `APIRequest.dispatcher` property are replaced by `TRON` and `APIRequest` properties - `processingQueue` and `resultDeliveryQueue`, which are used to determine on which queue should processing be performed and on which queue results should be delivered.
1717
* `Progress` and `ProgressClosure` typealiases have been removed
18+
* `ResponseParseable` protocol now accepts NSData instead of `AnyObject` in its constructor
1819

1920
### Added
2021

Source/Core/APIRequest.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,4 @@ public class APIRequest<Model: ResponseParseable, ErrorModel: ResponseParseable>
166166
self.callSuccessFailureBlocks(success, failure: failure, response: $0)
167167
}
168168
}
169-
}
170-
171-
extension NSData {
172-
func parseToAnyObject() throws -> AnyObject {
173-
return try NSJSONSerialization.JSONObjectWithData(self, options: .AllowFragments)
174-
}
175169
}

Source/Core/APIStub.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ public extension APIStub {
4545
public func buildModelFromFile(fileName: String, inBundle bundle: NSBundle = NSBundle.mainBundle()) {
4646
if let filePath = bundle.pathForResource(fileName as String, ofType: nil)
4747
{
48-
guard let data = NSData(contentsOfFile: filePath),
49-
let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) else {
48+
guard let data = NSData(contentsOfFile: filePath) else {
5049
print("failed building response model from file: \(filePath)")
5150
return
5251
}
53-
model = try? Model.from(json)
52+
model = try? Model.from(data: data)
5453
}
5554
}
5655
}

Source/Core/BaseRequest.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,9 @@ public class BaseRequest<Model: ResponseParseable, ErrorModel: ResponseParseable
136136
guard error == nil else {
137137
return .Failure(self.errorBuilder.buildErrorFromRequest(urlRequest, response: response, data: data, error: error))
138138
}
139-
if Model.self is EmptyResponse.Type {
140-
return .Success(try! Model.from(NSDictionary()))
141-
}
142-
let object : AnyObject
143-
do {
144-
object = try (data ?? NSData()).parseToAnyObject()
145-
}
146-
catch let jsonError as NSError {
147-
return .Failure(self.errorBuilder.buildErrorFromRequest(urlRequest, response: response, data: data, error: jsonError))
148-
}
149139
let model: Model.ModelType
150140
do {
151-
model = try self.responseBuilder.buildResponseFromJSON(object)
141+
model = try self.responseBuilder.buildResponseFromData(data ?? NSData())
152142
}
153143
catch let parsingError as NSError {
154144
return .Failure(self.errorBuilder.buildErrorFromRequest(urlRequest, response: response, data: data, error: parsingError))

Source/Core/EmptyResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct EmptyResponse : ResponseParseable {
3030

3131
public init() {}
3232

33-
public static func from(json: AnyObject) throws -> EmptyResponse {
33+
public static func from(data data: NSData) throws -> EmptyResponse {
3434
return EmptyResponse()
3535
}
3636
}

Source/Core/ErrorBuilder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public struct APIError<T:ResponseParseable> : ErrorType {
8686
self.response = response
8787
self.data = data
8888
self.error = error
89-
guard let object = try? data?.parseToAnyObject() else {
90-
return
91-
}
92-
self.errorModel = object.flatMap { try? T.from($0) }
89+
// guard let object = try? data?.parseToAnyObject() else {
90+
// return
91+
// }
92+
self.errorModel = try? T.from(data: data ?? NSData())
9393
}
9494

9595
/**

Source/Core/ResponseBuilder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public protocol ResponseParseable {
3838
- returns: parsed model
3939
- note: Ideally, we would like to return Self here, however Swift 2 understands Self as final class or struct and therefore prohibits subclassing. Which is why we are using workaround with ModelType.
4040
*/
41-
static func from(json: AnyObject) throws -> ModelType
41+
static func from(data data: NSData) throws -> ModelType
4242
}
4343

4444
/**
@@ -56,8 +56,8 @@ public class ResponseBuilder<T:ResponseParseable>
5656
//
5757
// - returns parsed model.
5858
// */
59-
public func buildResponseFromJSON(json : AnyObject) throws -> T.ModelType {
60-
return try T.from(json)
59+
public func buildResponseFromData(data : NSData) throws -> T.ModelType {
60+
return try T.from(data: data)
6161
}
6262
}
6363

Source/SwiftyJSON/SwiftyJSONDecodable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public protocol JSONDecodable : ResponseParseable {
3636
}
3737

3838
public extension ResponseParseable where Self.ModelType : JSONDecodable, Self == Self.ModelType {
39-
public static func from(json: AnyObject) throws -> ModelType {
40-
return self.init(json: JSON(json))
39+
public static func from(data data: NSData) throws -> ModelType {
40+
return self.init(json: JSON(data: data))
4141
}
4242
}
4343

TRON.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'TRON'
3-
s.version = '1.0.0-beta.1'
3+
s.version = '1.0.0-beta.2'
44
s.license = 'MIT'
55
s.summary = 'Lightweight network abstraction layer, written on top of Alamofire'
66
s.homepage = 'https://github.com/MLSDev/TRON'

Tests/ApiStubbingTestCase.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ class ApiStubbingTestCase: XCTestCase {
5858
expect(request.apiStub.model?.id) == 1
5959
}
6060

61-
func testInvalidJSONIsNotAllowed() {
62-
let request :APIRequest<TestUser,TronError> = tron.request(path: "f00")
63-
request.stubbingEnabled = true
64-
request.apiStub.buildModelFromFile("invalid.json", inBundle: NSBundle(forClass: self.dynamicType))
65-
66-
expect(request.apiStub.model).to(beNil())
67-
}
68-
6961
func testMultipartStubbingSuccessWorks() {
7062
let request: MultipartAPIRequest<Int,TronError> = tron.uploadMultipart(path: "f00") { formData in
7163
}

0 commit comments

Comments
 (0)