1
package hammock
2

3
import cats.{Eq, Show}
4
import cats.instances.int._
5
import cats.syntax.contravariant._
6
import monocle.macros.Lenses
7

8
@Lenses case class Status(code: Int, text: String, description: String) {
9 1
  def isInformational: Boolean = this.code / 100 == 1
10 1
  def isSuccess: Boolean       = this.code / 100 == 2
11 1
  def isRedirection: Boolean   = this.code / 100 == 3
12 1
  def isClientError: Boolean   = this.code / 100 == 4
13 1
  def isServerError: Boolean   = this.code / 100 == 5
14
}
15

16
object Status {
17 1
  val Continue = Status(
18
    100,
19
    "Continue",
20
    "The server has received the request headers, and the client should proceed to send the request body.")
21
  val SwitchingProtocols =
22 1
    Status(101, "Switching Protocols", "The server is switching protocols, because the client requested the switch.")
23 1
  val Processing = Status(102, "Processing", "The server is processing the request, but no response is available yet.")
24 1
  val OK         = Status(200, "OK", "OK")
25 1
  val Created    = Status(201, "Created", "The request has been fulfilled and resulted in a new resource being created.")
26
  val Accepted =
27 1
    Status(202, "Accepted", "The request has been accepted for processing, but the processing has not been completed.")
28 1
  val NonAuthoritativeInformation = Status(
29
    203,
30
    "Non-Authoritative Information",
31
    "The server successfully processed the request, but is returning information that may be from another source.")
32
  val NoContent =
33 1
    Status(204, "No Content", "The server successfully processed the request and is not returning any content.")
34
  val ResetContent =
35 1
    Status(205, "Reset Content", "The server successfully processed the request, but is not returning any content.")
36 1
  val PartialContent = Status(
37
    206,
38
    "Partial Content",
39
    "The server is delivering only part of the resource due to a range header sent by the client.")
40 1
  val MultiStatus = Status(
41
    207,
42
    "Multi-Status",
43
    "The message body that follows is an XML message and can contain a number of separate response codes, depending on how many sub-requests were made."
44
  )
45 1
  val AlreadyReported = Status(
46
    208,
47
    "Already Reported",
48
    "The members of a DAV binding have already been enumerated in a previous reply to this request, and are not being included again."
49
  )
50 1
  val IMUsed = Status(
51
    226,
52
    "IM Used",
53
    "The server has fulfilled a GET request for the resource, and the response is a representation of the result of one or more instance-manipulations applied to the current instance."
54
  )
55 1
  val MultipleChoices = Status(300, "Multiple Choices", "")
56
  val MovedPermanently =
57 1
    Status(301, "Moved Permanently", "This and all future requests should be directed to the given URI.")
58 1
  val Found = Status(302, "Found", "The resource was found, but at a different URI.")
59
  val SeeOther =
60 1
    Status(303, "See Other", "The response to the request can be found under another URI using a GET method.")
61 1
  val NotModified = Status(304, "Not Modified", "The resource has not been modified since last requested.")
62
  val UseProxy =
63 1
    Status(305, "Use Proxy", "This single request is to be repeated via the proxy given by the Location field.")
64 1
  val TemporaryRedirect = Status(
65
    307,
66
    "Temporary Redirect",
67
    "The request should be repeated with another URI, but future requests can still use the original URI.")
68
  val PermanentRedirect =
69 1
    Status(308, "Permanent Redirect", "The request, and all future requests should be repeated using another URI.")
70 1
  val BadRequest      = Status(400, "Bad Request", "The request contains bad syntax or cannot be fulfilled.")
71 1
  val Unauthorized    = Status(401, "Unauthorized", "Authentication is possible but has failed or not yet been provided.")
72 1
  val PaymentRequired = Status(402, "Payment Required", "Reserved for future use.")
73
  val Forbidden =
74 1
    Status(403, "Forbidden", "The request was a legal request, but the server is refusing to respond to it.")
75
  val NotFound =
76 1
    Status(404, "Not Found", "The requested resource could not be found but may be available again in the future.")
77 1
  val MethodNotAllowed = Status(
78
    405,
79
    "Method Not Allowed",
80
    "A request was made of a resource using a request method not supported by that resource;")
81 1
  val NotAcceptable = Status(
82
    406,
83
    "Not Acceptable",
84
    "The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request."
85
  )
86
  val ProxyAuthenticationRequired =
87 1
    Status(407, "Proxy Authentication Required", "Proxy authentication is required to access the requested resource.")
88 1
  val RequestTimeout = Status(408, "Request Timeout", "The server timed out waiting for the request.")
89 1
  val Conflict = Status(
90
    409,
91
    "Conflict",
92
    "The request could not be processed because of conflict in the request, such as an edit conflict.")
93 1
  val Gone = Status(410, "Gone", "The resource requested is no longer available and will not be available again.")
94 1
  val LengthRequired = Status(
95
    411,
96
    "Length Required",
97
    "The request did not specify the length of its content, which is required by the requested resource.")
98 1
  val PreconditionFailed = Status(
99
    412,
100
    "Precondition Failed",
101
    "The server does not meet one of the preconditions that the requester put on the request.")
102
  val RequestEntityTooLarge =
103 1
    Status(413, "Request Entity Too Large", "The request is larger than the server is willing or able to process.")
104
  val RequestUriTooLong =
105 1
    Status(414, "Request-URI Too Long", "The URI provided was too long for the server to process.")
106 1
  val UnsupportedMediaType = Status(
107
    415,
108
    "Unsupported Media Type",
109
    "The request entity has a media type which the server or resource does not support.")
110 1
  val RequestedRangeNotSatisfiable = Status(
111
    416,
112
    "Requested Range Not Satisfiable",
113
    "The client has asked for a portion of the file, but the server cannot supply that portion.")
114
  val ExpectationFailed =
115 1
    Status(417, "Expectation Failed", "The server cannot meet the requirements of the Expect request-header field.")
116 1
  val EnhanceYourCalm = Status(420, "Enhance Your Calm", "You are being rate-limited.")
117 1
  val UnprocessableEntity = Status(
118
    422,
119
    "Unprocessable Entity",
120
    "The request was well-formed but was unable to be followed due to semantic errors.")
121 1
  val Locked           = Status(423, "Locked", "The resource that is being accessed is locked.")
122 1
  val FailedDependency = Status(424, "Failed Dependency", "The request failed due to failure of a previous request.")
123
  val TooEarly =
124 1
    Status(425, "Too Early", "The server is unwilling to risk processing a request that might be replayed.")
125 1
  val UpgradeRequired      = Status(426, "Upgrade Required", "The client should switch to a different protocol.")
126 1
  val PreconditionRequired = Status(428, "Precondition Required", "The server requires the request to be conditional.")
127
  val TooManyRequests =
128 1
    Status(429, "Too Many Requests", "The user has sent too many requests in a given amount of time.")
129 1
  val RequestHeaderFieldsTooLarge = Status(
130
    431,
131
    "Request Header Fields Too Large",
132
    "The server is unwilling to process the request because either an individual header field, or all the header fields collectively, are too large."
133
  )
134 1
  val RetryWith = Status(449, "Retry With", "The request should be retried after doing the appropriate action.")
135 1
  val BlockedByParentalControls = Status(
136
    450,
137
    "Blocked by Windows Parental Controls",
138
    "Windows Parental Controls are turned on and are blocking access to the given webpage.")
139
  val UnavailableForLegalReasons =
140 1
    Status(451, "Unavailable For Legal Reasons", "Resource access is denied for legal reasons.")
141 1
  val InternalServerError = Status(500, "Internal Server Error", "There was an internal server error.")
142 1
  val NotImplemented = Status(
143
    501,
144
    "Not Implemented",
145
    "The server either does not recognize the request method, or it lacks the ability to fulfill the request.")
146 1
  val BadGateway = Status(
147
    502,
148
    "Bad Gateway",
149
    "The server was acting as a gateway or proxy and received an invalid response from the upstream server.")
150 1
  val ServiceUnavailable = Status(
151
    503,
152
    "Service Unavailable",
153
    "The server is currently unavailable (because it is overloaded or down for maintenance).")
154 1
  val GatewayTimeout = Status(
155
    504,
156
    "Gateway Timeout",
157
    "The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.")
158 1
  val HTTPVersionNotSupported = Status(
159
    505,
160
    "HTTP Version Not Supported",
161
    "The server does not support the HTTP protocol version used in the request.")
162 1
  val VariantAlsoNegotiates = Status(
163
    506,
164
    "Variant Also Negotiates",
165
    "Transparent content negotiation for the request, results in a circular reference.")
166 1
  val InsufficientStorage    = Status(507, "Insufficient Storage", "Insufficient storage to complete the request.")
167 1
  val LoopDetected           = Status(508, "Loop Detected", "The server detected an infinite loop while processing the request.")
168 1
  val BandwidthLimitExceeded = Status(509, "Bandwidth Limit Exceeded", "Bandwidth limit has been exceeded.")
169
  val NotExtended =
170 1
    Status(510, "Not Extended", "Further extensions to the request are required for the server to fulfill it.")
171
  val NetworkAuthenticationRequired =
172 1
    Status(511, "Network Authentication Required", "The client needs to authenticate to gain network access.")
173 1
  val NetworkReadTimeout    = Status(598, "Network read timeout error", "")
174 1
  val NetworkConnectTimeout = Status(599, "Network connect timeout error", "")
175

176 0
  def custom(x: Int, text: String = "", description: String = "") = Status(x, text, description)
177

178 1
  val Statuses = Map(
179 1
    100 -> Continue,
180 1
    101 -> SwitchingProtocols,
181 1
    102 -> Processing,
182 1
    200 -> OK,
183 1
    201 -> Created,
184 1
    202 -> Accepted,
185 1
    203 -> NonAuthoritativeInformation,
186 1
    204 -> NoContent,
187 1
    205 -> ResetContent,
188 1
    206 -> PartialContent,
189 1
    207 -> MultiStatus,
190 1
    208 -> AlreadyReported,
191 1
    226 -> IMUsed,
192 1
    300 -> MultipleChoices,
193 1
    301 -> MovedPermanently,
194 1
    302 -> Found,
195 1
    303 -> SeeOther,
196 1
    304 -> NotModified,
197 1
    305 -> UseProxy,
198 1
    307 -> TemporaryRedirect,
199 1
    308 -> PermanentRedirect,
200 1
    400 -> BadRequest,
201 1
    401 -> Unauthorized,
202 1
    402 -> PaymentRequired,
203 1
    403 -> Forbidden,
204 1
    404 -> NotFound,
205 1
    405 -> MethodNotAllowed,
206 1
    406 -> NotAcceptable,
207 1
    407 -> ProxyAuthenticationRequired,
208 1
    408 -> RequestTimeout,
209 1
    409 -> Conflict,
210 1
    410 -> Gone,
211 1
    411 -> LengthRequired,
212 1
    412 -> PreconditionFailed,
213 1
    413 -> RequestEntityTooLarge,
214 1
    414 -> RequestUriTooLong,
215 1
    415 -> UnsupportedMediaType,
216 1
    416 -> RequestedRangeNotSatisfiable,
217 1
    417 -> ExpectationFailed,
218 1
    420 -> EnhanceYourCalm,
219 1
    422 -> UnprocessableEntity,
220 1
    423 -> Locked,
221 1
    424 -> FailedDependency,
222 1
    425 -> TooEarly,
223 1
    426 -> UpgradeRequired,
224 1
    428 -> PreconditionRequired,
225 1
    429 -> TooManyRequests,
226 1
    431 -> RequestHeaderFieldsTooLarge,
227 1
    449 -> RetryWith,
228 1
    450 -> BlockedByParentalControls,
229 1
    451 -> UnavailableForLegalReasons,
230 1
    500 -> InternalServerError,
231 1
    501 -> NotImplemented,
232 1
    502 -> BadGateway,
233 1
    503 -> ServiceUnavailable,
234 1
    504 -> GatewayTimeout,
235 1
    505 -> HTTPVersionNotSupported,
236 1
    506 -> VariantAlsoNegotiates,
237 1
    507 -> InsufficientStorage,
238 1
    508 -> LoopDetected,
239 1
    509 -> BandwidthLimitExceeded,
240 1
    510 -> NotExtended,
241 1
    511 -> NetworkAuthenticationRequired,
242 1
    598 -> NetworkReadTimeout,
243 1
    599 -> NetworkConnectTimeout
244
  )
245

246 0
  def get(code: Int): Status = Statuses.getOrElse(code, Status(code, "Undefined", "Undefined StatusCode"))
247

248 1
  implicit val show: Show[Status] = Show[Int].contramap(_.code)
249 1
  implicit val eq: Eq[Status]     = Eq.fromUniversalEquals
250
}

Read our documentation on viewing source code .

Loading