• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala RequestBuilding类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: ReplyMessageRequest

//设置package包名称以及导入依赖的类
package bot.line.client

import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.headers.OAuth2BearerToken
import akka.http.scaladsl.model._
import bot.line.json.MessagesJsonSupport
import bot.line.model.send.{Messages, TextMessage}

import scala.concurrent.ExecutionContext

case class ReplyMessageRequest(
                                accessToken: String,
                                replyToken: String,
                                message: String
                              ) extends MessagesJsonSupport {

  def httpRequest(implicit ec: ExecutionContext): HttpRequest = {
    val auth:HttpHeader = headers.Authorization(OAuth2BearerToken(accessToken))
    val content = Messages(
      replyToken = replyToken,
      messages = List(TextMessage(text = message))
    )
    RequestBuilding.Post(
      uri = "https://api.line.me/v2/bot/message/reply",
      content = content
    ).withHeaders(auth)
  }
} 
开发者ID:xoyo24,项目名称:akka-http-line-bot,代码行数:29,代码来源:ReplyMessageRequest.scala


示例2: HttpFetcher

//设置package包名称以及导入依赖的类
package au.csiro.data61.magda.external

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model._
import akka.stream.Materializer
import akka.stream.scaladsl._
import au.csiro.data61.magda.util.Http.getPort

import scala.concurrent.{ ExecutionContext, Future }

class HttpFetcher(interfaceConfig: InterfaceConfig, implicit val system: ActorSystem,
  implicit val materializer: Materializer, implicit val ec: ExecutionContext) {

  lazy val connectionFlow: Flow[HttpRequest, HttpResponse, Any] =
    Http().outgoingConnection(interfaceConfig.baseUrl.getHost, getPort(interfaceConfig.baseUrl))

  def request(path: String): Future[HttpResponse] =
    interfaceConfig.fakeConfig match {
      case Some(fakeConfig) => Future {
        val file = io.Source.fromInputStream(getClass.getResourceAsStream(fakeConfig.datasetPath))

        val response = new HttpResponse(
          status = StatusCodes.OK,
          headers = scala.collection.immutable.Seq(),
          protocol = HttpProtocols.`HTTP/1.1`,
          entity = HttpEntity(ContentTypes.`application/json`, file.mkString))

        file.close()

        response
      }
      case None => {
        val request = RequestBuilding.Get(s"${interfaceConfig.baseUrl.getPath}${path}")
        Source.single(request).via(connectionFlow).runWith(Sink.head)
      }
    }
} 
开发者ID:TerriaJS,项目名称:magda-ckan,代码行数:40,代码来源:HttpFetcher.scala


示例3: getWithCfpbHeaders

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

import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.marshalling.ToEntityMarshaller
import akka.http.scaladsl.model.{ HttpMethods, HttpRequest }
import hmda.api.headers.{ HmdaInstitutionsHeader, HmdaUsernameHeader }

trait RequestHeaderUtils extends RequestBuilding {
  import HttpMethods._

  def getWithCfpbHeaders(path: String): HttpRequest = {
    new RequestBuilder(GET).apply(path)
      .addHeader(usernameHeader)
      .addHeader(institutionsHeader)
  }

  def postWithCfpbHeaders[T, ec: EC](path: String, content: T)(implicit m: ToEntityMarshaller[T]) = {
    new RequestBuilder(POST).apply(path, content)
      .addHeader(usernameHeader)
      .addHeader(institutionsHeader)
  }

  def postWithCfpbHeaders(path: String) = {
    new RequestBuilder(POST).apply(path)
      .addHeader(usernameHeader)
      .addHeader(institutionsHeader)
  }

  val usernameHeader = new HmdaUsernameHeader("banker11")
  val institutionsHeader = new HmdaInstitutionsHeader(List("0", "xxxxx"))

} 
开发者ID:cfpb,项目名称:hmda-platform,代码行数:33,代码来源:RequestHeaderUtils.scala


示例4: HttpDispatcher

//设置package包名称以及导入依赖的类
import akka.actor.ActorSystem

import akka.event.{LoggingAdapter, Logging}
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.{ActorMaterializer, Materializer}
import akka.stream.scaladsl.{Flow, Sink, Source}
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import java.io.IOException
import scala.concurrent.{ExecutionContextExecutor, Future}
import scala.math._
import spray.json.DefaultJsonProtocol


package util.http {

  class HttpDispatcher { 

   implicit val system = ActorSystem()
   implicit val executor = system.dispatcher
   implicit val materializer = ActorMaterializer()

    lazy val setlisterConnectionFlow: Flow[HttpRequest, HttpResponse, Any] = Http().outgoingConnection("www.setlister.me", 80)  

    def setlisterRequest(request: HttpRequest): Future[HttpResponse] = Source.single(request).via(setlisterConnectionFlow).runWith(Sink.head)

    def getURL(url: String) {
      setlisterRequest(RequestBuilding.Get(url)).flatMap { response =>
        response.status match {
          case OK =>  {
            Unmarshal(response.entity).to[String].flatMap { entity =>
              Future.successful(println(entity))
            }
          }
          case BadRequest => Future.successful(Left(s"incorrect IP format"))
          case _ => Unmarshal(response.entity).to[String].flatMap { entity =>
            val error = s"FreeGeoIP request failed with status code ${response.status} and entity $entity"
            //logger.error(error)
            println("error")
            Future.failed(new IOException(error))
          }
        }
      }
    }
  }
} 
开发者ID:dev-yohan,项目名称:akka-testing,代码行数:54,代码来源:HttpDispatcher.scala


示例5: omdbApiRequest

//设置package包名称以及导入依赖的类
package org.yashsriv.api

import java.io.IOException

import scala.concurrent.Future

import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.{ HttpResponse, HttpRequest }
import akka.http.scaladsl.model.StatusCodes.{ OK, BadRequest }
import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.unmarshalling.Unmarshal

import spray.json._

import org.yashsriv.helpers.Worker
import org.yashsriv.json.MovieSupport
import org.yashsriv.models.MovieQuery
import org.yashsriv.models.MovieResult

trait Movie extends Directives with Worker with MovieSupport with SprayJsonSupport {

  def omdbApiRequest(request: HttpRequest): Future[HttpResponse] = Http().singleRequest(request)

  def movieApi(implicit requestUri: Uri): Route = pathPrefix("movie") {
    (post & entity(as[MovieQuery])) { movieQuery ?
      complete {
        fetchMovieInfo(movieQuery).map[ToResponseMarshallable] {
          case Right(movieInfo) => movieInfo
          case Left(errorMessage) => BadRequest ? errorMessage
        }
      }
    }
  }

  def fetchMovieInfo(mq: MovieQuery)(implicit requestUri: Uri): Future[Either[String, MovieResult]] = {
    omdbApiRequest(RequestBuilding.Get(requestUri withQuery convertToQuery(mq))).flatMap { response =>
      response.status match {
        case OK         ? Unmarshal(response.entity).to[MovieResult].map(Right(_))
        case BadRequest ? Future.successful(Left(s"${mq.toJson.prettyPrint} \nIncorrect Movie Format"))
        case _          ? Unmarshal(response.entity).to[String].flatMap { entity ?
          val error = s"Omdb request failed with status code ${response.status} and entity $entity"
          Future.failed(new IOException(error))
        }
      }
    }
  }

} 
开发者ID:yashsriv,项目名称:akka-http-batch-api,代码行数:54,代码来源:Movie.scala


示例6: CheckJobStatusInteractor

//设置package包名称以及导入依赖的类
package xyz.joaovasques.sparkapi.api.standalone.interactors

import xyz.joaovasques.sparkapi.messages.SparkApiMessages._
import scala.concurrent.Future
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.actor.ActorSystem
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.model.ResponseEntity
import akka.http.scaladsl.client.RequestBuilding
import xyz.joaovasques.sparkapi.helpers._
import akka.stream.ActorMaterializer
import akka.http.scaladsl.model.StatusCodes._
import xyz.joaovasques.sparkapi.actors.SparkActor._

class CheckJobStatusInteractor(val sparkApi: HttpRequest => Future[HttpResponse],
  val master: String, val apiVersion: String = "v1"
)(implicit system: ActorSystem) extends Interactor[String, SparkJobStatusResponse] with JsonHelpers {
  private val endpoint = s"/${apiVersion}/submissions/status"
  implicit val executionContext = system.dispatcher
  implicit val materializer = ActorMaterializer()

  private val unmarshaller: ResponseEntity => Future[SparkJobStatusResponse] =
  { entity => Unmarshal(entity).to[SparkJobStatusResponse] }

  private def handleSparkResponse(response: HttpResponse): Future[SparkJobStatusResponse] =
    response.status match {
      case OK => unmarshaller(response.entity)
      case otherStatus =>
        throw new Exception(s"Error Communicating with Spark. Status code ${otherStatus}")
    }

  def call(driverId: String): Future[SparkJobStatusResponse] = {
    val httpRequest = RequestBuilding.Get(s"${endpoint}/${driverId}")
    sparkApi(httpRequest) flatMap { handleSparkResponse(_) }
  }
} 
开发者ID:JoaoVasques,项目名称:spark-api,代码行数:37,代码来源:CheckJobStatusInteractor.scala


示例7: KillJobInteractor

//设置package包名称以及导入依赖的类
package xyz.joaovasques.sparkapi.api.standalone.interactors

import scala.concurrent.Future

import akka.actor.ActorSystem
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpRequest, HttpResponse, ResponseEntity}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import xyz.joaovasques.sparkapi.helpers._
import xyz.joaovasques.sparkapi.messages.SparkApiMessages._
import xyz.joaovasques.sparkapi.actors.SparkActor._

class KillJobInteractor(val sparkApi: HttpRequest => Future[HttpResponse],
  val master: String, val apiVersion: String = "v1"
)(implicit system: ActorSystem) extends Interactor[String, SparkJobKillResponse] with JsonHelpers {

  private val endpoint = s"/${apiVersion}/submissions/kill"
  implicit val executionContext = system.dispatcher
  implicit val materializer = ActorMaterializer()

  private val unmarshaller: ResponseEntity => Future[SparkJobKillResponse] =
  { entity => Unmarshal(entity).to[SparkJobKillResponse] }

  private def handleSparkResponse(response: HttpResponse): Future[SparkJobKillResponse] =
    response.status match {
      case OK => unmarshaller(response.entity)
      case otherStatus =>
        Future.failed(new Exception(s"Communicating with Spark: status code ${otherStatus}"))
    }

  def call(driverId: String): Future[SparkJobKillResponse] = {
    val httpRequest = RequestBuilding.Post(s"${endpoint}/${driverId}", HttpEntity(ContentTypes.`application/json`, ""))
      .addHeader(header)

    sparkApi(httpRequest) flatMap { handleSparkResponse(_) }
  }
} 
开发者ID:JoaoVasques,项目名称:spark-api,代码行数:40,代码来源:KillJobInteractor.scala


示例8: SubmitJobInteractor

//设置package包名称以及导入依赖的类
package xyz.joaovasques.sparkapi.api.standalone.interactors

import xyz.joaovasques.sparkapi.messages.SparkApiMessages._
import scala.concurrent.Future
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.actor.ActorSystem
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.model.{ HttpHeader, ResponseEntity }
import akka.http.scaladsl.model.HttpHeader.ParsingResult
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.{ ContentTypes, HttpEntity }
import xyz.joaovasques.sparkapi.helpers._
import akka.stream.ActorMaterializer
import akka.http.scaladsl.model.StatusCodes._
import xyz.joaovasques.sparkapi.exceptions.SparkApiExceptions._
import xyz.joaovasques.sparkapi.actors.SparkActor._

class SubmitJobInteractor(val sparkApi: HttpRequest => Future[HttpResponse],
  val master: String, val apiVersion: String = "v1"
)(implicit system: ActorSystem) extends Interactor[SubmitJob, SparkJobSumissionResponse] with JsonHelpers {

  private val endpoint = s"/${apiVersion}/submissions/create"
  implicit val executionContext = system.dispatcher
  implicit val materializer = ActorMaterializer()

  private val unmarshaller: ResponseEntity => Future[SparkJobSumissionResponse] =
  { entity => Unmarshal(entity).to[SparkJobSumissionResponse] }

  private def handleSparkResponse(response: HttpResponse): Future[SparkJobSumissionResponse] =
    response.status match {
      case OK =>
        for(unmarshalledResponse <- unmarshaller(response.entity)) yield {
          if(unmarshalledResponse.success)
            unmarshalledResponse
          else
          throw JobSubmissionFailedException()
        }
      case otherStatus =>
        throw new SparkCommunicationException(otherStatus.intValue)
    }

  def call(request: SubmitJob): Future[SparkJobSumissionResponse] = {
    val httpRequest = RequestBuilding.Post(endpoint,
      HttpEntity(ContentTypes.`application/json`, generateSubmitJsonBody(request, master, request.envVars)))
      .addHeader(header)

    sparkApi(httpRequest) flatMap { handleSparkResponse(_) }
  }
} 
开发者ID:JoaoVasques,项目名称:spark-api,代码行数:50,代码来源:SubmitJobInteractor.scala


示例9: Starter

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

import java.io.IOException

import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import com.typesafe.config.ConfigFactory

import scala.concurrent.Future



object Starter extends App {

  implicit val system = ActorSystem()
  implicit val executor = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val config = ConfigFactory.load()
  val logger = Logging(system, getClass)

  lazy val apiFlow: Flow[HttpRequest, HttpResponse, Any] =
    Http().outgoingConnectionHttps(config.getString("services.trade-api.host"), config.getInt("services.trade-api.port"))

  def apiRequest(request: HttpRequest): Future[HttpResponse] =
    Source.single(request)
      .via(apiFlow)
      .runWith(Sink.head)

  def fetchInfo(symbol: String): Future[Either[String, String]] = {
    val apiurl = config.getString("services.trade-api.trade")
    apiRequest(RequestBuilding.Get(apiurl+"?symbol="+symbol)).flatMap { response =>
      response.status match {
        case OK =>
          Unmarshal(response.entity).to[String].map(Right(_))
        case BadRequest =>
          Future.successful(Left(s"$symbol: incorrect Symbol format"))
        case _ =>
          Unmarshal(response.entity).to[String].flatMap { entity =>
            val error = s"Trade request failed with status code ${response.status} and entity $entity"
            logger.error(error)
            Future.failed(new IOException(error))
          }
      }
    }
  }

  val res = fetchInfo("btc_cny").map {
    case Right(info) => logger.info("Result: {}", info)
    case Left(errorMessage) => logger.info("Result: {}", errorMessage)
  }
} 
开发者ID:solverit,项目名称:akka-okoin-client,代码行数:60,代码来源:Starter.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala SourceShape类代码示例发布时间:2022-05-23
下一篇:
Scala Log类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap