Skip to content

Commit

Permalink
style: rearrange methods in files (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath authored Feb 8, 2022
1 parent 047944f commit d3fa639
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions zio-http/src/main/scala/zhttp/http/Headers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ final case class Headers(toChunk: Chunk[Header]) extends HeaderExtension[Headers

override def headers: Headers = self

def toList: List[(String, String)] = toChunk.map { case (name, value) => (name.toString, value.toString) }.toList

def modify(f: Header => Header): Headers = Headers(toChunk.map(f(_)))

def toList: List[(String, String)] = toChunk.map { case (name, value) => (name.toString, value.toString) }.toList

override def updateHeaders(update: Headers => Headers): Headers = update(self)

def when(cond: Boolean): Headers = if (cond) self else Headers.empty
Expand Down
64 changes: 32 additions & 32 deletions zio-http/src/main/scala/zhttp/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
final def as[C](c: C): Http[R, E, A, C] =
self *> Http.succeed(c)

/**
* Extracts body
*/
final def body(implicit eb: IsResponse[B], ee: E <:< Throwable): Http[R, Throwable, A, Chunk[Byte]] =
self.bodyAsByteBuf.mapZIO(buf => Task(Chunk.fromArray(ByteBufUtil.getBytes(buf))))

/**
* Extracts body as a string
*/
final def bodyAsString(implicit eb: IsResponse[B], ee: E <:< Throwable): Http[R, Throwable, A, String] =
self.bodyAsByteBuf.mapZIO(bytes => Task(bytes.toString(HTTP_CHARSET)))

/**
* Catches all the exceptions that the http app can fail with
*/
Expand Down Expand Up @@ -115,6 +127,12 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
final def compose[R1 <: R, E1 >: E, A1 <: A, C1](other: Http[R1, E1, C1, A1]): Http[R1, E1, C1, B] =
other andThen self

/**
* Extracts content-length from the response if available
*/
final def contentLength(implicit eb: IsResponse[B]): Http[R, E, A, Option[Long]] =
headers.map(_.contentLength)

/**
* Transforms the input of the http before passing it on to the current Http
*/
Expand Down Expand Up @@ -179,24 +197,6 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
dd: Http[R1, E1, A1, B1],
): Http[R1, E1, A1, B1] = Http.FoldHttp(self, ee, bb, dd)

/**
* Extracts body
*/
final def body(implicit eb: IsResponse[B], ee: E <:< Throwable): Http[R, Throwable, A, Chunk[Byte]] =
self.bodyAsByteBuf.mapZIO(buf => Task(Chunk.fromArray(ByteBufUtil.getBytes(buf))))

/**
* Extracts body as a string
*/
final def bodyAsString(implicit eb: IsResponse[B], ee: E <:< Throwable): Http[R, Throwable, A, String] =
self.bodyAsByteBuf.mapZIO(bytes => Task(bytes.toString(HTTP_CHARSET)))

/**
* Extracts content-length from the response if available
*/
final def contentLength(implicit eb: IsResponse[B]): Http[R, E, A, Option[Long]] =
headers.map(_.contentLength)

/**
* Extracts the value of the provided header name.
*/
Expand All @@ -208,11 +208,6 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
*/
final def headers(implicit eb: IsResponse[B]): Http[R, E, A, Headers] = self.map(eb.headers)

/**
* Extracts `Status` from the type `B` is possible.
*/
final def status(implicit ev: IsResponse[B]): Http[R, E, A, Status] = self.map(ev.status)

/**
* Transforms the output of the http app
*/
Expand Down Expand Up @@ -292,6 +287,11 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
final def silent[E1 >: E, B1 >: B](implicit s: CanBeSilenced[E1, B1]): Http[R, Nothing, A, B1] =
self.catchAll(e => Http.succeed(s.silent(e)))

/**
* Extracts `Status` from the type `B` is possible.
*/
final def status(implicit ev: IsResponse[B]): Http[R, E, A, Status] = self.map(ev.status)

/**
* Returns an Http that peeks at the success of this Http.
*/
Expand Down Expand Up @@ -368,6 +368,15 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
final def zipRight[R1 <: R, E1 >: E, A1 <: A, C1](other: Http[R1, E1, A1, C1]): Http[R1, E1, A1, C1] =
self.flatMap(_ => other)

/**
* Extracts body as a ByteBuf
*/
private[zhttp] final def bodyAsByteBuf(implicit
eb: IsResponse[B],
ee: E <:< Throwable,
): Http[R, Throwable, A, ByteBuf] =
self.widen[Throwable, B].mapZIO(eb.bodyAsByteBuf)

/**
* Evaluates the app and returns an HExit that can be resolved further
*
Expand Down Expand Up @@ -398,15 +407,6 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>

case RunMiddleware(app, mid) => mid(app).execute(a)
}

/**
* Extracts body as a ByteBuf
*/
private[zhttp] final def bodyAsByteBuf(implicit
eb: IsResponse[B],
ee: E <:< Throwable,
): Http[R, Throwable, A, ByteBuf] =
self.widen[Throwable, B].mapZIO(eb.bodyAsByteBuf)
}

object Http {
Expand Down
3 changes: 2 additions & 1 deletion zio-http/src/main/scala/zhttp/http/Method.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package zhttp.http
import io.netty.handler.codec.http.HttpMethod

sealed trait Method { self =>
override def toString(): String = Method.asHttpMethod(self).name()
lazy val asHttpMethod: HttpMethod = Method.asHttpMethod(self)

override def toString(): String = Method.asHttpMethod(self).name()
}

object Method {
Expand Down
4 changes: 1 addition & 3 deletions zio-http/src/main/scala/zhttp/http/PathModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package zhttp.http
import scala.annotation.tailrec

private[zhttp] trait PathModule { module =>
val !! = Path.End
@deprecated("Use `!!` operator instead.", "23-Aug-2021")
val Root = !!
val !! : Path = Path.End

sealed trait Path { self =>
final override def toString: String = this.encode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zhttp.http.headers

import io.netty.handler.codec.http.HttpHeaderNames
import zhttp.http.Headers.BasicSchemeName
import zhttp.http.{Cookie, HTTP_CHARSET, HeaderNames, Headers, Method}
import zhttp.http._
import zio.duration.Duration

import java.util.Base64
Expand Down

0 comments on commit d3fa639

Please sign in to comment.