ResponseSerialization.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Alamofire.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. // MARK: String
  24. extension Request {
  25. /**
  26. Creates a response serializer that returns a string initialized from the response data with the specified string encoding.
  27. - 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.
  28. - returns: A string response serializer.
  29. */
  30. public class func stringResponseSerializer(var encoding encoding: NSStringEncoding? = nil) -> Serializer {
  31. return { _, response, data in
  32. if data == nil || data?.length == 0 {
  33. return (nil, nil)
  34. }
  35. if encoding == nil {
  36. if let encodingName = response?.textEncodingName {
  37. encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName))
  38. }
  39. }
  40. let string = NSString(data: data!, encoding: encoding ?? NSISOLatin1StringEncoding)
  41. return (string, nil)
  42. }
  43. }
  44. /**
  45. Adds a handler to be called once the request has finished.
  46. - 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.
  47. - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the string, if one could be created from the URL response and data, and any error produced while creating the string.
  48. - returns: The request.
  49. */
  50. public func responseString(encoding encoding: NSStringEncoding? = nil, completionHandler: (NSURLRequest?, NSHTTPURLResponse?, String?, NSError?) -> Void) -> Self {
  51. return response(serializer: Request.stringResponseSerializer(encoding: encoding), completionHandler: { request, response, string, error in
  52. completionHandler(request, response, string as? String, error)
  53. })
  54. }
  55. }
  56. // MARK: - JSON
  57. extension Request {
  58. /**
  59. Creates a response serializer that returns a JSON object constructed from the response data using `NSJSONSerialization` with the specified reading options.
  60. - parameter options: The JSON serialization reading options. `.AllowFragments` by default.
  61. - returns: A JSON object response serializer.
  62. */
  63. public class func JSONResponseSerializer(options options: NSJSONReadingOptions = .AllowFragments) -> Serializer {
  64. return { request, response, data in
  65. if data == nil || data?.length == 0 {
  66. return (nil, nil)
  67. }
  68. var JSON: AnyObject?
  69. var serializationError: NSError?
  70. do {
  71. JSON = try NSJSONSerialization.JSONObjectWithData(data!, options: options)
  72. } catch {
  73. serializationError = error as NSError
  74. }
  75. return (JSON, serializationError)
  76. }
  77. }
  78. /**
  79. Adds a handler to be called once the request has finished.
  80. - parameter options: The JSON serialization reading options. `.AllowFragments` by default.
  81. - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the JSON object, if one could be created from the URL response and data, and any error produced while creating the JSON object.
  82. - returns: The request.
  83. */
  84. public func responseJSON(options options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest?, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {
  85. return response(serializer: Request.JSONResponseSerializer(options: options), completionHandler: { request, response, JSON, error in
  86. completionHandler(request, response, JSON, error)
  87. })
  88. }
  89. }
  90. // MARK: - Property List
  91. extension Request {
  92. /**
  93. Creates a response serializer that returns an object constructed from the response data using `NSPropertyListSerialization` with the specified reading options.
  94. - parameter options: The property list reading options. `0` by default.
  95. - returns: A property list object response serializer.
  96. */
  97. public class func propertyListResponseSerializer(options options: NSPropertyListReadOptions = NSPropertyListReadOptions(rawValue: 0)) -> Serializer {
  98. return { request, response, data in
  99. if data == nil || data?.length == 0 {
  100. return (nil, nil)
  101. }
  102. var plist: AnyObject?
  103. var propertyListSerializationError: NSError?
  104. do {
  105. plist = try NSPropertyListSerialization.propertyListWithData(data!, options: options, format: nil)
  106. } catch {
  107. propertyListSerializationError = error as NSError
  108. }
  109. return (plist, propertyListSerializationError)
  110. }
  111. }
  112. /**
  113. Adds a handler to be called once the request has finished.
  114. - parameter options: The property list reading options. `0` by default.
  115. - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the property list, if one could be created from the URL response and data, and any error produced while creating the property list.
  116. - returns: The request.
  117. */
  118. public func responsePropertyList(options options: NSPropertyListReadOptions = NSPropertyListReadOptions(rawValue: 0), completionHandler: (NSURLRequest?, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {
  119. return response(serializer: Request.propertyListResponseSerializer(options: options), completionHandler: { request, response, plist, error in
  120. completionHandler(request, response, plist, error)
  121. })
  122. }
  123. }