ResponseSerialization.swift 13.0 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

/**
    The type in which all response serializers must conform to in order to serialize a response.
*/
30 31
public protocol ResponseSerializerType {
    /// The type of serialized object to be created by this `ResponseSerializerType`.
32
    typealias SerializedObject
33 34 35

    /// The type of error to be created by this `ResponseSerializer` if serialization fails.
    typealias ErrorObject: ErrorType
36

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

43 44
// MARK: -

45
/**
46
    A generic `ResponseSerializerType` used to serialize a request, response, and data into a serialized object.
47
*/
48
public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerType {
49
    /// The type of serialized object to be created by this `ResponseSerializer`.
50
    public typealias SerializedObject = Value
51

52
    /// The type of error to be created by this `ResponseSerializer` if serialization fails.
53
    public typealias ErrorObject = Error
54

55
    /**
56
        A closure used by response handlers that takes a request, response, data and error and returns a result.
57
    */
58
    public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>
59 60

    /**
61
        Initializes the `ResponseSerializer` instance with the given serialize response closure.
62

63
        - parameter serializeResponse: The closure used to serialize the response.
64

65
        - returns: The new generic response serializer instance.
66
    */
67
    public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
68 69
        self.serializeResponse = serializeResponse
    }
70 71 72
}

// MARK: - Default
73 74

extension Request {
75

76 77 78 79 80 81 82 83 84
    /**
        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(
85
        queue queue: dispatch_queue_t? = nil,
86
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Void)
87 88 89 90 91 92 93 94 95 96 97
        -> Self
    {
        delegate.queue.addOperationWithBlock {
            dispatch_async(queue ?? dispatch_get_main_queue()) {
                completionHandler(self.request, self.response, self.delegate.data, self.delegate.error)
            }
        }

        return self
    }

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

101 102 103 104
        - 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.
105

106
        - returns: The request.
107
    */
108
    public func response<T: ResponseSerializerType>(
109
        queue queue: dispatch_queue_t? = nil,
110
        responseSerializer: T,
111
        completionHandler: Response<T.SerializedObject, T.ErrorObject> -> Void)
112 113
        -> Self
    {
114
        delegate.queue.addOperationWithBlock {
115 116 117 118 119 120
            let result = responseSerializer.serializeResponse(
                self.request,
                self.response,
                self.delegate.data,
                self.delegate.error
            )
121 122

            dispatch_async(queue ?? dispatch_get_main_queue()) {
123 124 125 126 127 128 129 130
                let response = Response<T.SerializedObject, T.ErrorObject>(
                    request: self.request,
                    response: self.response,
                    data: self.delegate.data,
                    result: result
                )

                completionHandler(response)
131 132 133 134 135 136 137 138 139 140 141 142 143 144
            }
        }

        return self
    }
}

// MARK: - Data

extension Request {

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

145
        - returns: A data response serializer.
146
    */
147
    public static func dataResponseSerializer() -> ResponseSerializer<NSData, NSError> {
148
        return ResponseSerializer { _, _, data, error in
149 150
            guard error == nil else { return .Failure(error!) }

151 152
            guard let validData = data where validData.length > 0 else {
                let failureReason = "Data could not be serialized. Input data was nil or zero length."
153
                let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
154
                return .Failure(error)
155 156 157
            }

            return .Success(validData)
158 159 160 161 162 163
        }
    }

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

164
        - parameter completionHandler: The code to be executed once the request has finished.
165

166
        - returns: The request.
167
    */
168
    public func responseData(completionHandler: Response<NSData, NSError> -> Void) -> Self {
169
        return response(responseSerializer: Request.dataResponseSerializer(), completionHandler: completionHandler)
170 171 172 173
    }
}

// MARK: - String
174 175

extension Request {
176

177
    /**
178 179
        Creates a response serializer that returns a string initialized from the response data with the specified 
        string encoding.
180

181 182
        - 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.
183

184
        - returns: A string response serializer.
185
    */
186 187
    public static func stringResponseSerializer(
        var encoding encoding: NSStringEncoding? = nil)
188
        -> ResponseSerializer<String, NSError>
189
    {
190
        return ResponseSerializer { _, response, data, error in
191 192
            guard error == nil else { return .Failure(error!) }

193 194
            guard let validData = data where validData.length > 0 else {
                let failureReason = "String could not be serialized. Input data was nil or zero length."
195
                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
196
                return .Failure(error)
197
            }
198

199
            if let encodingName = response?.textEncodingName where encoding == nil {
200 201 202
                encoding = CFStringConvertEncodingToNSStringEncoding(
                    CFStringConvertIANACharSetNameToEncoding(encodingName)
                )
203
            }
204

205
            let actualEncoding = encoding ?? NSISOLatin1StringEncoding
206

207
            if let string = String(data: validData, encoding: actualEncoding) {
208 209 210 211
                return .Success(string)
            } else {
                let failureReason = "String could not be serialized with encoding: \(actualEncoding)"
                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
212
                return .Failure(error)
213
            }
214 215
        }
    }
216

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

220 221 222
        - 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.
223
        - parameter completionHandler: A closure to be executed once the request has finished.
224

225
        - returns: The request.
226
    */
227
    public func responseString(
228
        encoding encoding: NSStringEncoding? = nil,
229
        completionHandler: Response<String, NSError> -> Void)
230 231 232 233
        -> Self
    {
        return response(
            responseSerializer: Request.stringResponseSerializer(encoding: encoding),
234
            completionHandler: completionHandler
235
        )
236 237 238
    }
}

239
// MARK: - JSON
240 241

extension Request {
242

243
    /**
244 245
        Creates a response serializer that returns a JSON object constructed from the response data using 
        `NSJSONSerialization` with the specified reading options.
246

247
        - parameter options: The JSON serialization reading options. `.AllowFragments` by default.
248

249
        - returns: A JSON object response serializer.
250
    */
251 252
    public static func JSONResponseSerializer(
        options options: NSJSONReadingOptions = .AllowFragments)
253
        -> ResponseSerializer<AnyObject, NSError>
254
    {
255
        return ResponseSerializer { _, _, data, error in
256 257
            guard error == nil else { return .Failure(error!) }

258 259
            guard let validData = data where validData.length > 0 else {
                let failureReason = "JSON could not be serialized. Input data was nil or zero length."
260
                let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
261
                return .Failure(error)
262
            }
263

264
            do {
265 266
                let JSON = try NSJSONSerialization.JSONObjectWithData(validData, options: options)
                return .Success(JSON)
267
            } catch {
268
                return .Failure(error as NSError)
269
            }
270 271
        }
    }
272

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

276
        - parameter options:           The JSON serialization reading options. `.AllowFragments` by default.
277
        - parameter completionHandler: A closure to be executed once the request has finished.
278

279
        - returns: The request.
280
    */
281
    public func responseJSON(
282
        options options: NSJSONReadingOptions = .AllowFragments,
283
        completionHandler: Response<AnyObject, NSError> -> Void)
284 285 286 287
        -> Self
    {
        return response(
            responseSerializer: Request.JSONResponseSerializer(options: options),
288
            completionHandler: completionHandler
289
        )
290 291 292
    }
}

293
// MARK: - Property List
294 295

extension Request {
296

297
    /**
298 299
        Creates a response serializer that returns an object constructed from the response data using 
        `NSPropertyListSerialization` with the specified reading options.
300

301
        - parameter options: The property list reading options. `NSPropertyListReadOptions()` by default.
302

303
        - returns: A property list object response serializer.
304
    */
305 306
    public static func propertyListResponseSerializer(
        options options: NSPropertyListReadOptions = NSPropertyListReadOptions())
307
        -> ResponseSerializer<AnyObject, NSError>
308
    {
309
        return ResponseSerializer { _, _, data, error in
310 311
            guard error == nil else { return .Failure(error!) }

312 313
            guard let validData = data where validData.length > 0 else {
                let failureReason = "Property list could not be serialized. Input data was nil or zero length."
314
                let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: failureReason)
315
                return .Failure(error)
316
            }
317

318
            do {
319 320
                let plist = try NSPropertyListSerialization.propertyListWithData(validData, options: options, format: nil)
                return .Success(plist)
321
            } catch {
322
                return .Failure(error as NSError)
323
            }
324 325
        }
    }
326

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

330
        - parameter options:           The property list reading options. `0` by default.
331
        - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 3
332 333
                                       arguments: the URL request, the URL response, the server data and the result 
                                       produced while creating the property list.
334

335
        - returns: The request.
336
    */
337
    public func responsePropertyList(
338
        options options: NSPropertyListReadOptions = NSPropertyListReadOptions(),
339
        completionHandler: Response<AnyObject, NSError> -> Void)
340 341 342 343
        -> Self
    {
        return response(
            responseSerializer: Request.propertyListResponseSerializer(options: options),
344
            completionHandler: completionHandler
345
        )
346 347
    }
}