Skip to content

Commit

Permalink
Handling HTTP status
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Nov 19, 2016
1 parent 85af055 commit 6294cb3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion AlamofireJsonToObjects.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "AlamofireJsonToObjects"
s.version = "2.3.2"
s.version = "2.4.0"
s.summary = "An Alamofire extension which converts JSON response data into swift objects using EVReflection"
s.description = "An Alamofire extension which converts JSON response data into swift objects using EVReflection. "
s.homepage = "https://github.com/evermeer/AlamofireJsonToObjects"
Expand Down
13 changes: 13 additions & 0 deletions AlamofireJsonToObjects/AlamofireJsonToObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import EVReflection
import Alamofire


open class EVNetworkingObject: EVObject {
override open func initValidation(_ dict: NSDictionary) {
if dict["__response_statusCode"] != nil {
self.addStatusMessage(DeserializationStatus.Custom, message: "HTTP Status = \(dict["__response_statusCode"]!)")
}
}

override open func propertyMapping() -> [(String?, String?)] {
return [("__response_statusCode", nil)]
}
}


extension DataRequest {

enum ErrorCode: Int {
Expand Down
61 changes: 41 additions & 20 deletions AlamofireJsonToObjectsTests/AlamofireJsonToObjectsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import XCTest
import Alamofire
import EVReflection

class WeatherResponse: EVObject {
class WeatherResponse: EVNetworkingObject {
var location: String?
var three_day_forecast: [Forecast] = [Forecast]()
}

class Forecast: EVObject {
class Forecast: EVNetworkingObject {
var day: String?
var temperature: NSNumber?
var conditions: String?
Expand Down Expand Up @@ -44,7 +44,6 @@ class AlamofireJsonToObjectsTests: XCTestCase {
Alamofire.request(URL)
.responseObject { (response: DataResponse<WeatherResponse>) in

exp.fulfill()
if let result = response.result.value {
print("\(result.description)")
XCTAssertNotNil(result.location, "Location should not be nil")
Expand All @@ -59,15 +58,38 @@ class AlamofireJsonToObjectsTests: XCTestCase {
} else {
XCTAssert(true, "no result from service")
}


exp.fulfill()
}

waitForExpectations(timeout: 10) { error in
XCTAssertNil(error, "\(error)")
}
}


func testErrorResponse() {
let URL: URLConvertible = "http://raw.githubusercontent.com/evermeer/AlamofireJsonToObjects/master/AlamofireJsonToObjectsTests/non_existing_file"
let exp = expectation(description: "\(URL)")

Alamofire.request(URL)
.responseObject { (response: DataResponse<WeatherResponse>) in

if let result = response.result.value {
print("\(result.description)")
print("\(result.evReflectionStatuses)")
XCTAssertNotNil(result.evReflectionStatuses.first?.0 == DeserializationStatus.Custom, "A custom validation error should have been added")
XCTAssertNotNil(result.evReflectionStatuses.first?.1 == "HTTP Status = 404", "The custom validation error should be for a 404 HTTP status error")
} else {
XCTAssert(true, "no result from service")
}
exp.fulfill()
}

waitForExpectations(timeout: 10) { error in
XCTAssertNil(error, "\(error)")
}
}


func testResponseObject2() {
// This is an example of a functional test case.
Expand All @@ -77,19 +99,19 @@ class AlamofireJsonToObjectsTests: XCTestCase {
Alamofire.request(Router.list1())
.responseObject { (response: DataResponse<WeatherResponse>) in

exp.fulfill()
if let result = response.result.value {
XCTAssertNotNil(result.location, "Location should not be nil")
XCTAssertNotNil(result.three_day_forecast, "ThreeDayForcast should not be nil")
XCTAssertEqual(result.three_day_forecast.count, 3, "ThreeDayForcast should have 2 items.")
for forecast in result.three_day_forecast {
XCTAssertNotNil(forecast.day, "day should not be nil")
XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
}
} else {
XCTAssert(true, "Could not get result from service")
if let result = response.result.value {
XCTAssertNotNil(result.location, "Location should not be nil")
XCTAssertNotNil(result.three_day_forecast, "ThreeDayForcast should not be nil")
XCTAssertEqual(result.three_day_forecast.count, 3, "ThreeDayForcast should have 2 items.")
for forecast in result.three_day_forecast {
XCTAssertNotNil(forecast.day, "day should not be nil")
XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
}
} else {
XCTAssert(true, "Could not get result from service")
}
exp.fulfill()
}

waitForExpectations(timeout: 10) { error in
Expand All @@ -106,7 +128,6 @@ class AlamofireJsonToObjectsTests: XCTestCase {
Alamofire.request(URL, method: HTTPMethod.get, parameters: nil, encoding: URLEncoding.default, headers: nil)
.responseObject { (response: DataResponse<WeatherResponse>) in

exp.fulfill()
if let result = response.result.value {
XCTAssertNotNil(result.location, "Location should not be nil")
XCTAssertNotNil(result.three_day_forecast, "ThreeDayForcast should not be nil")
Expand All @@ -119,6 +140,7 @@ class AlamofireJsonToObjectsTests: XCTestCase {
} else {
XCTAssert(true, "Could not get result from service")
}
exp.fulfill()
}

waitForExpectations(timeout: 10) { error in
Expand All @@ -133,7 +155,6 @@ class AlamofireJsonToObjectsTests: XCTestCase {

Alamofire.request(URL)
.responseArray { (response: DataResponse<[Forecast]>) in
exp.fulfill()

if let result = response.result.value {
for forecast in result {
Expand All @@ -144,7 +165,7 @@ class AlamofireJsonToObjectsTests: XCTestCase {
} else {
XCTAssert(true, "Service did not return a result")
}

exp.fulfill()
}

waitForExpectations(timeout: 10) { error in
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ import AlamofireJsonToObjects
## Sample code

```
class WeatherResponse: EVObject {
class WeatherResponse: EVNetworkingObject {
var location: String?
var three_day_forecast: [Forecast] = [Forecast]()
}
class Forecast: EVObject {
class Forecast: EVNetworkingObject {
var day: String?
var temperature: NSNumber?
var conditions: String?
Expand Down Expand Up @@ -105,6 +105,10 @@ The code above will pass the folowing json to the objects:
## Advanced object mapping
AlamofireJsonToObjects is based on [EVReflection](https://github.com/evermeer/EVReflection) and you can use all [EVReflection](https://github.com/evermeer/EVReflection) features like property mapping, converters, validators and key kleanup. See [EVReflection](https://github.com/evermeer/EVReflection) for more information.

## Handling HTTP status >= 300
When a network call returns a [HTTP error status](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) (300 or highter) then this will be added to the evReflectionStatuses as a custom error. see the unit test testErrorResponse as a sample. In order to make this work, you do have to set EVNetworkingObject as your bass class and not EVObject. You then also have to be aware that if you override the initValidation or the propertyMapping function, that you also have to call the super for that function.


## License

AlamofireJsonToObjects is available under the MIT 3 license. See the LICENSE file for more info.
Expand Down

0 comments on commit 6294cb3

Please sign in to comment.