Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Rename ClientParams to ClientRequest #856

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions zio-http/src/main/scala/zhttp/service/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import zhttp.http.URL.Location
import zhttp.http._
import zhttp.http.headers.HeaderExtension
import zhttp.service
import zhttp.service.Client.{ClientParams, ClientResponse}
import zhttp.service.Client.{ClientRequest, ClientResponse}
import zhttp.service.client.ClientSSLHandler.ClientSSLOptions
import zhttp.service.client.{ClientChannelInitializer, ClientInboundHandler}
import zio.{Chunk, Promise, Task, ZIO}
Expand All @@ -23,7 +23,7 @@ import java.net.{InetAddress, InetSocketAddress}
final case class Client(rtm: HttpRuntime[Any], cf: JChannelFactory[Channel], el: JEventLoopGroup)
extends HttpMessageCodec {
def request(
request: Client.ClientParams,
request: Client.ClientRequest,
sslOption: ClientSSLOptions = ClientSSLOptions.DefaultSSL,
): Task[Client.ClientResponse] =
for {
Expand All @@ -33,7 +33,7 @@ final case class Client(rtm: HttpRuntime[Any], cf: JChannelFactory[Channel], el:
} yield res

private def asyncRequest(
req: ClientParams,
req: ClientRequest,
promise: Promise[Throwable, ClientResponse],
sslOption: ClientSSLOptions,
): Unit = {
Expand Down Expand Up @@ -111,49 +111,49 @@ object Client {
method: Method,
url: URL,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
request(ClientParams(method, url))
request(ClientRequest(method, url))

def request(
method: Method,
url: URL,
sslOptions: ClientSSLOptions,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
request(ClientParams(method, url), sslOptions)
request(ClientRequest(method, url), sslOptions)

def request(
method: Method,
url: URL,
headers: Headers,
sslOptions: ClientSSLOptions,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
request(ClientParams(method, url, headers), sslOptions)
request(ClientRequest(method, url, headers), sslOptions)

def request(
method: Method,
url: URL,
headers: Headers,
content: HttpData,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
request(ClientParams(method, url, headers, content))
request(ClientRequest(method, url, headers, content))

def request(
req: ClientParams,
req: ClientRequest,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
make.flatMap(_.request(req))

def request(
req: ClientParams,
req: ClientRequest,
sslOptions: ClientSSLOptions,
): ZIO[EventLoopGroup with ChannelFactory, Throwable, ClientResponse] =
make.flatMap(_.request(req, sslOptions))

final case class ClientParams(
final case class ClientRequest(
method: Method,
url: URL,
getHeaders: Headers = Headers.empty,
data: HttpData = HttpData.empty,
private val channelContext: ChannelHandlerContext = null,
) extends HeaderExtension[ClientParams] { self =>
) extends HeaderExtension[ClientRequest] { self =>

def getBodyAsString: Option[String] = data match {
case HttpData.Text(text, _) => Some(text)
Expand All @@ -172,7 +172,7 @@ object Client {
/**
* Updates the headers using the provided function
*/
override def updateHeaders(update: Headers => Headers): ClientParams =
override def updateHeaders(update: Headers => Headers): ClientRequest =
self.copy(getHeaders = update(self.getHeaders))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait EncodeClientParams {
/**
* Converts client params to JFullHttpRequest
*/
def encodeClientParams(jVersion: HttpVersion, req: Client.ClientParams): FullHttpRequest = {
def encodeClientParams(jVersion: HttpVersion, req: Client.ClientRequest): FullHttpRequest = {
val method = req.method.asHttpMethod
val uri = req.url.asString
val content = req.getBodyAsString match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import zio.random.Random
import zio.test.Assertion._
import zio.test._

object EncodeClientParamsSpec extends DefaultRunnableSpec with EncodeClientParams {
object EncodeClientRequestSpec extends DefaultRunnableSpec with EncodeClientParams {

val anyClientParam: Gen[Random with Sized, Client.ClientParams] = HttpGen.clientParams(
val anyClientParam: Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientParams(
HttpGen.httpData(
Gen.listOf(Gen.alphaNumericString),
),
)

def clientParamWithFiniteData(size: Int): Gen[Random with Sized, Client.ClientParams] = HttpGen.clientParams(
def clientParamWithFiniteData(size: Int): Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientParams(
for {
content <- Gen.alphaNumericStringBounded(size, size)
data <- Gen.fromIterable(List(HttpData.fromString(content)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object GetBodyAsStringSpec extends DefaultRunnableSpec {

check(charsetGen) { charset =>
val encoded = Client
.ClientParams(
.ClientRequest(
Method.GET,
URL(Path("/")),
getHeaders = Headers(HttpHeaderNames.CONTENT_TYPE.toString, s"text/html; charset=$charset"),
Expand All @@ -33,7 +33,7 @@ object GetBodyAsStringSpec extends DefaultRunnableSpec {
test("should map bytes to default utf-8 if no charset given") {
val data = Chunk.fromArray("abc".getBytes())
val content = HttpData.BinaryChunk(data)
val request = Client.ClientParams(Method.GET, URL(Path("/")), data = content)
val request = Client.ClientRequest(Method.GET, URL(Path("/")), data = content)
val encoded = request.getBodyAsString
val actual = Option(new String(data.toArray, HTTP_CHARSET))
assert(actual)(equalTo(encoded))
Expand Down
6 changes: 3 additions & 3 deletions zio-http/src/test/scala/zhttp/internal/HttpGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zhttp.internal

import io.netty.buffer.Unpooled
import zhttp.http._
import zhttp.service.Client.ClientParams
import zhttp.service.Client.ClientRequest
import zio.random.Random
import zio.stream.ZStream
import zio.test.{Gen, Sized}
Expand All @@ -17,15 +17,15 @@ object HttpGen {
url <- HttpGen.url
headers <- Gen.listOf(HttpGen.header).map(Headers(_))
data <- dataGen
} yield ClientParams(method, url, headers, data)
} yield ClientRequest(method, url, headers, data)

def clientParamsForFileHttpData() = {
for {
file <- Gen.fromEffect(ZIO.succeed(new File(getClass.getResource("/TestFile.txt").getPath)))
method <- HttpGen.method
url <- HttpGen.url
headers <- Gen.listOf(HttpGen.header).map(Headers(_))
} yield ClientParams(method, url, headers, HttpData.fromFile(file))
} yield ClientRequest(method, url, headers, HttpData.fromFile(file))
}

def cookies: Gen[Random with Sized, Cookie] = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class HttpRunnableSpec extends DefaultRunnableSpec { self =>
port <- DynamicServer.getPort
data = HttpData.fromString(content)
response <- Client.request(
Client.ClientParams(method, URL(path, Location.Absolute(Scheme.HTTP, "localhost", port)), headers, data),
Client.ClientRequest(method, URL(path, Location.Absolute(Scheme.HTTP, "localhost", port)), headers, data),
ClientSSLOptions.DefaultSSL,
)
} yield response
Expand Down