HttpResponse.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // HttpResponse.swift
  3. // Swifter
  4. //
  5. // Created by Damian Kolakowski on 18/06/14.
  6. // Copyright (c) 2014 Damian Kołakowski. All rights reserved.
  7. //
  8. import Foundation
  9. enum HttpResponseBody {
  10. case JSON(AnyObject)
  11. case XML(AnyObject)
  12. case PLIST(AnyObject)
  13. case HTML(String)
  14. case RAW(String)
  15. func data() -> String? {
  16. switch self {
  17. case .JSON(let object):
  18. if NSJSONSerialization.isValidJSONObject(object) {
  19. var serializationError: NSError?
  20. if let json = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions.PrettyPrinted, error: &serializationError) {
  21. return NSString(data: json, encoding: NSUTF8StringEncoding)
  22. }
  23. return "Serialisation error: \(serializationError)"
  24. }
  25. return "Invalid object to serialise."
  26. case .XML(let data):
  27. return "XML serialization not supported."
  28. case .PLIST(let object):
  29. let format = NSPropertyListFormat.XMLFormat_v1_0
  30. if NSPropertyListSerialization.propertyList(object, isValidForFormat: format) {
  31. var serializationError: NSError?
  32. if let plist = NSPropertyListSerialization.dataWithPropertyList(object, format: format, options: 0, error: &serializationError) {
  33. return NSString(data: plist, encoding: NSUTF8StringEncoding)
  34. }
  35. return "Serialisation error: \(serializationError)"
  36. }
  37. return "Invalid object to serialise."
  38. case .RAW(let body):
  39. return body
  40. case .HTML(let body):
  41. return "<html><body>\(body)</body></html>"
  42. }
  43. }
  44. }
  45. enum HttpResponse {
  46. case OK(HttpResponseBody), Created, Accepted
  47. case MovedPermanently(String)
  48. case BadRequest, Unauthorized, Forbidden, NotFound
  49. case InternalServerError
  50. case RAW(Int, NSData)
  51. func statusCode() -> Int {
  52. switch self {
  53. case .OK(_) : return 200
  54. case .Created : return 201
  55. case .Accepted : return 202
  56. case .MovedPermanently : return 301
  57. case .BadRequest : return 400
  58. case .Unauthorized : return 401
  59. case .Forbidden : return 403
  60. case .NotFound : return 404
  61. case .InternalServerError : return 500
  62. case .RAW(let code, _) : return code
  63. }
  64. }
  65. func reasonPhrase() -> String {
  66. switch self {
  67. case .OK(_) : return "OK"
  68. case .Created : return "Created"
  69. case .Accepted : return "Accepted"
  70. case .MovedPermanently : return "Moved Permanently"
  71. case .BadRequest : return "Bad Request"
  72. case .Unauthorized : return "Unauthorized"
  73. case .Forbidden : return "Forbidden"
  74. case .NotFound : return "Not Found"
  75. case .InternalServerError : return "Internal Server Error"
  76. case .RAW(_,_) : return "Custom"
  77. }
  78. }
  79. func headers() -> Dictionary<String, String> {
  80. switch self {
  81. case .MovedPermanently(let location) : return [ "Location" : location ]
  82. default: return Dictionary()
  83. }
  84. }
  85. func body() -> NSData? {
  86. switch self {
  87. case .OK(let body) : return body.data()?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
  88. case .RAW(_, let data) : return data
  89. default : return nil
  90. }
  91. }
  92. }