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

Docs: Use :silent on import statements #2337

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/src/main/tut/auth.md
Expand Up @@ -11,7 +11,7 @@ For this section, remember that, like mentioned in the [service] section, a serv

Lets start by defining all the imports we will need in the examples below:

```tut:book:silent
```tut:silent
import cats._, cats.effect._, cats.implicits._, cats.data._
import org.http4s._
import org.http4s.dsl.io._
Expand All @@ -25,7 +25,7 @@ such object, and is the equivalent to `(User, Request[F])`. _http4s_ provides yo
but you have to provide your own _user_, or _authInfo_ representation. For our purposes here we will
use the following definition:

```tut:book:silent
```tut:silent
case class User(id: Long, name: String)
```

Expand All @@ -38,7 +38,7 @@ With that we can represent a service that requires authentication, but to actual
to define how to extract the authentication information from the request. For that, we need a function
with the following signature: `Request[F] => OptionT[F, User]`. Here is an example of how to define it:

```tut:book:silent
```tut:silent
val authUser: Kleisli[OptionT[IO, ?], Request[IO], User] =
Kleisli(_ => OptionT.liftF(IO(???)))
```
Expand All @@ -50,7 +50,7 @@ IO operations.
Now we need a middleware that can bridge a "normal" service into an `AuthedService`, which is quite easy to
get using our function defined above. We use `AuthMiddleware` for that:

```tut:book:silent
```tut:silent
val middleware: AuthMiddleware[IO, User] =
AuthMiddleware(authUser)
```
Expand All @@ -65,7 +65,7 @@ wish to always return 401, use `AuthMiddleware.noSpider` and specify the `onAuth
Finally, we can create our `AuthedService`, and wrap it with our authentication middleware, getting the
final `HttpRoutes` to be exposed. Notice that we now have access to the user object in the service implementation:

```tut:book:silent
```tut:silent
val authedService: AuthedService[User, IO] =
AuthedService {
case GET -> Root / "welcome" as user => Ok(s"Welcome, ${user.name}")
Expand All @@ -87,7 +87,7 @@ To allow for failure, the `authUser` function has to be adjusted to a `Request[F
=> F[Either[String,User]]`. So we'll need to handle that possibility. For advanced
error handling, we recommend an error [ADT] instead of a `String`.

```tut:book:silent
```tut:silent
val authUser: Kleisli[IO, Request[IO], Either[String,User]] = Kleisli(_ => IO(???))

val onFailure: AuthedService[String, IO] = Kleisli(req => OptionT.liftF(Forbidden(req.authInfo)))
Expand Down Expand Up @@ -118,7 +118,7 @@ use multiple application instances.

The message is simply the user id.

```tut:book:silent
```tut:silent
import org.reactormonk.{CryptoBits, PrivateKey}
import java.time._

Expand All @@ -141,7 +141,7 @@ val logIn: Kleisli[IO, Request[IO], Response[IO]] = Kleisli({ request =>

Now that the cookie is set, we can retrieve it again in the `authUser`.

```tut:book:silent
```tut:silent
def retrieveUser: Kleisli[IO, Long, User] = Kleisli(id => IO(???))
val authUser: Kleisli[IO, Request[IO], Either[String,User]] = Kleisli({ request =>
val message = for {
Expand All @@ -160,7 +160,7 @@ There is no inherent way to set the Authorization header, send the token in any
way that your [SPA] understands. Retrieve the header value in the `authUser`
function.

```tut:book:silent
```tut:silent
import org.http4s.util.string._
import org.http4s.headers.Authorization

Expand Down
16 changes: 8 additions & 8 deletions docs/src/main/tut/client.md
Expand Up @@ -26,7 +26,7 @@ libraryDependencies ++= Seq(

Then we create the [service] again so tut picks it up:
>
```tut:book:silent
```tut:silent
import cats.effect._
import org.http4s._
import org.http4s.dsl.io._
Expand All @@ -38,7 +38,7 @@ Blaze needs a [[`ConcurrentEffect`]] instance, which is derived from
[[`ContextShift`]]. The following lines are not necessary if you are
in an [[`IOApp`]]:

```tut:book:silent
```tut:silent
import scala.concurrent.ExecutionContext.global
implicit val cs: ContextShift[IO] = IO.contextShift(global)
implicit val timer: Timer[IO] = IO.timer(global)
Expand Down Expand Up @@ -68,7 +68,7 @@ val fiber = server.use(_ => IO.never).start.unsafeRunSync()
A good default choice is the `BlazeClientBuilder`. The
`BlazeClientBuilder` maintains a connection pool and speaks HTTP 1.x.

```tut:book:silent
```tut:silent
import org.http4s.client.blaze._
import org.http4s.client._
import scala.concurrent.ExecutionContext.Implicits.global
Expand All @@ -92,7 +92,7 @@ interface!
It uses blocking IO and is less suited for production, but it is
highly useful in a REPL:

```tut:book:silent
```tut:silent
import scala.concurrent.ExecutionContext
import java.util.concurrent._

Expand Down Expand Up @@ -123,7 +123,7 @@ side effects to the end.
Let's describe how we're going to greet a collection of people in
parallel:

```tut:book:silent
```tut:silent
import cats._, cats.effect._, cats.implicits._
import org.http4s.Uri
import scala.concurrent.ExecutionContext.Implicits.global
Expand Down Expand Up @@ -231,7 +231,7 @@ libraryDependencies ++= Seq(
We can create a middleware that registers metrics prefixed with a
provided prefix like this.

```tut:book:silent
```tut:silent
import org.http4s.client.middleware.Metrics
import org.http4s.metrics.dropwizard.Dropwizard
import com.codahale.metrics.SharedMetricRegistries
Expand Down Expand Up @@ -261,7 +261,7 @@ libraryDependencies ++= Seq(
We can create a middleware that registers metrics prefixed with a
provided prefix like this.

```tut:book:silent
```tut:silent
import org.http4s.client.middleware.Metrics
import org.http4s.metrics.prometheus.Prometheus
import io.prometheus.client.CollectorRegistry
Expand Down Expand Up @@ -291,7 +291,7 @@ httpClient.expect[String](Uri.uri("https://google.com/"))
If you need to do something more complicated like setting request headers, you
can build up a request object and pass that to `expect`:

```tut:book:silent
```tut:silent
import org.http4s.client.dsl.io._
import org.http4s.headers._
import org.http4s.MediaType
Expand Down
9 changes: 7 additions & 2 deletions docs/src/main/tut/cors.md
Expand Up @@ -40,8 +40,11 @@ service.orNotFound(request).unsafeRunSync

Now we can wrap the service in the `CORS` middleware.

```tut:book
```tut:silent
import org.http4s.server.middleware._
```

```tut:book
val corsService = CORS(service)

corsService.orNotFound(request).unsafeRunSync
Expand Down Expand Up @@ -78,9 +81,11 @@ val duckPost = Request[IO](Method.POST, Uri.uri("/"), headers = Headers(Header("
Now, we'll create a configuration that limits the allowed methods to `GET`
and `POST`, pass that to the `CORS` middleware, and try it out on our requests.

```tut:book
```tut:silent
import scala.concurrent.duration._
```

```tut:book
val methodConfig = CORSConfig(
anyOrigin = true,
anyMethod = false,
Expand Down
22 changes: 15 additions & 7 deletions docs/src/main/tut/dsl.md
Expand Up @@ -35,7 +35,7 @@ $ sbt console

We'll need the following imports to get started:

```tut:book
```tut:silent
import cats.effect._
import org.http4s._, org.http4s.dsl.io._, org.http4s.implicits._
```
Expand Down Expand Up @@ -133,7 +133,7 @@ Ok("Ok response.").unsafeRunSync.headers

Extra headers can be added using `putHeaders`, for example to specify cache policies:

```tut:book
```tut:silent
import org.http4s.headers.`Cache-Control`
import org.http4s.CacheDirective.`no-cache`
import cats.data.NonEmptyList
Expand Down Expand Up @@ -208,7 +208,7 @@ NoContent("does not compile")
While http4s prefers `F[_]: Effect`, you may be working with libraries that
use standard library `Future`s. Some relevant imports:

```tut:book
```tut:silent
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
```
Expand Down Expand Up @@ -253,11 +253,13 @@ An intro to `Stream` is out of scope, but we can glimpse the
power here. This stream emits the elapsed time every 100 milliseconds
for one second:

```tut:book
```tut:silent
import fs2.Stream
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
```

```tut:book
// Provided by `cats.effect.IOApp`
implicit val timer: Timer[IO] = IO.timer(global)

Expand Down Expand Up @@ -377,11 +379,13 @@ If you want to extract a variable of type `T`, you can provide a custom extracto
object which implements `def unapply(str: String): Option[T]`, similar to the way
in which `IntVar` does it.

```tut:book
```tut:silent
import java.time.LocalDate
import scala.util.Try
import org.http4s.client.dsl.io._
```

```tut:book
object LocalDateVar {
def unapply(str: String): Option[LocalDate] = {
if (!str.isEmpty)
Expand Down Expand Up @@ -412,10 +416,12 @@ return optional or validated parameter values.
In the example below we're finding query params named `country` and `year` and
then parsing them as a `String` and `java.time.Year`.

```tut:book
```tut:silent
import java.time.Year
import cats.data.ValidatedNel
```

```tut:book
object CountryQueryParamMatcher extends QueryParamDecoderMatcher[String]("country")

implicit val yearQueryParamDecoder: QueryParamDecoder[Year] =
Expand All @@ -435,10 +441,12 @@ val averageTemperatureService = HttpRoutes.of[IO] {

To accept a optional query parameter a `OptionalQueryParamDecoderMatcher` can be used.

```tut:book
```tut:silent
import java.time.Year
import org.http4s.client.dsl.io._
```

```tut:book
implicit val yearQueryParamDecoder: QueryParamDecoder[Year] =
QueryParamDecoder[Int].map(Year.of)

Expand Down
4 changes: 3 additions & 1 deletion docs/src/main/tut/entity.md
Expand Up @@ -44,7 +44,7 @@ runtime errors.
Decoders' content types are used when chaining decoders with `orElse` in order to
determine which of the chained decoders are to be used.

```tut
```tut:silent
import org.http4s._
import org.http4s.headers.`Content-Type`
import org.http4s.dsl.io._
Expand All @@ -53,7 +53,9 @@ import cats._, cats.effect._, cats.implicits._, cats.data._
sealed trait Resp
case class Audio(body: String) extends Resp
case class Video(body: String) extends Resp
```

```tut:book
val response = Ok("").map(_.withContentType(`Content-Type`(MediaType.audio.ogg)))
val audioDec = EntityDecoder.decodeBy(MediaType.audio.ogg) { msg: Message[IO] =>
EitherT {
Expand Down
9 changes: 7 additions & 2 deletions docs/src/main/tut/hsts.md
Expand Up @@ -43,8 +43,11 @@ response.headers

If we were to wrap this on the `HSTS` middleware.

```tut:book
```tut:silent
import org.http4s.server.middleware._
```

```tut:book
val hstsService = HSTS(service)

// Do not call 'unsafeRunSync' in your code
Expand All @@ -65,10 +68,12 @@ should be done over `https` and it will contain the `includeSubDomains` directiv

If you want to `preload` or change other default values you can pass a custom header, e.g.

```tut:book
```tut:silent
import org.http4s.headers._
import scala.concurrent.duration._
```

```tut:book
val hstsHeader = `Strict-Transport-Security`.unsafeFromDuration(30.days, includeSubDomains = true, preload = true)
val hstsService = HSTS(service, hstsHeader)

Expand Down
20 changes: 13 additions & 7 deletions docs/src/main/tut/json.md
Expand Up @@ -63,7 +63,7 @@ bases its codecs on runtime reflection.

Let's create a function to produce a simple JSON greeting with circe. First, the imports:

```tut:book:silent
```tut:silent
import cats.effect._
import io.circe._
import io.circe.literal._
Expand Down Expand Up @@ -91,19 +91,23 @@ To encode a Scala value of type `A` into an entity, we need an
`org.http4s.circe` object, which gives us exactly this for an
`io.circe.Json` value:

```tut:book
```tut:silent
import org.http4s.circe._
```

```tut:book
Ok(greeting).unsafeRunSync
```

The same `EntityEncoder[Json]` we use on server responses is also
useful on client requests:

```tut:book
```tut:silent
import org.http4s.client._
import org.http4s.client.dsl.io._
```

```tut:book
POST(json"""{"name": "Alice"}""", Uri.uri("/hello")).unsafeRunSync
```

Expand Down Expand Up @@ -148,9 +152,11 @@ That was easy, but gets tedious for applications dealing in lots of
types. Fortunately, circe can automatically derive an encoder for us,
using the field names of the case class as key names in a JSON object:

```tut:book
```tut:silent
import io.circe.generic.auto._
```

```tut:book
User("Alice").asJson
```

Expand All @@ -164,7 +170,7 @@ POST(User("Bob").asJson, Uri.uri("/hello")).unsafeRunSync

If within some route we serve json only, we can use:

```tut:book
```tut:silent
{
import org.http4s.circe.CirceEntityEncoder._
}
Expand Down Expand Up @@ -209,7 +215,7 @@ POST("""{"name":"Bob"}""", Uri.uri("/hello")).flatMap(_.as[User]).unsafeRunSync
If we are always decoding from JSON to a typed model, we can use
the following import:

```tut:book
```tut:silent
import org.http4s.circe.CirceEntityDecoder._
```

Expand All @@ -222,7 +228,7 @@ from JSON (even if it is XML or plain text, for instance).
For more convenience there is import combining both encoding
and decoding derivation:

```tut:book
```tut:silent
import org.http4s.circe.CirceEntityCodec._
```

Expand Down