HttpResponse.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 RAW(String)
  14. func data() -> String? {
  15. switch self {
  16. case .JSON(let object):
  17. if NSJSONSerialization.isValidJSONObject(object) {
  18. var serializationError: NSError?
  19. if let json = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions.PrettyPrinted, error: &serializationError) {
  20. return NSString(data: json, encoding: NSUTF8StringEncoding)
  21. }
  22. return "Serialisation error: \(serializationError)"
  23. }
  24. return "Invalid object to serialise."
  25. case .XML(let data):
  26. return "XML serialization not supported."
  27. case .PLIST(let object):
  28. let format = NSPropertyListFormat.XMLFormat_v1_0
  29. if NSPropertyListSerialization.propertyList(object, isValidForFormat: format) {
  30. var serializationError: NSError?
  31. if let plist = NSPropertyListSerialization.dataWithPropertyList(object, format: format, options: 0, error: &serializationError) {
  32. return NSString(data: plist, encoding: NSUTF8StringEncoding)
  33. }
  34. return "Serialisation error: \(serializationError)"
  35. }
  36. return "Invalid object to serialise."
  37. case .RAW(let data):
  38. return data
  39. }
  40. }
  41. }
  42. enum HttpResponse {
  43. case OK(HttpResponseBody), Created, Accepted
  44. case MovedPermanently(String)
  45. case BadRequest, Unauthorized, Forbidden, NotFound
  46. case InternalServerError
  47. case Raw(Int,String)
  48. func statusCode() -> Int {
  49. switch self {
  50. case .OK(_) : return 200
  51. case .Created : return 201
  52. case .Accepted : return 202
  53. case .MovedPermanently : return 301
  54. case .BadRequest : return 400
  55. case .Unauthorized : return 401
  56. case .Forbidden : return 403
  57. case .NotFound : return 404
  58. case .InternalServerError : return 500
  59. case .Raw(let code, _) : return code
  60. }
  61. }
  62. func reasonPhrase() -> String {
  63. switch self {
  64. case .OK(_) : return "OK"
  65. case .Created : return "Created"
  66. case .Accepted : return "Accepted"
  67. case .MovedPermanently : return "Moved Permanently"
  68. case .BadRequest : return "Bad Request"
  69. case .Unauthorized : return "Unauthorized"
  70. case .Forbidden : return "Forbidden"
  71. case .NotFound : return "Not Found"
  72. case .InternalServerError : return "Internal Server Error"
  73. case .Raw(_,_) : return "Custom"
  74. }
  75. }
  76. func headers() -> Dictionary<String, String> {
  77. switch self {
  78. case .MovedPermanently(let location) : return [ "Location" : location ]
  79. default: return Dictionary()
  80. }
  81. }
  82. func body() -> String? {
  83. switch self {
  84. case .OK(let body) : return body.data()
  85. default : return nil
  86. }
  87. }
  88. }