Skip to content

Instantly share code, notes, and snippets.

@chris-hatton
Last active May 17, 2016 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-hatton/ffa1eba3e28372fd085d37ff39c71607 to your computer and use it in GitHub Desktop.
Save chris-hatton/ffa1eba3e28372fd085d37ff39c71607 to your computer and use it in GitHub Desktop.
Implementation of HTTP status codes as Swift enumerations, with descriptions
//
// HTTPStatus.swift
//
// Created by Chris Hatton on 17/05/2016.
//
// Transcribed from: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
//
import Foundation
public enum HTTPStatus : CustomStringConvertible
{
private static let UnspecifiedSubtypeDescription = "Unspecified sub-type"
case Info ( subType: HTTPInfo? )
case Success ( subType: HTTPSuccess? )
case Redirection ( subType: HTTPRedirection? )
case ClientError ( subType: HTTPClientError? )
case ServerError ( subType: HTTPServerError? )
public init?( code: Int )
{
switch code
{
case 100 ..< 200: self = .Info ( subType: HTTPInfo ( rawValue: code ) )
case 200 ..< 300: self = .Success ( subType: HTTPSuccess ( rawValue: code ) )
case 300 ..< 400: self = .Redirection ( subType: HTTPRedirection ( rawValue: code ) )
case 400 ..< 500: self = .ClientError ( subType: HTTPClientError ( rawValue: code ) )
case 500 ..< 600: self = .ServerError ( subType: HTTPServerError ( rawValue: code ) )
default: return nil
}
}
public var description: String
{
switch self
{
case .Info ( let subType ): return "Info: " + ( subType?.description ?? HTTPStatus.UnspecifiedSubtypeDescription )
case .Success ( let subType ): return "Success: " + ( subType?.description ?? HTTPStatus.UnspecifiedSubtypeDescription )
case .Redirection ( let subType ): return "Redirection: " + ( subType?.description ?? HTTPStatus.UnspecifiedSubtypeDescription )
case .ClientError ( let subType ): return "Client error: " + ( subType?.description ?? HTTPStatus.UnspecifiedSubtypeDescription )
case .ServerError ( let subType ): return "Server error: " + ( subType?.description ?? HTTPStatus.UnspecifiedSubtypeDescription )
}
}
}
public enum HTTPInfo : Int, CustomStringConvertible
{
case Continue = 100
case SwitchingProtocols = 101
case Processing = 102
public var description: String
{
switch self
{
case .Continue: return "Continue"
case .SwitchingProtocols: return "Switching protocols"
case .Processing: return "Processing"
}
}
}
public enum HTTPSuccess : Int
{
case OK = 200
case Created = 201
case Accepted = 202
case NonAuthoritativeInformation = 203
case NoContent = 204
case ResetContent = 205
case PartialContent = 206
case MultiStatus = 207
case AlreadyReported = 208
case IMUsed = 226
public var description: String
{
switch self
{
case .OK: return "OK"
case .Created: return "Created"
case .Accepted: return "Accepted"
case .NonAuthoritativeInformation: return "Non authoritative information"
case .NoContent: return "No content"
case .ResetContent: return "Reset content"
case .PartialContent: return "Partial content"
case .MultiStatus: return "Multi status"
case .AlreadyReported: return "Already reported"
case .IMUsed: return "IM used"
}
}
}
public enum HTTPRedirection : Int, CustomStringConvertible
{
case MultipleChoices = 300
case MovedPermanently = 301
case Found = 302
case SeeOther = 303
case NotModified = 304
case UseProxy = 305
case SwitchProxy = 306
case TemporaryRedirect = 307
case PermanentRedirect = 308
public var description: String
{
switch self
{
case .MultipleChoices: return "Multiple choices"
case .MovedPermanently: return "Moved permanently"
case .Found: return "Found"
case .SeeOther: return "See other"
case .NotModified: return "Not modified"
case .UseProxy: return "Use proxy"
case .SwitchProxy: return "Switch proxy"
case .TemporaryRedirect: return "Temporary redirect"
case .PermanentRedirect: return "Permanent redirect"
}
}
}
public enum HTTPClientError : Int, CustomStringConvertible
{
case BadRequest = 400
case Unauthorized = 401
case PaymentRequired = 402
case Forbidden = 403
case NotFound = 404
case MethodNotAllowed = 405
case NotAcceptable = 406
case ProxyAuthenticationRequired = 407
case RequestTimeout = 408
case Conflict = 409
case Gone = 410
case LengthRequired = 411
case PreconditionFailed = 412
case PayloadTooLarge = 413
case URITooLong = 414
case UnsupportedMediaType = 415
case RangeNotSatifiable = 416
case ImATeapot = 418
case MisdirectRequested = 421
case UnprocessedEntity = 422
case Locked = 423
case FailedDependency = 424
case UpgradeRequired = 426
case PreconditionRequired = 428
case TooManyRequests = 429
case RequestHeaderFieldsTooLarge = 431
case UnavailableForLegalReasons = 451
public var description: String
{
switch self
{
case .BadRequest: return "Bad request"
case .Unauthorized: return "Unauthorized"
case .PaymentRequired: return "Payment required"
case .Forbidden: return "Forbidden"
case .NotFound: return "NotFound"
case .MethodNotAllowed: return "Method not allowed"
case .NotAcceptable: return "Not acceptable"
case .ProxyAuthenticationRequired: return "Proxy authentication required"
case .RequestTimeout: return "Request timeout"
case .Conflict: return "Conflict"
case .Gone: return "Gone"
case .LengthRequired: return "Length required"
case .PreconditionFailed: return "Precondition failed"
case .PayloadTooLarge: return "Payload too large"
case .URITooLong: return "URI too long"
case .UnsupportedMediaType: return "Unsupported media type"
case .RangeNotSatifiable: return "Range not datifiable"
case .ImATeapot: return "I'm a teapot"
case .MisdirectRequested: return "Misdirect requested"
case .UnprocessedEntity: return "Unprocessed entity"
case .Locked: return "Locked"
case .FailedDependency: return "Failed dependency"
case .UpgradeRequired: return "Upgrade required"
case .PreconditionRequired: return "Precondition required"
case .TooManyRequests: return "Too many requests"
case .RequestHeaderFieldsTooLarge: return "Request header fields too large"
case .UnavailableForLegalReasons: return "Unavailable for legal reasons"
}
}
}
public enum HTTPServerError : Int, CustomStringConvertible
{
case InternalServerError = 500
case NotImplemented = 501
case BadGateway = 502
case ServiceUnavailable = 503
case GatewayTimeout = 504
case HTTPVersionNotSupported = 505
case VariantAlsoNegotiates = 506
case InsufficientStorage = 507
case LoopDetected = 508
case NotExtended = 510
case NetworkAuthenticationRequired = 511
public var description: String
{
switch self
{
case .InternalServerError: return "Internal server error"
case .NotImplemented: return "Not implemented"
case .BadGateway: return "Bad gateway"
case .ServiceUnavailable: return "Service unavailable"
case .GatewayTimeout: return "Gateway timeout"
case .HTTPVersionNotSupported: return "HTTPVersion not supported"
case .VariantAlsoNegotiates: return "Variant also negotiates"
case .InsufficientStorage: return "Insufficient storage"
case .LoopDetected: return "Loop detected"
case .NotExtended: return "Not extended"
case .NetworkAuthenticationRequired: return "Network authentication required"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment