Skip to content

Commit

Permalink
refactor: add HeaderNames and HeaderValues objects to access names an…
Browse files Browse the repository at this point in the history
…d values. Simpler than `Headers.Literals.Names.Accept` (#730)
  • Loading branch information
Tushar Mathur authored Dec 30, 2021
1 parent dc7413d commit 1918118
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 334 deletions.
9 changes: 5 additions & 4 deletions example/src/main/scala/example/HelloWorldTwirl.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package example

import zhttp.endpoint._
import zhttp.http.Headers.Literals._
import zhttp.http.Method.GET
import zhttp.http._
import zhttp.nav.Navigation
Expand All @@ -15,15 +14,15 @@ object HelloWorldTwirl extends zio.App {
val (a, b) = pathParams.params
val response: UResponse = Response.text(advanced.html.index(a, b).toString())
response
.copy(headers = Headers(Name.ContentType, Value.TextHtml))
.copy(headers = Headers(HeaderNames.contentType, HeaderValues.textHtml))
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode

val h1: HttpApp[Any, Nothing] = Http.collect[Request] {
case Method.GET -> !! =>
Response.text(html.index("John Doe").toString()).addHeader(Name.ContentType, Value.TextHtml)
Response.text(html.index("John Doe").toString()).addHeader(HeaderNames.contentType, HeaderValues.textHtml)
case Method.GET -> !! / "nav" => {
val nav = Seq(
Navigation("link-1", "http://google.com", Some("bi-alarm-fill")),
Expand All @@ -40,7 +39,9 @@ object HelloWorldTwirl extends zio.App {
),
),
)
Response.text(advanced.html.template(1, false, nav).toString()).addHeader(Name.ContentType, Value.TextHtml)
Response
.text(advanced.html.template(1, false, nav).toString())
.addHeader(HeaderNames.contentType, HeaderValues.textHtml)
}
}
}
7 changes: 1 addition & 6 deletions zio-http/src/main/scala/zhttp/http/Headers.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zhttp.http

import io.netty.handler.codec.http.{DefaultHttpHeaders, HttpHeaders}
import zhttp.http.headers.{HeaderConstructors, HeaderExtension, HeaderNames, HeaderValues}
import zhttp.http.headers.{HeaderConstructors, HeaderExtension}
import zio.Chunk

import scala.jdk.CollectionConverters._
Expand Down Expand Up @@ -65,9 +65,4 @@ object Headers extends HeaderConstructors {

private[zhttp] def decode(headers: HttpHeaders): Headers =
Headers(headers.entries().asScala.toList.map(entry => (entry.getKey, entry.getValue)))

object Literals {
object Name extends HeaderNames
object Value extends HeaderValues
}
}
7 changes: 3 additions & 4 deletions zio-http/src/main/scala/zhttp/http/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.netty.handler.codec.http.HttpVersion.HTTP_1_1
import io.netty.handler.codec.http.{HttpHeaderNames, HttpResponse}
import zhttp.core.Util
import zhttp.html.Html
import zhttp.http.Headers.Literals._
import zhttp.http.HttpError.HTTPErrorWithCause
import zhttp.http.headers.HeaderExtension
import zhttp.socket.{Socket, SocketApp, WebSocketFrame}
Expand Down Expand Up @@ -127,7 +126,7 @@ object Response {
def html(data: Html): UResponse =
Response(
data = HttpData.fromString("<!DOCTYPE html>" + data.encode),
headers = Headers(Name.ContentType, Value.TextHtml),
headers = Headers(HeaderNames.contentType, HeaderValues.textHtml),
)

@deprecated("Use `Response(status, headers, data)` constructor instead.", "22-Sep-2021")
Expand All @@ -143,7 +142,7 @@ object Response {
def json(data: String): UResponse =
Response(
data = HttpData.fromChunk(Chunk.fromArray(data.getBytes(HTTP_CHARSET))),
headers = Headers(Name.ContentLength, Value.ApplicationJson),
headers = Headers(HeaderNames.contentLength, HeaderValues.applicationJson),
)

/**
Expand Down Expand Up @@ -182,7 +181,7 @@ object Response {
def text(text: String, charset: Charset = HTTP_CHARSET): UResponse =
Response(
data = HttpData.fromString(text, charset),
headers = Headers(Name.ContentType, Value.TextPlain),
headers = Headers(HeaderNames.contentType, HeaderValues.textPlain),
)

/**
Expand Down
12 changes: 6 additions & 6 deletions zio-http/src/main/scala/zhttp/http/headers/HeaderChecks.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zhttp.http.headers

import io.netty.util.AsciiString.contentEqualsIgnoreCase
import zhttp.http.Headers.Literals.Value
import zhttp.http.HeaderValues

/**
* Maintains a list of operators that checks if the Headers meet the give constraints.
Expand All @@ -14,7 +14,7 @@ trait HeaderChecks[+A] { self: HeaderExtension[A] with A =>
getContentType.exists(contentEqualsIgnoreCase(value, _))

final def hasFormUrlencodedContentType: Boolean =
hasContentType(Value.ApplicationXWWWFormUrlencoded)
hasContentType(HeaderValues.applicationXWWWFormUrlencoded)

final def hasHeader(name: CharSequence, value: CharSequence): Boolean =
getHeaderValue(name) match {
Expand All @@ -26,14 +26,14 @@ trait HeaderChecks[+A] { self: HeaderExtension[A] with A =>
getHeaderValue(name).nonEmpty

final def hasJsonContentType: Boolean =
hasContentType(Value.ApplicationJson)
hasContentType(HeaderValues.applicationJson)

final def hasTextPlainContentType: Boolean =
hasContentType(Value.TextPlain)
hasContentType(HeaderValues.textPlain)

final def hasXhtmlXmlContentType: Boolean =
hasContentType(Value.ApplicationXhtml)
hasContentType(HeaderValues.applicationXhtml)

final def hasXmlContentType: Boolean =
hasContentType(Value.ApplicationXml)
hasContentType(HeaderValues.applicationXml)
}
Loading

0 comments on commit 1918118

Please sign in to comment.