ResponseSerialization.swift 12.8 KB
Newer Older
1
// ResponseSerialization.swift
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
// Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

25
// MARK: ResponseSerializer
26 27 28 29 30 31 32 33

/**
    The type in which all response serializers must conform to in order to serialize a response.
*/
public protocol ResponseSerializer {
    /// The type of serialized object to be created by this `ResponseSerializer`.
    typealias SerializedObject

34
    /**
35
        A closure used by response handlers that takes a request, response, and data and returns a result.
36
    */
37
    var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<SerializedObject> { get }
38 39
}

40 41
// MARK: -

42 43 44 45 46 47 48
/**
    A generic `ResponseSerializer` used to serialize a request, response, and data into a serialized object.
*/
public struct GenericResponseSerializer<T>: ResponseSerializer {
    /// The type of serialized object to be created by this `ResponseSerializer`.
    public typealias SerializedObject = T

49
    /**
50
        A closure used by response handlers that takes a request, response, and data and returns a result.
51
    */
52
    public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<SerializedObject>
53 54 55 56

    /**
        Initializes the `GenericResponseSerializer` instance with the given serialize response closure.

57
        - parameter serializeResponse: The closure used to serialize the response.
58

59
        - returns: The new generic response serializer instance.
60
    */
61
    public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<SerializedObject>) {
62 63
        self.serializeResponse = serializeResponse
    }
64 65 66
}

// MARK: - Default
67 68

extension Request {
69

70 71 72 73 74 75 76 77 78
    /**
        Adds a handler to be called once the request has finished.

        - parameter queue:             The queue on which the completion handler is dispatched.
        - parameter completionHandler: The code to be executed once the request has finished.

        - returns: The request.
    */
    public func response(
79
        queue queue: dispatch_queue_t? = nil,
80
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, ErrorType?) -> Void)
81 82 83 84 85 86 87 88 89 90 91
        -> Self
    {
        delegate.queue.addOperationWithBlock {
            dispatch_async(queue ?? dispatch_get_main_queue()) {
                completionHandler(self.request, self.response, self.delegate.data, self.delegate.error)
            }
        }

        return self
    }

92 93 94
    /**
        Adds a handler to be called once the request has finished.

95 96 97 98
        - parameter queue:              The queue on which the completion handler is dispatched.
        - parameter responseSerializer: The response serializer responsible for serializing the request, response, 
                                        and data.
        - parameter completionHandler:  The code to be executed once the request has finished.
99

100
        - returns: The request.
101 102
    */
    public func response<T: ResponseSerializer, V where T.SerializedObject == V>(
103
        queue queue: dispatch_queue_t? = nil,
104
        responseSerializer: T,
105
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, Result<V>) -> Void)
106 107
        -> Self
    {
108
        delegate.queue.addOperationWithBlock {
109 110 111 112 113 114 115
            let result: Result<T.SerializedObject> = {
                if let error = self.delegate.error {
                    return .Failure(self.delegate.data, error)
                } else {
                    return responseSerializer.serializeResponse(self.request, self.response, self.delegate.data)
                }
            }()
116 117

            dispatch_async(queue ?? dispatch_get_main_queue()) {
118
                completionHandler(self.request, self.response, self.delegate.data, result)
119 120 121 122 123 124 125 126 127 128 129 130 131 132
            }
        }

        return self
    }
}

// MARK: - Data

extension Request {

    /**
        Creates a response serializer that returns the associated data as-is.

133
        - returns: A data response serializer.
134 135
    */
    public static func dataResponseSerializer() -> GenericResponseSerializer<NSData> {
136
        return GenericResponseSerializer { _, _, data in
137 138 139 140 141 142 143
            guard let validData = data else {
                let failureReason = "Data could not be serialized. Input data was nil."
                let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
            }

            return .Success(validData)
144 145 146 147 148 149
        }
    }

    /**
        Adds a handler to be called once the request has finished.

150
        - parameter completionHandler: The code to be executed once the request has finished.
151

152
        - returns: The request.
153
    */
154
    public func responseData(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, Result<NSData>) -> Void) -> Self {
155
        return response(responseSerializer: Request.dataResponseSerializer(), completionHandler: completionHandler)
156 157 158 159
    }
}

// MARK: - String
160 161

extension Request {
162

163
    /**
164 165
        Creates a response serializer that returns a string initialized from the response data with the specified 
        string encoding.
166

167 168
        - parameter encoding: The string encoding. If `nil`, the string encoding will be determined from the server 
                              response, falling back to the default HTTP default character set, ISO-8859-1.
169

170
        - returns: A string response serializer.
171
    */
172 173 174 175
    public static func stringResponseSerializer(
        var encoding encoding: NSStringEncoding? = nil)
        -> GenericResponseSerializer<String>
    {
176
        return GenericResponseSerializer { _, response, data in
177 178
            guard let validData = data else {
                let failureReason = "String could not be serialized because input data was nil."
179 180
                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
181
            }
182

183
            if let encodingName = response?.textEncodingName where encoding == nil {
184 185 186
                encoding = CFStringConvertEncodingToNSStringEncoding(
                    CFStringConvertIANACharSetNameToEncoding(encodingName)
                )
187
            }
188

189
            let actualEncoding = encoding ?? NSISOLatin1StringEncoding
190

191 192 193 194 195 196 197
            if let string = NSString(data: validData, encoding: actualEncoding) as? String {
                return .Success(string)
            } else {
                let failureReason = "String could not be serialized with encoding: \(actualEncoding)"
                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
            }
198 199
        }
    }
200

201 202
    /**
        Adds a handler to be called once the request has finished.
203

204 205 206
        - parameter encoding:          The string encoding. If `nil`, the string encoding will be determined from the 
                                       server response, falling back to the default HTTP default character set, 
                                       ISO-8859-1.
207 208 209
        - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 3
                                       arguments: the URL request, the URL response and the result produced while
                                       creating the string.
210

211
        - returns: The request.
212
    */
213
    public func responseString(
214
        encoding encoding: NSStringEncoding? = nil,
215
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, Result<String>) -> Void)
216 217 218 219
        -> Self
    {
        return response(
            responseSerializer: Request.stringResponseSerializer(encoding: encoding),
220
            completionHandler: completionHandler
221
        )
222 223 224
    }
}

225
// MARK: - JSON
226 227

extension Request {
228

229
    /**
230 231
        Creates a response serializer that returns a JSON object constructed from the response data using 
        `NSJSONSerialization` with the specified reading options.
232

233
        - parameter options: The JSON serialization reading options. `.AllowFragments` by default.
234

235
        - returns: A JSON object response serializer.
236
    */
237 238 239 240
    public static func JSONResponseSerializer(
        options options: NSJSONReadingOptions = .AllowFragments)
        -> GenericResponseSerializer<AnyObject>
    {
241 242 243
        return GenericResponseSerializer { _, _, data in
            guard let validData = data else {
                let failureReason = "JSON could not be serialized because input data was nil."
244 245
                let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
246
            }
247

248
            do {
249 250
                let JSON = try NSJSONSerialization.JSONObjectWithData(validData, options: options)
                return .Success(JSON)
251
            } catch {
252
                return .Failure(data, error as NSError)
253
            }
254 255
        }
    }
256

257 258
    /**
        Adds a handler to be called once the request has finished.
259

260
        - parameter options:           The JSON serialization reading options. `.AllowFragments` by default.
261 262 263
        - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 3
                                       arguments: the URL request, the URL response and the result produced while
                                       creating the JSON object.
264

265
        - returns: The request.
266
    */
267
    public func responseJSON(
268
        options options: NSJSONReadingOptions = .AllowFragments,
269
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, Result<AnyObject>) -> Void)
270 271 272 273
        -> Self
    {
        return response(
            responseSerializer: Request.JSONResponseSerializer(options: options),
274
            completionHandler: completionHandler
275
        )
276 277 278
    }
}

279
// MARK: - Property List
280 281

extension Request {
282

283
    /**
284 285
        Creates a response serializer that returns an object constructed from the response data using 
        `NSPropertyListSerialization` with the specified reading options.
286

287
        - parameter options: The property list reading options. `NSPropertyListReadOptions()` by default.
288

289
        - returns: A property list object response serializer.
290
    */
291 292 293 294
    public static func propertyListResponseSerializer(
        options options: NSPropertyListReadOptions = NSPropertyListReadOptions())
        -> GenericResponseSerializer<AnyObject>
    {
295 296 297
        return GenericResponseSerializer { _, _, data in
            guard let validData = data else {
                let failureReason = "Property list could not be serialized because input data was nil."
298 299
                let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
300
            }
301

302
            do {
303 304
                let plist = try NSPropertyListSerialization.propertyListWithData(validData, options: options, format: nil)
                return .Success(plist)
305
            } catch {
306
                return .Failure(data, error as NSError)
307
            }
308 309
        }
    }
310

311 312
    /**
        Adds a handler to be called once the request has finished.
313

314
        - parameter options:           The property list reading options. `0` by default.
315 316 317
        - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 3
                                       arguments: the URL request, the URL response and the result produced while
                                       creating the property list.
318

319
        - returns: The request.
320
    */
321
    public func responsePropertyList(
322
        options options: NSPropertyListReadOptions = NSPropertyListReadOptions(),
323
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, Result<AnyObject>) -> Void)
324 325 326 327
        -> Self
    {
        return response(
            responseSerializer: Request.propertyListResponseSerializer(options: options),
328
            completionHandler: completionHandler
329
        )
330 331
    }
}