ResponseSerialization.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. :param: 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: 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. :param: 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. :param: 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: 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. :param: options The JSON serialization reading options. `.AllowFragments` by default.
  61. :returns: A JSON object response serializer.
  62. */
  63. public class func JSONResponseSerializer(options: NSJSONReadingOptions = .AllowFragments) -> Serializer {
  64. return { (request, response, data) in
  65. if data == nil || data?.length == 0 {
  66. return (nil, nil)
  67. }
  68. var serializationError: NSError?
  69. let JSON: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: options, error: &serializationError)
  70. return (JSON, serializationError)
  71. }
  72. }
  73. /**
  74. Adds a handler to be called once the request has finished.
  75. :param: options The JSON serialization reading options. `.AllowFragments` by default.
  76. :param: 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.
  77. :returns: The request.
  78. */
  79. public func responseJSON(options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {
  80. return response(serializer: Request.JSONResponseSerializer(options: options), completionHandler: { (request, response, JSON, error) in
  81. completionHandler(request, response, JSON, error)
  82. })
  83. }
  84. }
  85. // MARK: Property List
  86. extension Request {
  87. /**
  88. Creates a response serializer that returns an object constructed from the response data using `NSPropertyListSerialization` with the specified reading options.
  89. :param: options The property list reading options. `0` by default.
  90. :returns: A property list object response serializer.
  91. */
  92. public class func propertyListResponseSerializer(options: NSPropertyListReadOptions = 0) -> Serializer {
  93. return { (request, response, data) in
  94. if data == nil || data?.length == 0 {
  95. return (nil, nil)
  96. }
  97. var propertyListSerializationError: NSError?
  98. let plist: AnyObject? = NSPropertyListSerialization.propertyListWithData(data!, options: options, format: nil, error: &propertyListSerializationError)
  99. return (plist, propertyListSerializationError)
  100. }
  101. }
  102. /**
  103. Adds a handler to be called once the request has finished.
  104. :param: options The property list reading options. `0` by default.
  105. :param: 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.
  106. :returns: The request.
  107. */
  108. public func responsePropertyList(options: NSPropertyListReadOptions = 0, completionHandler: (NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {
  109. return response(serializer: Request.propertyListResponseSerializer(options: options), completionHandler: { (request, response, plist, error) in
  110. completionHandler(request, response, plist, error)
  111. })
  112. }
  113. }