当前位置: 首页>>代码示例>>Scala>>正文


Scala Request类代码示例

本文整理汇总了Scala中com.twitter.finagle.http.Request的典型用法代码示例。如果您正苦于以下问题:Scala Request类的具体用法?Scala Request怎么用?Scala Request使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Request类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: RequestNormalization

//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter

import java.nio.charset.StandardCharsets
import java.util.UUID
import java.net.URLEncoder
import com.twitter.finagle.http.Request
import shapeless.tag._
import cats.implicits._
import com.lookout.ratelimitingfilter.models._


object RequestNormalization {
  def apply(
    serviceLookup: Request => Option[String @@ ServiceName],
    claimLookup: Request => Option[(UUID @@ EntClaim, UUID @@ SubClaim)],
    request: Request
  ): List[String] = {
    val method = request.method
    val path = encodePath(request.path)
    val serviceBuckets: Option[List[String]] = serviceLookup(request).map {
      serviceName => s"$method::$serviceName" :: s"$method::$path::$serviceName" :: Nil
    }
    val idBuckets: Option[List[String]] = claimLookup(request).map {
      case (entUuid, subUuid) =>
        s"$method::$path::$entUuid" :: s"$method::$path::$subUuid" :: Nil
    }

    (serviceBuckets |+| idBuckets).getOrElse(Nil)
  }

  def encodePath(path: String): String =
    URLEncoder.encode(path, StandardCharsets.UTF_8.toString).toLowerCase
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:34,代码来源:RequestNormalization.scala

示例2: beforeAll

//设置package包名称以及导入依赖的类
package placeholder.base

import java.net.InetSocketAddress

import com.twitter.finagle
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Response}
import com.twitter.util.Future
import org.scalatest._
import placeholder.Main

import scala.concurrent.Await
import scala.concurrent.duration._

trait IntegrationTestBase extends FeatureSpec with GivenWhenThen with Matchers with BeforeAndAfterAll {
  val server = Main

  var serverAddress: InetSocketAddress = _
  var client: Service[Request, Response] = _
  var requestHost: String = _

  server.main(Array())

  override def beforeAll(): Unit = {
    serverAddress = Await.result(server.getServerAddress, 10.seconds)
    requestHost = s"localhost:${serverAddress.getPort.toString}"
    client = finagle.Http.newService(requestHost)
  }

  def performRequest(request: Request): Future[Response] = {
    request.host = requestHost
    client(request)
  }
} 
开发者ID:scala-academy,项目名称:skeleton-project,代码行数:35,代码来源:IntegrationTestBase.scala

示例3: RequestLoggingFilter

//设置package包名称以及导入依赖的类
package app.filter

import com.twitter.finagle.filter.LogFormatter
import com.twitter.finagle.http.Status.InternalServerError
import com.twitter.finagle.http.filter.CommonLogFormatter
import com.twitter.finagle.http.{ Request, Response }
import com.twitter.finagle.{ Service, SimpleFilter }
import com.twitter.logging.Logger
import com.twitter.util._


abstract class RequestLoggingFilter[REQ <: Request](val formatter: LogFormatter[REQ, Response])
  extends SimpleFilter[REQ, Response] {

  private val log = Logger.get(getClass)

  final def apply(request: REQ, service: Service[REQ, Response]): Future[Response] = {
    //log.info(s">>> RAW REQUEST: '${request.getContentString()}'")
    val elapsed = Stopwatch.start()
    val future = service(request)
    future.respond {
      case Return(response) => logSuccess(elapsed(), request, response)
      case Throw(error)     => logException(elapsed(), request, error)
    }
    future
  }

  final def logSuccess(replyTime: Duration, request: REQ, response: Response): Unit = {
    val line = formatter.format(request, response, replyTime)
    log.info(line)
  }

  final def logException(duration: Duration, request: REQ, error: Throwable): Unit = {
    val response = Response(request.version, InternalServerError)
    val line = formatter.format(request, response, duration)
    log.info(line)
  }
}

object RequestLoggingFilter extends RequestLoggingFilter[Request](new CommonLogFormatter) 
开发者ID:PScopelliti,项目名称:ProjectTracker,代码行数:41,代码来源:RequestLoggingFilter.scala

示例4: apiErrorHandler

//设置package包名称以及导入依赖的类
package app.v1.handler

import com.twitter.finagle.http.{ Request, Response, Status, Version }
import com.twitter.logging.Logger
import com.twitter.util.Future
import io.finch.Error.{ NotParsed, NotPresent, NotValid }
import io.finch._

trait ErrorHandler {

  private val log = Logger.get(getClass)

  def apiErrorHandler: PartialFunction[Throwable, Output[Nothing]] = {
    case e: NotPresent => BadRequest(e)
    case e: NotParsed  => BadRequest(e)
    case e: NotValid   => BadRequest(e)
    case e: Exception  => InternalServerError(e)
  }

  def topLevelErrorHandler[REQUEST <: Request](request: REQUEST, encoder: Encode[Throwable]): PartialFunction[Throwable, Future[Response]] = {
    case t: Throwable => unhandledException(request, t, encoder)
  }

  private def unhandledException[REQUEST <: Request](request: REQUEST, t: Throwable, encoder: Encode[Throwable]): Future[Response] = {
    try {
      log.info(s"Unhandled exception on URI ${request.uri} with message $t")
      Future.value(Response(Version.Http11, Status.InternalServerError))
    } catch {
      case e: Throwable => {
        log.error(s"Unable to log unhandled exception: $e")
        throw e
      }
    }
  }
} 
开发者ID:PScopelliti,项目名称:ProjectTracker,代码行数:36,代码来源:ErrorHandler.scala

示例5: HandleRefusedByRateLimiterFilter

//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter

import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Failure, Service, SimpleFilter, RefusedByRateLimiter}
import com.twitter.util.Future
import com.twitter.logging.Logger


case class HandleRefusedByRateLimiterFilter() extends SimpleFilter[Request, Response] {
  val LOG = Logger.get(getClass)

  def apply(request: Request, service: Service[Request, Response]): Future[Response] = Future {
    service(request) handle {
      case _: RefusedByRateLimiter => {
        val rateLimitedMessage = s"Request is rate limited: ${request.encodeString()}"
        LOG.info(rateLimitedMessage)
        RefusedByRateLimiterError(rateLimitedMessage).toResponse
      }
    }
  }.flatMap(identity _)
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:22,代码来源:HandleRefusedByRateLimiterFilter.scala

示例6: LookoutRateLimitingStrategy

//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter

import java.util.UUID
import com.twitter.finagle.http.{Request}
import com.twitter.util.{Future}
import com.twitter.logging.Logger
import shapeless.tag._
import com.lookout.ratelimitingfilter.models._


case class LookoutRateLimitingStrategy(
  serviceLookup: Request => Option[String @@ ServiceName],
  claimLookup: Request => Option[(UUID @@ EntClaim, UUID @@ SubClaim)],
  rulesLookup: () => List[RateLimitRule],
  processRule: RateLimitRule => Boolean
) {
  val LOG = Logger.get(getClass)

  def apply(request: Request): Future[Boolean] = {
    val normalizedRequests = RequestNormalization(serviceLookup, claimLookup, request)
    val matchedRules = RateLimitRuleMatcher(rulesLookup, normalizedRequests)
    val underThreshold = try {
      matchedRules.map(processRule).reduce(_ && _)
    } catch {
      case e: RedisError => {
        LOG.info(s"Redis error. Rate limiting is switched off. ${e.getMessage}")
        true
      }
    }
    Future.value(underThreshold)
  }
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:33,代码来源:LookoutRateLimitingStrategy.scala

示例7: RefusedByRateLimiterErrorSpec

//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter

import com.twitter.finagle.http.{Request, Status}
import org.specs2.{Specification, ScalaCheck}
import io.circe.syntax._
import io.circe.jawn._
import com.lookout.ratelimitingfilter.models._

class RefusedByRateLimiterErrorSpec extends Specification with ScalaCheck with Arbitraries {
  def is = s2"""
    RefusedByRateLimiterError object
      it should not lose data on roundtrips to JSON $dataIntegrity
      it should contain a `message` field           $messageField
      it should create a response with 429 status   $statusCode
  """

  def fields(error: RefusedByRateLimiterError): Seq[String] =
    error.asJson.asObject.toList.flatMap(_.fields)

  def dataIntegrity = prop {
    (error: RefusedByRateLimiterError) => {
      (decode[RefusedByRateLimiterError](error.asJson.noSpaces)) must_== Right(error)
    }
  }

  def messageField = prop {
    (error: RefusedByRateLimiterError) => {
      fields(error) must contain("message")
    }
  }

  def statusCode = prop {
    (error: RefusedByRateLimiterError) => {
      error.toResponse.status must_== Status.TooManyRequests
    }
  }
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:38,代码来源:RefusedByRateLimiterErrorSpec.scala

示例8: MockClient

//设置package包名称以及导入依赖的类
package featherbed

import java.net.URL

import com.twitter.finagle.{Filter, Http}
import com.twitter.finagle.http.{Request, Response}
import org.scalamock.matchers.Matcher
import org.scalamock.scalatest.MockFactory

package object fixture {
  private[fixture] class MockClient (
    baseUrl: URL,
    filter: Filter[Request, Response, Request, Response]
  ) extends Client(baseUrl) {
    override def clientTransform(c: Http.Client) = c.filtered(filter)
  }

  trait ClientTest { self: MockFactory =>
    class TransportRequestMatcher(f: Request => Unit) extends Matcher[Any] {
      override def canEqual(x: Any) = x match {
        case x: Request => true
        case _ => false
      }
      override def safeEquals(that: Any): Boolean = that match {
        case x: Request => f(x); true
        case _ => false
      }
    }

    def request(f: Request => Unit): TransportRequestMatcher = new TransportRequestMatcher(f)

    def mockClient(url: String, filter: Filter[Request, Response, Request, Response]): Client =
      new MockClient(new URL(url), filter)
  }
} 
开发者ID:finagle,项目名称:featherbed,代码行数:36,代码来源:package.scala

示例9: QwebmonController

//设置package包名称以及导入依赖的类
package com.criteo.qwebmon

import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller

class QwebmonController(drivers: Map[String, DbDriver]) extends Controller {

  get("/refresh/:target") { r: Request =>
    val targetName = r.getParam("target")
    drivers.get(targetName).map { driver =>
      val runningQueries = driver.runningQueries
      DbStatus(runningQueries, runningQueries.length)
    }.getOrElse(response.badRequest(s"invalid target supplied: $targetName"))
  }

  get("/") { r: Request =>
    response.ok.file("/index.html")
  }

}

case class DbStatus(runningQueries: Seq[RunningQuery], runningQueryCount: Int) 
开发者ID:jqcoffey,项目名称:qwebmon,代码行数:23,代码来源:QwebmonController.scala

示例10: HttpTips

//设置package包名称以及导入依赖的类
package com.bob.scalatour.twitters

import com.twitter.finagle.transport.Transport
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http.{Method, Response, Request}
import com.twitter.finagle.loadbalancer.{Balancers, LoadBalancerFactory}
import com.twitter.util.{Await => twitterAwait, Monitor}

object HttpTips {

  def main(args: Array[String]) {
    val balancer: LoadBalancerFactory = Balancers.heap()

    val monitor: Monitor = new Monitor {
      override def handle(exc: Throwable): Boolean = {
        // do sth with the exception
        true
      }
    }

    val client: Service[Request, Response] = Http.client
      .withMonitor(monitor)
      .withLoadBalancer(balancer)
      // It’s important to disable Fail Fast when only have one host in the replica set
      // because Finagle doesn’t have any other path to choose.
      .withSessionQualifier.noFailFast
      .configured(Transport.Options(noDelay = false, reuseAddr = false))
      .newService("172.16.40.68:8090,172.16.40.69:8090", "riskservice")

    val request = Request(Method.Get, "/calculator/users/together?userId=1234&date=2015-12-12&bbb=bbb")
    // here we should set the host header value even it is empty,otherwise it will nothing can obtain
    request.headerMap.add("host", "")
    (1 to 20).map {
      z => {
        println(s"now is the ${z} request to send")
        val response = client(request)
        twitterAwait.ready(response.map(x => println(x.contentString)))
      }
    }

    val url = Request.queryString("/calculator/users/together", Map("userId" -> "314", "date" -> "2015", "bbb" -> "bbb"))
    println(url) // url value will be: /calculator/users/together?userId=314&date=2015&bbb=bbb
    println("http invoke done")
  }
} 
开发者ID:bobxwang,项目名称:scalatour,代码行数:46,代码来源:HttpTips.scala

示例11: MExceptionMapper

//设置package包名称以及导入依赖的类
package com.bob.reservefund.scala

import com.google.inject.Inject
import com.twitter.finagle.http.{Response, Request}
import com.twitter.finatra.http.exceptions.HttpException
import com.twitter.finatra.http.exceptions.ExceptionMapper
import com.twitter.finatra.http.response.ResponseBuilder

class MExceptionMapper @Inject()(response: ResponseBuilder) extends ExceptionMapper[Exception] {

  override def toResponse(request: Request, throwable: Exception): Response = {
    throwable match {
      case e: HttpException =>
        val builder = response.status(e.statusCode)
        builder.json(
          s"""
             |{
             |  "msg": ${e.getMessage()}
              |}
          """.stripMargin)
      case _: Exception =>
        response.status(500).json( """{
                                     |      "name": "Bob",
                                     |      "age": 19
                                     |    }""")
    }
  }
} 
开发者ID:bobxwang,项目名称:ReserveFundService,代码行数:29,代码来源:MExceptionMapper.scala

示例12: WebServer

//设置package包名称以及导入依赖的类
package com.gilesc
package mynab
package finch

import com.gilesc.mynab.finch.endpoint.AccountEndpoints
import com.gilesc.mynab.finch.endpoint.AccountGroupEndpoints

object WebServer {
  import com.twitter.finagle.http.{Request, Response}
  import com.twitter.finagle.{Http, Service}
  import com.twitter.util.Await
  import io.circe.generic.auto._
  import io.finch._
  import io.finch.circe._

  // Endpoints
  val api: Service[Request, Response] = (
      AccountGroupEndpoints.postGroup :+:
      AccountGroupEndpoints.getGroup :+:
      AccountEndpoints.getAccount :+:
      AccountEndpoints.postAccount
    ).handle({
    case e => NotFound(new Exception(e.toString))
  }).toServiceAs[Application.Json]

  // Service Stuff
  def shutdown(): Unit = {}

  def main(args: Array[String]): Unit = {
    val server = Http.server.serve(":8080", api)
    Await.ready(server)
    shutdown()
  }
} 
开发者ID:CraigGiles,项目名称:mynab,代码行数:35,代码来源:WebServer.scala

示例13: ServerStatsController

//设置package包名称以及导入依赖的类
package br.com.verde.serverstats

import com.twitter.finatra.http.Controller
import com.google.inject.Inject
import com.twitter.finatra.validation._
import com.google.inject.Singleton
import com.twitter.finagle.http.Request
import java.lang.management.ManagementFactory
import java.lang.management.OperatingSystemMXBean
import java.io.File

@Singleton
class ServerStatsController @Inject() extends Controller {

  val runtime = Runtime.getRuntime

  val osBean = ManagementFactory.getOperatingSystemMXBean
  val runtimeBean = ManagementFactory.getRuntimeMXBean
  val memoryBean = ManagementFactory.getMemoryMXBean

  val megabyte = 1024 * 1024
  val second = 1000 // milisecond

  get("/serverStats") {
    request: Request =>

      val jvmTotalRam = runtime.maxMemory
      val jvmFreeRam = runtime.freeMemory
      val availableProcessors = osBean.getAvailableProcessors

      val freeSpace = File.listRoots.head.getFreeSpace

      val serverSystemLoad = osBean.getSystemLoadAverage
      val serverUptime = runtimeBean.getUptime

      val param = request.getParam("onlyRam")
      if (param == null) {
        response
          .ok
          .json(s"""{ "jvm_total_ram": "${jvmTotalRam / megabyte} Mb",
          "jvm_free_ram": "${jvmFreeRam / megabyte} Mb",
          "free_space": "${freeSpace / megabyte} Mb",
          "server_system_load": "${serverSystemLoad} %",
          "server_uptime": "${serverUptime / second} s",
          "available_processors": "${availableProcessors}" }""")
      } else {
        response
          .ok
          .json(s"""{ "jvm_total_ram": "${jvmTotalRam / megabyte} Mb",
          "jvm_free_ram": "${jvmFreeRam / megabyte} Mb" }""")
      }
  }
} 
开发者ID:verdetecnologia,项目名称:ServerStats,代码行数:54,代码来源:ServerStatsController.scala

示例14: MockAppSpec

//设置package包名称以及导入依赖的类
package io.peregrine.test

import io.peregrine.{Controller, MockApp}
import com.twitter.finagle.http.Request
import org.jboss.netty.handler.codec.http.HttpMethod
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.JavaConverters._

class MockAppSpec extends FlatSpec with Matchers {
  val server = MockApp(new Controller{})

  "#toByteArray" should "directly convert String to Array[Byte]" in {
    val value = "hello world"
    server.toByteArray(value).get should be(value.getBytes)
  }

  it should "also directly convert Array[Byte] to Array[Byte]" in {
    val value = "hello world".getBytes
    server.toByteArray(value).get should be(value)
  }

  it should "convert Map[String, String] to url-encoded form data" in {
    val value = Map("hello" -> "world")
    server.toByteArray(value).get should be("hello=world".getBytes)
  }

  it should "convert util.Map[String, String] to url-encoded form data" in {
    val value = Map("hello" -> "world").asJava
    server.toByteArray(value).get should be("hello=world".getBytes)
  }

  it should "convert null to None" in {
    server.toByteArray(null) should be(None)
  }

  it should "attempt to convert other objects to a json equivalent" in {
    val sample = Sample("matt", "[email protected]")
    server.toByteArray(sample).get should be( """{"name":"matt","email":"[email protected]"}""".getBytes)
  }

  "#buildRequest" should "apply body if present" in {
    val sample = Sample("matt", "[email protected]")

    // When
    val request: Request = server.buildRequest(HttpMethod.POST, "/", body = sample)

    // Then
    request.contentString should be(MockApp.mapper.writeValueAsString(sample))
  }

  it should "not allow both params AND a non-null body in the same request" in {
    val sample = Sample("matt", "[email protected]")
    a [RuntimeException] should be thrownBy {
      server.buildRequest(HttpMethod.POST, "/", params = Map("hello" -> "world"), body = sample)
    }
  }

  case class Sample(name: String, email: String)
} 
开发者ID:pairi,项目名称:pairi,代码行数:61,代码来源:MockAppSpec.scala

示例15: ServerMain

//设置package包名称以及导入依赖的类
package $package$

import com.twitter.finagle.http.{Request, Response}
import com.twitter.finatra.http.HttpServer
import com.twitter.finatra.http.filters.{CommonFilters, LoggingMDCFilter, TraceIdMDCFilter}
import com.twitter.finatra.http.routing.HttpRouter
import $package$.controllers.MainController

object ServerMain extends Server

class Server extends HttpServer {
  override val name = "$package$ $name;format="Camel"$"

  override def configureHttp(router: HttpRouter) {
    router
      .filter[CommonFilters]
      .filter[LoggingMDCFilter[Request, Response]]
      .filter[TraceIdMDCFilter[Request, Response]]
      .add[MainController]
  }
} 
开发者ID:jimschubert,项目名称:finatra.g8,代码行数:22,代码来源:Server.scala


注:本文中的com.twitter.finagle.http.Request类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。