Skip to content

Commit 39ac676

Browse files
committed
tweak-readme
1 parent abdaa16 commit 39ac676

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

readme.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ intuitive, and straightforward to use.
1919
- [Cookies](#cookies)
2020
- [Redirects](#redirects)
2121
- [Sessions](#sessions)
22+
- [Why Requests-Scala?](#why-requests-scala)
2223

2324
## Getting Started
2425

@@ -478,6 +479,121 @@ want, but still fall short: both still use a pattern of fluent builders that to
478479
me doesn't fit how I think when making a HTTP request. I just want to call one
479480
function to make a HTTP request, and get back my HTTP response.
480481
482+
Most people will never reach the scale that asynchrony matters, and most of
483+
those who do reach that scale will only need it in a small number of specialized
484+
places, not everywhere.
485+
486+
Compare the getting-started code necessary for Requests-Scala against some other
487+
common Scala HTTP clients:
488+
```scala
489+
// Requests-Scala
490+
val r = requests.get(
491+
"https://api.github.com/search/repositories",
492+
params = Map("q" -> "http language:scala", "sort" -> None)
493+
)
494+
495+
r.text
496+
// {"login":"lihaoyi","id":934140,"node_id":"MDQ6VXNlcjkzNDE0MA==",...
497+
```
498+
```scala
499+
// Akka-Http
500+
import akka.actor.ActorSystem
501+
import akka.http.scaladsl.Http
502+
import akka.http.scaladsl.model._
503+
import akka.stream.ActorMaterializer
504+
505+
import scala.concurrent.Future
506+
import scala.util.{ Failure, Success }
507+
508+
implicit val system = ActorSystem()
509+
implicit val materializer = ActorMaterializer()
510+
// needed for the future flatMap/onComplete in the end
511+
implicit val executionContext = system.dispatcher
512+
513+
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))
514+
515+
responseFuture
516+
.onComplete {
517+
case Success(res) => println(res)
518+
case Failure(_) => sys.error("something wrong")
519+
}
520+
521+
```
522+
523+
```scala
524+
// Play-WS
525+
526+
import akka.actor.ActorSystem
527+
import akka.stream.ActorMaterializer
528+
import play.api.libs.ws._
529+
import play.api.libs.ws.ahc._
530+
531+
import scala.concurrent.Future
532+
533+
import DefaultBodyReadables._
534+
import scala.concurrent.ExecutionContext.Implicits._
535+
536+
// Create Akka system for thread and streaming management
537+
implicit val system = ActorSystem()
538+
implicit val materializer = ActorMaterializer()
539+
540+
// Create the standalone WS client
541+
// no argument defaults to a AhcWSClientConfig created from
542+
// "AhcWSClientConfigFactory.forConfig(ConfigFactory.load, this.getClass.getClassLoader)"
543+
val wsClient = StandaloneAhcWSClient()
544+
545+
wsClient.url("http://www.google.com").get()
546+
.map { response ⇒
547+
val statusText: String = response.statusText
548+
val body = response.body[String]
549+
println(s"Got a response $statusText")
550+
}.
551+
andThen { case _ => wsClient.close() }
552+
andThen { case _ => system.terminate() }
553+
554+
```
555+
```scala
556+
// Http4s
557+
import org.http4s.client.dsl.io._
558+
import org.http4s.headers._
559+
import org.http4s.MediaType
560+
561+
val request = GET(
562+
Uri.uri("https://my-lovely-api.com/"),
563+
Authorization(Credentials.Token(AuthScheme.Bearer, "open sesame")),
564+
Accept(MediaType.application.json)
565+
)
566+
567+
httpClient.expect[String](request)
568+
```
569+
```scala
570+
// sttp
571+
import com.softwaremill.sttp._
572+
573+
val sort: Option[String] = None
574+
val query = "http language:scala"
575+
576+
// the `query` parameter is automatically url-encoded
577+
// `sort` is removed, as the value is not defined
578+
val request = sttp.get(uri"https://api.github.com/search/repositories?q=$query&sort=$sort")
579+
580+
implicit val backend = HttpURLConnectionBackend()
581+
val response = request.send()
582+
583+
// response.unsafeBody: by default read into a String
584+
println(response.unsafeBody)
585+
```
586+
```dispatch
587+
import dispatch._, Defaults._
588+
val svc = url("http://api.hostip.info/country.php")
589+
val country = Http.default(svc OK as.String)
590+
```
591+
592+
The existing clients require a complex mix of imports, implicits, operators, and
593+
DSLs. The goal of Requests-Scala is to do away with all of that: your HTTP
594+
request is just a function call that takes parameters; that is all you need to
595+
know.
596+
481597
As it turns out, Kenneth Reitz's Requests is
482598
[not a lot of code](https://github.com/requests/requests/tree/master/requests).
483599
Most of the heavy lifting is done in other libraries, and his library is a just

0 commit comments

Comments
 (0)