Skip to content

Commit

Permalink
Refactor: Http.Status names to Camel Case (#1129)
Browse files Browse the repository at this point in the history
* support custom Status for HtppError

* support custom Status for HttpError

* updated code based on review.

* fixed tests

* changed from Capitalized Status names to Camel Case

* proper camel case usage

Co-authored-by: Gabriel Ciuloaica <con_gabriel.ciuloaica@dream11.com>
  • Loading branch information
tusharmath and gciuloaica authored Mar 10, 2022
1 parent 94be6b9 commit 5069856
Show file tree
Hide file tree
Showing 27 changed files with 333 additions and 326 deletions.
2 changes: 1 addition & 1 deletion example/src/main/scala/example/StreamingResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object StreamingResponse extends App {
// ZStream powered response
case Method.GET -> !! / "stream" =>
Response(
status = Status.OK,
status = Status.Ok,
headers = Headers.contentLength(message.length.toLong),
data = HttpData.fromStream(ZStream.fromChunk(message)), // Encoding content using a ZStream
)
Expand Down
6 changes: 3 additions & 3 deletions zio-http/src/main/scala/zhttp/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ object Http {
/**
* Creates an HTTP app which always responds with a 200 status code.
*/
def ok: HttpApp[Any, Nothing] = status(Status.OK)
def ok: HttpApp[Any, Nothing] = status(Status.Ok)

/**
* Creates an Http app which always responds with the same value.
Expand Down Expand Up @@ -956,12 +956,12 @@ object Http {
* Creates an Http app that responds with a 408 status code after the provided
* time duration
*/
def timeout(duration: Duration): HttpApp[Clock, Nothing] = Http.status(Status.REQUEST_TIMEOUT).delay(duration)
def timeout(duration: Duration): HttpApp[Clock, Nothing] = Http.status(Status.RequestTimeout).delay(duration)

/**
* Creates an HTTP app which always responds with a 413 status code.
*/
def tooLarge: HttpApp[Any, Nothing] = Http.status(Status.REQUEST_ENTITY_TOO_LARGE)
def tooLarge: HttpApp[Any, Nothing] = Http.status(Status.RequestEntityTooLarge)

// Ctor Help
final case class PartialCollectZIO[A](unit: Unit) extends AnyVal {
Expand Down
77 changes: 39 additions & 38 deletions zio-http/src/main/scala/zhttp/http/HttpError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,98 +27,99 @@ object HttpError {
cause.foreach(initCause)
}

final case class BadRequest(msg: String = "Bad Request") extends HttpError(Status.BAD_REQUEST, msg)
final case class BadRequest(msg: String = "Bad Request") extends HttpError(Status.BadRequest, msg)

final case class Unauthorized(msg: String = "Unauthorized") extends HttpError(Status.UNAUTHORIZED, msg)
final case class Unauthorized(msg: String = "Unauthorized") extends HttpError(Status.Unauthorized, msg)

final case class PaymentRequired(msg: String = "Payment Required") extends HttpError(Status.PAYMENT_REQUIRED, msg)
final case class PaymentRequired(msg: String = "Payment Required") extends HttpError(Status.PaymentRequired, msg)

final case class Forbidden(msg: String = "Forbidden") extends HttpError(Status.FORBIDDEN, msg)
final case class Forbidden(msg: String = "Forbidden") extends HttpError(Status.Forbidden, msg)

final case class NotFound(path: Path)
extends HttpError(Status.NOT_FOUND, s"""The requested URI "${path.encode}" was not found on this server\n""")
extends HttpError(Status.NotFound, s"""The requested URI "${path.encode}" was not found on this server\n""")

final case class MethodNotAllowed(msg: String = "Method Not Allowed")
extends HttpError(Status.METHOD_NOT_ALLOWED, msg)
final case class MethodNotAllowed(msg: String = "Method Not Allowed") extends HttpError(Status.MethodNotAllowed, msg)

final case class NotAcceptable(msg: String = "Not Acceptable") extends HttpError(Status.NOT_ACCEPTABLE, msg)
final case class NotAcceptable(msg: String = "Not Acceptable") extends HttpError(Status.NotAcceptable, msg)

final case class ProxyAuthenticationRequired(msg: String = "Proxy Authentication Required")
extends HttpError(Status.PROXY_AUTHENTICATION_REQUIRED, msg)
extends HttpError(Status.ProxyAuthenticationRequired, msg)

final case class Conflict(msg: String = "Conflict") extends HttpError(Status.CONFLICT, msg)
final case class Conflict(msg: String = "Conflict") extends HttpError(Status.Conflict, msg)

final case class Gone(msg: String = "Gone") extends HttpError(Status.GONE, msg)
final case class Gone(msg: String = "Gone") extends HttpError(Status.Gone, msg)

final case class LengthRequired(msg: String = "Length Required") extends HttpError(Status.LENGTH_REQUIRED, msg)
final case class LengthRequired(msg: String = "Length Required") extends HttpError(Status.LengthRequired, msg)

final case class PreconditionFailed(msg: String = "Precondition Failed")
extends HttpError(Status.PRECONDITION_FAILED, msg)
extends HttpError(Status.PreconditionFailed, msg)

final case class RequestTimeout(msg: String = "Request Timeout") extends HttpError(Status.REQUEST_TIMEOUT, msg)
final case class RequestTimeout(msg: String = "Request Timeout") extends HttpError(Status.RequestTimeout, msg)

final case class RequestEntityTooLarge(msg: String = "Request Entity Too Large")
extends HttpError(Status.REQUEST_ENTITY_TOO_LARGE, msg)
extends HttpError(Status.RequestEntityTooLarge, msg)

final case class RequestUriTooLong(msg: String = "Request-URI Too Long")
extends HttpError(Status.REQUEST_URI_TOO_LONG, msg)
extends HttpError(Status.RequestUriTooLong, msg)

final case class UnsupportedMediaType(msg: String = "Unsupported Media Type")
extends HttpError(Status.UNSUPPORTED_MEDIA_TYPE, msg)
extends HttpError(Status.UnsupportedMediaType, msg)

final case class RequestedRangeNotSatisfiable(msg: String = "Requested Range Not Satisfiable")
extends HttpError(Status.REQUESTED_RANGE_NOT_SATISFIABLE, msg)
extends HttpError(Status.RequestedRangeNotSatisfiable, msg)

final case class ExpectationFailed(msg: String = "Expectation Failed")
extends HttpError(Status.EXPECTATION_FAILED, msg)
extends HttpError(Status.ExpectationFailed, msg)

final case class MisdirectedRequest(msg: String = "Misdirected Request")
extends HttpError(Status.MISDIRECTED_REQUEST, msg)
extends HttpError(Status.MisdirectedRequest, msg)

final case class UnprocessableEntity(msg: String = "Unprocessable Entity")
extends HttpError(Status.UNPROCESSABLE_ENTITY, msg)
extends HttpError(Status.UnprocessableEntity, msg)

final case class Locked(msg: String = "Locked") extends HttpError(Status.LOCKED, msg)
final case class Locked(msg: String = "Locked") extends HttpError(Status.Locked, msg)

final case class FailedDependency(msg: String = "Failed Dependency") extends HttpError(Status.FAILED_DEPENDENCY, msg)
final case class FailedDependency(msg: String = "Failed Dependency") extends HttpError(Status.FailedDependency, msg)

final case class UnorderedCollection(msg: String = "Unordered Collection")
extends HttpError(Status.UNORDERED_COLLECTION, msg)
extends HttpError(Status.UnorderedCollection, msg)

final case class UpgradeRequired(msg: String = "Upgrade Required") extends HttpError(Status.UPGRADE_REQUIRED, msg)
final case class UpgradeRequired(msg: String = "Upgrade Required") extends HttpError(Status.UpgradeRequired, msg)

final case class PreconditionRequired(msg: String = "Precondition Required")
extends HttpError(Status.PRECONDITION_REQUIRED, msg)
extends HttpError(Status.PreconditionRequired, msg)

final case class TooManyRequests(msg: String = "Too Many Requests") extends HttpError(Status.TOO_MANY_REQUESTS, msg)
final case class TooManyRequests(msg: String = "Too Many Requests") extends HttpError(Status.TooManyRequests, msg)

final case class RequestHeaderFieldsTooLarge(msg: String = "Request Header Fields Too Large")
extends HttpError(Status.REQUEST_HEADER_FIELDS_TOO_LARGE, msg)
extends HttpError(Status.RequestHeaderFieldsTooLarge, msg)

final case class GatewayTimeout(msg: String = "Gateway Timeout") extends HttpError(Status.GATEWAY_TIMEOUT, msg)
final case class GatewayTimeout(msg: String = "Gateway Timeout") extends HttpError(Status.GatewayTimeout, msg)

final case class VariantAlsoNegotiates(msg: String = "Variant Also Negotiates")
extends HttpError(Status.VARIANT_ALSO_NEGOTIATES, msg)
extends HttpError(Status.VariantAlsoNegotiates, msg)

final case class InsufficientStorage(msg: String = "Insufficient Storage")
extends HttpError(Status.INSUFFICIENT_STORAGE, msg)
extends HttpError(Status.InsufficientStorage, msg)

final case class NotExtended(msg: String = "Not Extended") extends HttpError(Status.NOT_EXTENDED, msg)
final case class NotExtended(msg: String = "Not Extended") extends HttpError(Status.NotExtended, msg)

final case class NetworkAuthenticationRequired(msg: String = "Network Authentication Required")
extends HttpError(Status.NETWORK_AUTHENTICATION_REQUIRED, msg)
extends HttpError(Status.NetworkAuthenticationRequired, msg)

final case class InternalServerError(msg: String = "Internal Server Error", cause: Option[Throwable] = None)
extends HTTPErrorWithCause(Status.INTERNAL_SERVER_ERROR, msg)
extends HTTPErrorWithCause(Status.InternalServerError, msg)

final case class NotImplemented(msg: String = "Not Implemented") extends HttpError(Status.NOT_IMPLEMENTED, msg)
final case class NotImplemented(msg: String = "Not Implemented") extends HttpError(Status.NotImplemented, msg)

final case class HttpVersionNotSupported(msg: String = "HTTP Version Not Supported")
extends HttpError(Status.HTTP_VERSION_NOT_SUPPORTED, msg)
extends HttpError(Status.HttpVersionNotSupported, msg)

final case class ServiceUnavailable(msg: String = "Service Unavailable")
extends HttpError(Status.SERVICE_UNAVAILABLE, msg)
extends HttpError(Status.ServiceUnavailable, msg)

final case class BadGateway(msg: String = "Bad Gateway") extends HttpError(Status.BAD_GATEWAY, msg)
final case class BadGateway(msg: String = "Bad Gateway") extends HttpError(Status.BadGateway, msg)

final case class CustomResponseStatus(code: Int, reason: String) extends HttpError(Status.Custom(code), reason)

}
12 changes: 6 additions & 6 deletions zio-http/src/main/scala/zhttp/http/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final case class Response private (

object Response {
def apply[R, E](
status: Status = Status.OK,
status: Status = Status.Ok,
headers: Headers = Headers.empty,
data: HttpData = HttpData.Empty,
): Response =
Expand Down Expand Up @@ -161,7 +161,7 @@ object Response {
def fromSocketApp[R](app: SocketApp[R]): ZIO[R, Nothing, Response] = {
ZIO.environment[R].map { env =>
Response(
Status.SWITCHING_PROTOCOLS,
Status.SwitchingProtocols,
Headers.empty,
HttpData.empty,
Attribute(socketApp = Option(app.provideEnvironment(env))),
Expand All @@ -173,7 +173,7 @@ object Response {
/**
* Creates a response with content-type set to text/html
*/
def html(data: Html, status: Status = Status.OK): Response =
def html(data: Html, status: Status = Status.Ok): Response =
Response(
status = status,
data = HttpData.fromString("<!DOCTYPE html>" + data.encode),
Expand All @@ -182,7 +182,7 @@ object Response {

@deprecated("Use `Response(status, headers, data)` constructor instead.", "22-Sep-2021")
def http[R, E](
status: Status = Status.OK,
status: Status = Status.Ok,
headers: Headers = Headers.empty,
data: HttpData = HttpData.empty,
): Response = Response(status, headers, data)
Expand All @@ -199,14 +199,14 @@ object Response {
/**
* Creates an empty response with status 200
*/
def ok: Response = Response(Status.OK)
def ok: Response = Response(Status.Ok)

/**
* Creates an empty response with status 301 or 302 depending on if it's
* permanent or not.
*/
def redirect(location: String, isPermanent: Boolean = false): Response = {
val status = if (isPermanent) Status.PERMANENT_REDIRECT else Status.TEMPORARY_REDIRECT
val status = if (isPermanent) Status.PermanentRedirect else Status.TemporaryRedirect
Response(status, Headers.location(location))
}

Expand Down
Loading

0 comments on commit 5069856

Please sign in to comment.