HttpResponse.swift 3.6 KB

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