Skip to content

Commit 1f331d4

Browse files
committed
Added error/success callbacks
1 parent 7a316d6 commit 1f331d4

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

NetworkMapper.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
Pod::Spec.new do |s|
22
s.name = "NetworkMapper"
3-
s.version = "0.1.1"
3+
s.version = "0.1.2"
44
s.summary = "A framework to map JSON responses to swift objects"
55
s.homepage = "https://github.com/adamdebono/NetworkMapper"
66
s.license = { :type => "MIT", :file => "LICENSE" }
77
s.author = { "Adam Debono" => "[email protected]" }
8-
8+
99
s.ios.deployment_target = "9.0"
1010
s.osx.deployment_target = "10.11"
1111
s.watchos.deployment_target = "2.0"
1212
s.tvos.deployment_target = "9.0"
13-
13+
1414
s.source = { :git => "https://github.com/adamdebono/NetworkMapper.git", :tag => s.version }
1515
s.source_files = "Source/*.swift"
16-
16+
1717
s.dependency "Alamofire", "~> 4.2.0"
1818
s.dependency "Unbox", "~> 2.2.0"
1919
end

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A framework to map JSON responses to swift objects, based on
88

99
### Cocoapods
1010
```ruby
11-
pod 'NetworkMapper', '~> 0.1.1'
11+
pod 'NetworkMapper', '~> 0.1.2'
1212
```
1313

1414
## Usage
@@ -57,7 +57,7 @@ implementation of which does nothing.
5757
```swift
5858
struct ExampleObjectRequest: NetworkObjectRequest {
5959
typealias ResponseType = ExampleObjectResponse
60-
60+
6161
let method: HTTPMethod = .get
6262
let url: URL = URL(string: "https://example.org/example/users")
6363
let parameters: [String:Any]? = nil
@@ -66,7 +66,7 @@ struct ExampleObjectRequest: NetworkObjectRequest {
6666
struct ExampleObjectResponse: NetworkObjectResponse {
6767
let users: [User]
6868
let pageNumber: Int
69-
69+
7070
init(unboxer: Unboxable) throws {
7171
self.users = try unboxer.unbox(key: "users")
7272
self.pageNumber = try unboxer.unbox(keyPath: "pagination.page_number")
@@ -76,7 +76,7 @@ struct ExampleObjectResponse: NetworkObjectResponse {
7676
struct User: Unboxable {
7777
let name: String
7878
let age: Int
79-
79+
8080
init(unboxer: Unboxable) throws {
8181
self.name = try unboxer.unbox(key: "name")
8282
self.name = try unboxer.unbox(key: "age")

Source/NetworkObjectRequest.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ import Unbox
77
public protocol NetworkObjectRequest: NetworkRequest {
88
/// The type of the response object
99
associatedtype ResponseType: NetworkObjectResponse
10-
11-
/// A callback function to which is called immediately after the response is
12-
/// decoded, but before the callback
13-
///
14-
/// The default implementation of this funciton does nothing
15-
func responseDecoded(_ response: ResponseType)
1610
}
1711
/// A protocol to define network response objects
1812
public protocol NetworkObjectResponse: Unboxable {
@@ -86,7 +80,6 @@ public extension NetworkObjectRequest {
8680

8781
do {
8882
let object: ResponseType = try unbox(dictionary: value)
89-
self.responseDecoded(object)
9083
self.complete(object: object, response: response, completionHandler: completionHandler)
9184
} catch let unboxError as UnboxError {
9285
self.complete(error: unboxError, response: response, completionHandler: completionHandler)
@@ -96,6 +89,4 @@ public extension NetworkObjectRequest {
9689
}
9790
}
9891
}
99-
100-
public func responseDecoded(_ response: ResponseType) {}
10192
}

Source/NetworkRequest.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct RequestDetails: URLRequestConvertible {
2525
}
2626

2727
public func asURLRequest() throws -> URLRequest {
28-
var request = try URLRequest(url: self.url, method: self.method, headers: self.headers)
28+
let request = try URLRequest(url: self.url, method: self.method, headers: self.headers)
2929

3030
return try URLEncoding.methodDependent.encode(request, with: self.parameters)
3131
}
@@ -44,6 +44,21 @@ public protocol NetworkRequest: URLRequestConvertible {
4444
var parameters: [String: Any]? { get }
4545
/// Any headers (or nil) for the request
4646
var headers: HTTPHeaders? { get }
47+
48+
/// A callback function which is called after the request succeeds,
49+
/// immediately before the callback
50+
///
51+
/// The default implementation of this funciton does nothing.
52+
///
53+
/// - parameter response: The decoded response in the format requested
54+
func onSuccess<T>(_ response: T) -> Void
55+
/// A callback function which is called after the request fails, immediately
56+
/// before the callback
57+
///
58+
/// The default implementation of this funciton does nothing.
59+
///
60+
/// - parameter error: The error that caused the failure
61+
func onError(_ error: Error) -> Void
4762
}
4863
public extension NetworkRequest {
4964
/// Creates a `RequestDetails` object based on the attributes of the
@@ -71,6 +86,8 @@ public extension NetworkRequest {
7186
/// - parameter response: The response from the Alamofire
7287
/// - parameter completionHandler: The completion handler to run
7388
public func complete<R, T>(error: Error, response: DataResponse<R>?, completionHandler: (DataResponse<T>) -> Void) {
89+
self.onError(error)
90+
7491
let result = Result<T>.failure(error)
7592
let errorResponse = DataResponse(request: response?.request, response: response?.response, data: response?.data, result: result)
7693
completionHandler(errorResponse)
@@ -84,11 +101,16 @@ public extension NetworkRequest {
84101
/// - parameter response: The response from Alamofire
85102
/// - parameter completionHandler: The completion handler to run
86103
public func complete<R, T>(object: T, response: DataResponse<R>, completionHandler: (DataResponse<T>) -> Void) {
104+
self.onSuccess(object)
105+
87106
let result = Result<T>.success(object)
88107
let successResponse = DataResponse(request: response.request, response: response.response, data: response.data, result: result)
89108
completionHandler(successResponse)
90109
}
91-
110+
111+
public func onSuccess<T>(_ response: T) {}
112+
public func onError(_ error: Error) {}
113+
92114
// MARK: Data
93115

94116
/// Performs a network request based on the attributes of this instance, and

0 commit comments

Comments
 (0)