Skip to content

Commit

Permalink
Merge branch 'zio-series/2.x' of https://github.com/dream11/zio-http
Browse files Browse the repository at this point in the history
…into rc3
  • Loading branch information
adamgfraser committed Mar 23, 2022
2 parents 72ffc6b + 093b1e8 commit 909d89c
Show file tree
Hide file tree
Showing 85 changed files with 2,052 additions and 1,045 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ val app = Http.collect[Request] {
case req @ Method.GET -> Root / "fruits" / "a" =>
Response.text("URL:" + req.url.path.asString + " Headers: " + r.headers)
case req @ Method.POST -> Root / "fruits" / "a" =>
Response.text(req.getBodyAsString.getOrElse("No body!"))
Response.text(req.bodyAsString.getOrElse("No body!"))
}
```

Expand Down Expand Up @@ -105,7 +105,7 @@ val app = Http.collect[Request] {
case req @ Method.GET -> Root / "fruits" / "a" =>
Response.text("URL:" + req.url.path.asString + " Headers: " + r.headers)
case req @ Method.POST -> Root / "fruits" / "a" =>
Response.text(req.getBodyAsString.getOrElse("No body!"))
Response.text(req.bodyAsString.getOrElse("No body!"))
}
```

Expand Down
3 changes: 3 additions & 0 deletions docs/website/docs/v1.x/dsl/cookies.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "7"
---
# Cookie

**ZIO HTTP** has special support for Cookie headers using the `Cookie` Domain to add and invalidate cookies. Adding a cookie will generate the correct [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) headers
Expand Down
11 changes: 7 additions & 4 deletions docs/website/docs/v1.x/dsl/headers.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "6"
---
# Headers

**ZIO HTTP** provides support for all HTTP headers (as defined in [RFC2616](https://datatracker.ietf.org/doc/html/rfc2616) ) along with custom headers.
Expand Down Expand Up @@ -142,14 +145,14 @@ val responseHeaders: Task[Headers] = Client.request(url).map(_.headers)
// Pass headers to request
res <- Client.request(url, headers)
// List all response headers
_ <- console.putStrLn(res.headers.toList.mkString("\n"))
_ <- Console.printLine(res.headers.toList.mkString("\n"))
data <-
// Check if response contains a specified header with a specified value.
if (res.hasHeader(HeaderNames.contentType, HeaderValues.applicationJson))
res.getBodyAsString
res.bodyAsString
else
res.getBodyAsString
_ <- console.putStrLn { data }
res.bodyAsString
_ <- Console.printLine { data }
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = program.exitCode.provideCustomLayer(env)
Expand Down
4 changes: 2 additions & 2 deletions docs/website/docs/v1.x/dsl/headers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ val responseHeaders: Task[Headers] = Client.request(url).map(_.headers)
// Pass headers to request
res <- Client.request(url, headers)
// List all response headers
_ <- console.putStrLn(res.headers.toList.mkString("\n"))
_ <- Console.printLine(res.headers.toList.mkString("\n"))
data <-
// Check if response contains a specified header with a specified value.
if (res.hasHeader(HeaderNames.contentType, HeaderValues.applicationJson))
res.getBodyAsString
else
res.getBodyAsString
_ <- console.putStrLn { data }
_ <- Console.printLine { data }
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = program.exitCode.provideCustomLayer(env)
Expand Down
5 changes: 4 additions & 1 deletion docs/website/docs/v1.x/dsl/http.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "2"
---
# Http

`Http` is a functional domain that models HTTP applications. It’s polymorphic on input and output type.
Expand Down Expand Up @@ -317,7 +320,7 @@ Patches the response produced by the HTTP application using a `Patch`.
val a: HttpApp[Any, Nothing] = Http.collect[Request] {
case Method.GET -> !! / "text" => Response.text("Hello World!")
}
val app: Http[Any, Throwable, Request, String] = a.getBodyAsString
val app: Http[Any, Throwable, Request, String] = a.bodyAsString
```

## Converting an `Http` to `HttpApp`
Expand Down
3 changes: 3 additions & 0 deletions docs/website/docs/v1.x/dsl/httpdata.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "5"
---
# HttpData
`HttpData` is a domain to model content for `Request`, `Response` and `ClientRequest`. ZIO HTTP uses Netty at it's core and Netty handles content as `ByteBuf`. `HttpData` helps you decode and encode this content into simpler, easier to use data types while creating a Request or Response.
## Server-side usage of `HttpData`
Expand Down
3 changes: 3 additions & 0 deletions docs/website/docs/v1.x/dsl/middleware.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "8"
---
# Middleware

WIP
5 changes: 4 additions & 1 deletion docs/website/docs/v1.x/dsl/request.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "3"
---
# Request

**ZIO HTTP** `Request` is designed in the simplest way possible to decode HTTP Request into a ZIO HTTP request.
Expand Down Expand Up @@ -51,7 +54,7 @@ According to the request path, it will respond with the corresponding response:
```
- `getBodyAsString` to access the content of request as string
```scala
val app = Http.collectZIO[Request] { case req => req.getBodyAsString.as(Response.ok) }
val app = Http.collectZIO[Request] { case req => req.bodyAsString.as(Response.ok) }
```
- `getHeaders` to get all the headers in the Request
```scala
Expand Down
3 changes: 3 additions & 0 deletions docs/website/docs/v1.x/dsl/response.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "4"
---
# Response

**ZIO HTTP** `Response` is designed to encode HTTP Response.
Expand Down
7 changes: 5 additions & 2 deletions docs/website/docs/v1.x/dsl/server.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
sidebar_position: "1"
---
# Server

This section describes, ZIO HTTP Server and different configurations you can provide while creating the Server
Expand Down Expand Up @@ -28,7 +31,7 @@ This section describes, ZIO HTTP Server and different configurations you can pro
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
server.make
.use(start =>
console.putStrLn(s"Server started on port ${start.port}")
Console.printLine(s"Server started on port ${start.port}")
*> ZIO.never,
).provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(2))
.exitCode
Expand Down Expand Up @@ -81,7 +84,7 @@ object HelloWorldAdvanced extends App {
server.make
.use(_ =>
// Waiting for the server to start
console.putStrLn(s"Server started on port $PORT")
Console.printLine(s"Server started on port $PORT")

// Ensures the server doesn't die after printing
*> ZIO.never,
Expand Down
4 changes: 2 additions & 2 deletions docs/website/docs/v1.x/dsl/server/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This section describes, ZIO HTTP Server and different configurations you can pro
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
server.make
.use(start =>
console.putStrLn(s"Server started on port ${start.port}")
Console.printLine(s"Server started on port ${start.port}")
*> ZIO.never,
).provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(2))
.exitCode
Expand Down Expand Up @@ -81,7 +81,7 @@ object HelloWorldAdvanced extends App {
server.make
.use(_ =>
// Waiting for the server to start
console.putStrLn(s"Server started on port $PORT")
Console.printLine(s"Server started on port $PORT")

// Ensures the server doesn't die after printing
*> ZIO.never,
Expand Down
2 changes: 1 addition & 1 deletion docs/website/docs/v1.x/dsl/socket/_category_.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Socket",
"position": 10
"position": 9
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object HelloWorldAdvanced extends App {
server.make
.use(start =>
// Waiting for the server to start
console.putStrLn(s"Server started on port ${start.port}")
Console.printLine(s"Server started on port ${start.port}")

// Ensures the server doesn't die after printing
*> ZIO.never,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio._

import java.time.Clock

object Authentication extends App {
object Authentication extends ZIOAppDefault {
// Secret Authentication key
val SECRET_KEY = "secretKey"

Expand All @@ -33,7 +33,7 @@ object Authentication extends App {
def authenticate[R, E](fail: HttpApp[R, E], success: JwtClaim => HttpApp[R, E]): HttpApp[R, E] =
Http
.fromFunction[Request] {
_.getHeader("X-ACCESS-TOKEN")
_.header("X-ACCESS-TOKEN")
.flatMap(header => jwtDecode(header._2.toString))
.fold[HttpApp[R, E]](fail)(success)
}
Expand All @@ -56,8 +56,8 @@ object Authentication extends App {
val app: UHttpApp = login ++ authenticate(Http.forbidden("Not allowed!"), user)

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
override val run =
Server.start(8090, app)
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ object WebSocketAdvanced extends App {
.onOpen(open)

// Called after the connection is closed
.onClose(_ => console.putStrLn("Closed!").ignore)
.onClose(_ => Console.printLine("Closed!").ignore)

// Called whenever there is an error on the socket channel
.onError(_ => console.putStrLn("Error!").ignore)
.onError(_ => Console.printLine("Error!").ignore)

// Setup websocket decoder config
.withDecoder(decoder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ object SimpleClient extends App {

val program = for {
res <- Client.request(url, headers)
data <- res.getBodyAsString
_ <- console.putStrLn { data }
data <- res.bodyAsString
_ <- Console.printLine { data }
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ object HttpsClient extends App {

val program = for {
res <- Client.request(url, headers, sslOption)
data <- res.getBodyAsString
_ <- console.putStrLn { data }
data <- res.bodyAsString
_ <- Console.printLine { data }
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode]
Expand Down
2 changes: 1 addition & 1 deletion docs/website/docs/v1.x/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ val app = Http.collectZIO[Request] {
case req @ Method.GET -> !! / "fruits" / "a" =>
UIO(Response.text("URL:" + req.url.path.asString + " Headers: " + req.getHeaders))
case req @ Method.POST -> !! / "fruits" / "a" =>
req.getBodyAsString.map(Response.text(_))
req.bodyAsString.map(Response.text(_))
}
```

Expand Down
12 changes: 6 additions & 6 deletions docs/website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6653,9 +6653,9 @@ prism-react-renderer@^1.2.1:
integrity sha512-w23ch4f75V1Tnz8DajsYKvY5lF7H1+WvzvLUcF0paFxkTHSp42RS0H5CttdN2Q8RR3DRGZ9v5xD/h3n8C8kGmg==

prismjs@^1.23.0:
version "1.26.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.26.0.tgz#16881b594828bb6b45296083a8cbab46b0accd47"
integrity sha512-HUoH9C5Z3jKkl3UunCyiD5jwk0+Hz0fIgQ2nbwU2Oo/ceuTAQAg+pPVnfdt2TJWRVLcxKh9iuoYDUSc8clb5UQ==
version "1.27.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.27.0.tgz#bb6ee3138a0b438a3653dd4d6ce0cc6510a45057"
integrity sha512-t13BGPUlFDR7wRB5kQDG4jjl7XeuH6jbJGt11JHPL96qwsEHNX2+68tFXqc1/k+/jALsbSWJKUOT/hcYAZ5LkA==

process-nextick-args@~2.0.0:
version "2.0.1"
Expand Down Expand Up @@ -8415,9 +8415,9 @@ url-parse-lax@^3.0.0:
prepend-http "^2.0.0"

url-parse@^1.4.3, url-parse@^1.5.3:
version "1.5.7"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.7.tgz#00780f60dbdae90181f51ed85fb24109422c932a"
integrity sha512-HxWkieX+STA38EDk7CE9MEryFeHCKzgagxlGvsdS7WBImq9Mk+PGwiT56w82WI3aicwJA8REp42Cxo98c8FZMA==
version "1.5.10"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.10.tgz#9d3c2f736c1d75dd3bd2be507dcc111f1e2ea9c1"
integrity sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==
dependencies:
querystringify "^2.1.1"
requires-port "^1.0.0"
Expand Down
60 changes: 0 additions & 60 deletions example/src/main/scala/example/Authentication.scala

This file was deleted.

28 changes: 28 additions & 0 deletions example/src/main/scala/example/AuthenticationClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package example

import zhttp.http.Headers
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zio.{ExitCode, URIO, ZIOAppDefault}

object AuthenticationClient extends ZIOAppDefault {

/**
* This example is trying to access a protected route in AuthenticationServer
* by first making a login request to obtain a jwt token and use it to access
* a protected route. Run AuthenticationServer before running this example.
*/
val url = "http://localhost:8090"
val env = ChannelFactory.auto ++ EventLoopGroup.auto()

val program = for {
// Making a login request to obtain the jwt token. In this example the password should be the reverse string of username.
token <- Client.request(s"${url}/login/username/emanresu").flatMap(_.bodyAsString)
// Once the jwt token is procured, adding it as a Barer token in Authorization header while accessing a protected route.
response <- Client.request(s"${url}/user/userName/greet", headers = Headers.bearerAuthorizationHeader(token))
body <- response.bodyAsString
_ <- zio.Console.printLine(body)
} yield ()

val run: URIO[zio.ZEnv, ExitCode] = program.exitCode.provideCustomLayer(env)

}
Loading

0 comments on commit 909d89c

Please sign in to comment.