本文整理汇总了Scala中org.http4s.Request类的典型用法代码示例。如果您正苦于以下问题:Scala Request类的具体用法?Scala Request怎么用?Scala Request使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Request类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: StravaRoutes
//设置package包名称以及导入依赖的类
package com.mfglabs
package models
package external
import org.http4s.Http4s.uri
import org.http4s.{ Request, Method, Headers, Header }
import java.time.Instant
object StravaRoutes {
def activities(activitiesPerPage: Int)(token: Strava.Token)(after: Instant) =
Request(
Method.GET,
uri("https://www.strava.com/api/v3/activities")
.withQueryParam("per_page", activitiesPerPage)
.withQueryParam("after", after.getEpochSecond),
headers = Headers(Header("Authorization", s"Bearer ${token.value}"))
)
}
示例2: ServiceSpec
//设置package包名称以及导入依赖的类
package org.mdoc.rendering.service
import org.http4s.{ Request, Uri }
import org.http4s.dsl._
import org.http4s.headers.`Content-Type`
import org.http4s.util.CaseInsensitiveString
import org.mdoc.common.model.{ Document, Format }
import org.mdoc.common.model.jvm.FormatOps
import org.scalacheck.Prop._
import org.scalacheck.Properties
import scodec.bits.ByteVector
object ServiceSpec extends Properties("Service") {
property("docToResponse sets ContentType") = secure {
Format.values.forall { format =>
val doc = Document(format, ByteVector.view("".getBytes))
val response = Service.docToResponse(doc).run
response.headers.get(CaseInsensitiveString("content-type"))
.contains(`Content-Type`(FormatOps.toMediaType(format)))
}
}
property("showRequest") = forAll { (s: String) =>
Service.showRequest(Request(uri = Uri(path = s))) ?= s"GET $s"
}
property("route: /") = secure {
Service.route.run(Request()).run.status ?= NotFound
}
property("route: /render failure") = secure {
val req: Request = Request(uri = Uri(path = "/render"), method = POST)
val body = Service.route.run(req).run.body.runLast.run
.flatMap(_.decodeAscii.right.toOption).getOrElse("")
body ?= "Invalid JSON"
}
property("route: /render/pdf/test.pdf") = secure {
val req = Request(uri = Uri(path = "/render/pdf/test.pdf"))
Service.route.run(req).run.status ?= BadRequest
}
property("route: /version") = secure {
Service.route.run(Request(uri = Uri(path = "/version"))).run.status ?= Ok
}
}
示例3: TestClient
//设置package包名称以及导入依赖的类
package com.github.hgiddens.ausms
package telstra
import JsonHelpers._
import io.circe.Json
import io.circe.syntax._
import org.http4s.{ Charset, MediaType, OAuth2BearerToken, Request, Response }
import org.http4s.client.{ Client, DisposableResponse }
import org.http4s.dsl._
import org.http4s.headers.{ Authorization, `Content-Type` }
import scalaz.Kleisli
import scalaz.concurrent.Task
private[telstra] object TestClient {
val key = "key"
val secret = "secret"
val accessToken = "g85MslKJGuDIPhOklSgNLbWFPu5g"
val messageId = "48E48E5EB8125001D6154CADFA19DA48"
private val jsonContentType = `Content-Type`(MediaType.`application/json`, Some(Charset.`UTF-8`))
private def perform(req: Request): Task[Response] =
req match {
case GET -> Root / "v1" / "oauth" / "token" =>
Ok().withBody(Json.obj("access_token" -> accessToken.asJson, "expires_in" -> "3599".asJson))
case POST -> Root / "v1" / "sms" / "messages" =>
(req.headers, req.headers) match {
case (Authorization(Authorization(OAuth2BearerToken(`accessToken`))), `Content-Type`(`jsonContentType`)) =>
Accepted().withBody(Json.obj("messageId" -> messageId.asJson))
}
case GET -> Root / "v1" / "sms" / "messages" / `messageId` =>
req.headers match {
case Authorization(Authorization(OAuth2BearerToken(`accessToken`))) =>
Ok().withBody(Json.obj(
"to" -> "0400000000".asJson,
"receivedTimestamp" -> "2015-02-05T14:10:14+11:00".asJson,
"sentTimestamp" -> "2015-02-05T14:10:12+11:00".asJson,
"status" -> "DELIVRD".asJson
))
}
}
def client: Client =
Client(Kleisli(perform _).map(new DisposableResponse(_, Task.now(()))), Task.now(()))
}
示例4: ProjectController
//设置package包名称以及导入依赖的类
package fairshare.backend.project
import java.util.UUID
import argonaut.Argonaut._
import argonaut.Json
import org.http4s.argonaut._
import org.http4s.dsl._
import org.http4s.{ HttpService, Request, Response }
import fairshare.backend.util.RequestValidation._
import fairshare.backend.util.JsonHelpers._
import scalaz.concurrent.Task
class ProjectController(commandHandler: ProjectCommandHandler, queryHandler: ProjectQueryHandler) {
def create(request: Request): Task[Response] = bindFromRequest(
request,
"name" -> nonEmptyString,
"motto" -> nonEmptyString
) { (name, motto) =>
val id = ProjectId(UUID.randomUUID())
commandHandler.create(id, name, motto).flatMap(
_.fold(
error => InternalServerError(error),
fact => Ok(Json("id" := fact.subject))
)
)
}.fold(
errs => BadRequest(errs.asJson),
success => success
)
def index(request: Request): Task[Response] = {
val projects = queryHandler.getAll
Ok(projects.asJson)
}
}
object ProjectController {
def service(controller: ProjectController): HttpService = HttpService {
case req @ GET -> Root / "index" => controller.index(req)
case req @ POST -> Root / "create" => controller.create(req)
}
}
示例5: UserRoute
//设置package包名称以及导入依赖的类
package com.onur.moviedb.route
import argonaut.Argonaut._
import com.onur.moviedb.domain._
import com.onur.moviedb.route.error.{HttpError => HE}
import com.onur.moviedb.user.UserCreation.UserCreationValidation
import com.onur.moviedb.user.{UserModule, UserService}
import org.http4s.Request
import org.http4s.argonaut61.ArgonautInstances
import org.http4s.rho.{RhoService, _}
import scalaz.{-\/, Failure, NonEmptyList, Success, \/-}
object UserRoute {
lazy val route = new UserRoute(UserModule.userService).toService()
}
private class UserRoute private(userService: UserService) extends RhoService with ArgonautInstances {
GET |>> Ok(userService.getUsers.map(_.asJson))
GET / pathVar[UserId] |>> { (uid: UserId) =>
Ok(userService.getUserById(uid).map(_.asJson))
}
POST ^ jsonOf[UserCreationValidation] |>> { (r: Request, input: UserCreationValidation) =>
input match {
case Success(user) =>
userService.createUser(user).flatMap {
case \/-(res) => Created(res.asJson)
case -\/(err) => HE.s409("Email is already registered", NonEmptyList(err))
}
case Failure(errs) => HE.s400("Invalid account details", errs)
}
}
}
示例6: ModernLibsService
//设置package包名称以及导入依赖的类
package org.modernlibs
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s.circe.{jsonEncoder, jsonOf}
import org.http4s.dsl._
import org.http4s.{HttpService, Request}
class ModernLibsService(db: DB) {
val modernLibsService = HttpService {
case GET -> Root / "ml" / name => getPerson(name)
case GET -> Root / "ml" => getPersons
case [email protected] -> Root / "ml" => newPerson(r)
}
private def getPersons = {
for {
dbResult <- db.list()
result <- dbResult.map(r => Ok(r.asJson)) | InternalServerError()
} yield result
}
private def getPerson(name: String) = {
for {
dbResult <- db.find(name)
result <- dbResult.map(_.fold(NotFound())(r => Ok(r.asJson))) | InternalServerError()
} yield result
}
private def newPerson(r: Request) = {
for {
person <- r.as(jsonOf[Person])
dbResponse <- db.save(person)
response <- dbResponse.map {
case 1 => Created()
case _ => Conflict()
} | InternalServerError()
} yield response
}
}
示例7: Sources
//设置package包名称以及导入依赖的类
package com.joescii.scalaz.stream
import org.http4s.{Method, Request, Status, Uri}
import org.http4s.client.blaze.SimpleHttp1Client
import scalaz.{-\/, \/-}
import scalaz.concurrent.Task
import scalaz.stream.io
import scalaz.stream.Process
object Sources {
lazy val fromFileSystem:Process[Task, String] = io.linesR("./logs/AWSLogs.log")
private [this] val c = SimpleHttp1Client()
private [this] val uri = Uri.uri("https://raw.githubusercontent.com/ozantunca/elb-log-analyzer/master/logs/AWSLogs.log")
lazy val fromTehWebz:Process[Task, String] = {
val t = c.fetch(Request(Method.GET, uri)){ res => res.status match {
case Status.Ok => res
.bodyAsText
.runLog
}}
Process.await(t)(Process.emitAll)
}
}