Skip to content

Commit 0f597bc

Browse files
author
Adam Debono
committed
added documentation
1 parent a3dd0ac commit 0f597bc

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

NetworkMapper.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 = "NetworkMapper"
3-
s.version = "0.0.3"
3+
s.version = "0.0.4"
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" }

Source/Error.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
/// The error type returned by NetworkMapper
3+
///
4+
/// - invalidResponse: The response JSON was not in a format that could be
5+
/// decoded
26
public enum ResponseError: Error {
37
case invalidResponse
48
}

Source/NetworkObjectRequest.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,45 @@ import Alamofire
33
import Foundation
44
import Unbox
55

6+
/// A protocol to define network requests that map directly to response objects
67
public protocol NetworkObjectRequest: NetworkRequest {
8+
/// The type of the response object
79
associatedtype ResponseType: NetworkObjectResponse
810

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
915
func responseDecoded(_ response: ResponseType)
1016
}
17+
/// A protocol to define network response objects
1118
public protocol NetworkObjectResponse: Unboxable {
1219

1320
}
1421

1522
public extension NetworkObjectRequest {
23+
/// Performs a network request based on the attributes of this instance, and
24+
/// retrieves the response object
25+
///
26+
/// - parameter completionHandler: A callback which is run on the
27+
/// completion of the request
28+
///
29+
/// - returns: The request that was sent
1630
@discardableResult
1731
public func responseObject(completionHandler: @escaping ((DataResponse<ResponseType>) -> Void)) -> DataRequest {
1832
return self.responseJSON { response in
1933
self.processObjectResponse(response: response, completionHandler: completionHandler)
2034
}
2135
}
36+
/// Processes an object response from the server
37+
///
38+
/// This can be overridden to provide custom processing. The default
39+
/// implementation will attempt to unbox the object response from the root
40+
/// json object of the response.
41+
///
42+
/// - parameter response: The response to process
43+
/// - parameter completionHandler: A callback which is run once processing
44+
/// is complete
2245
public func processObjectResponse(response: DataResponse<Any>, completionHandler: @escaping ((DataResponse<ResponseType>) -> Void)) {
2346
switch response.result {
2447
case .failure(let error):

Source/NetworkRequest.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
import Alamofire
33
import Foundation
44

5+
/// Contains basic information which is used to create a `URLRequest`
56
public struct RequestDetails: URLRequestConvertible {
7+
8+
/// The HTTP method of the request
69
public let method: HTTPMethod
10+
/// The url of the request
711
public let url: URL
12+
/// Any parameters (or nil) for the request
13+
///
14+
/// Parameters are encoded and added to the request based on the method of
15+
/// the request
816
public let parameters: [String: Any]?
917

1018
public init(method: HTTPMethod, url: URL, parameters: [String: Any]? = nil) {
@@ -21,12 +29,21 @@ public struct RequestDetails: URLRequestConvertible {
2129
}
2230
}
2331

32+
/// A basic protocol to define network requests
2433
public protocol NetworkRequest: URLRequestConvertible {
34+
/// The HTTP method of the request
2535
var method: HTTPMethod { get }
36+
/// The url of the request
2637
var url: URL { get }
38+
/// Any parameters (or nil) for the request
39+
///
40+
/// Parameters are encoded and added to the request based on the method of
41+
/// the request
2742
var parameters: [String: Any]? { get }
2843
}
2944
public extension NetworkRequest {
45+
/// Creates a `RequestDetails` object based on the attributes of the
46+
/// instance
3047
public func getRequestDetails() -> RequestDetails {
3148
return RequestDetails(
3249
method: self.method,
@@ -39,11 +56,28 @@ public extension NetworkRequest {
3956
return try self.getRequestDetails().asURLRequest()
4057
}
4158

59+
/// Performs the completionHandler when an error occurs during the response
60+
/// handler
61+
///
62+
/// This is a convenience function to map the response type to the expected
63+
/// completion type
64+
///
65+
/// - parameter error: The error that occurred
66+
/// - parameter response: The response from the Alamofire
67+
/// - parameter completionHandler: The completion handler to run
4268
public func complete<R, T>(error: Error, response: DataResponse<R>?, completionHandler: (DataResponse<T>) -> Void) {
4369
let result = Result<T>.failure(error)
4470
let errorResponse = DataResponse(request: response?.request, response: response?.response, data: response?.data, result: result)
4571
completionHandler(errorResponse)
4672
}
73+
/// Performs the completionHandler after a response is successfuly handled
74+
///
75+
/// This is a convenience function to map the response type to the expected
76+
/// completion type
77+
///
78+
/// - parameter object: The decoded response object
79+
/// - parameter response: The response from Alamofire
80+
/// - parameter completionHandler: The completion handler to run
4781
public func complete<R, T>(object: T, response: DataResponse<R>, completionHandler: (DataResponse<T>) -> Void) {
4882
let result = Result<T>.success(object)
4983
let successResponse = DataResponse(request: response.request, response: response.response, data: response.data, result: result)
@@ -52,6 +86,13 @@ public extension NetworkRequest {
5286

5387
// MARK: Data
5488

89+
/// Performs a network request based on the attributes of this instance, and
90+
/// retrieves the response data
91+
///
92+
/// - parameter completionHandler: A callback which is run on completion of
93+
/// the request
94+
///
95+
/// - returns: The request that was sent
5596
@discardableResult
5697
public func responseData(completionHandler: @escaping ((DataResponse<Data>) -> Void)) -> DataRequest {
5798
return Alamofire.request(self).responseData(completionHandler: { response in
@@ -72,13 +113,28 @@ public extension NetworkRequest {
72113

73114
// MARK: JSON
74115

116+
/// Performs a network request based on the attributes of this instance, and
117+
/// retrieves the response json
118+
///
119+
/// - parameter completionHandler: A callback which is run on completion of
120+
/// the request
121+
///
122+
/// - returns: The request that was sent
75123
@discardableResult
76124
public func responseJSON(completionHandler: @escaping ((DataResponse<Any>) -> Void)) -> DataRequest {
77125
return Alamofire.request(self).responseJSON { response in
78126
self.processJSONResponse(response: response, completionHandler: completionHandler)
79127
}
80128
}
81129

130+
/// Processes a JSON response from the server
131+
///
132+
/// This can be overridden to provide custom processing. The default
133+
/// implementation only checks for request cancellation.
134+
///
135+
/// - parameter response: The response to process
136+
/// - parameter completionHandler: A callback which is run once processing
137+
/// is complete
82138
public func processJSONResponse(response: DataResponse<Any>, completionHandler: @escaping ((DataResponse<Any>) -> Void)) {
83139
switch response.result {
84140
case .failure(let error):

0 commit comments

Comments
 (0)