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


Scala HttpClientBuilder类代码示例

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


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

示例1: Item

//设置package包名称以及导入依赖的类
package com.github.vladminzatu.surfer.persist

import com.github.vladminzatu.surfer.Score
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{HttpClientBuilder}
import org.apache.spark.rdd.RDD
import org.json4s.jackson.Serialization.write

case class Item(item:String, score:Double)

class RestPersister extends Persister {

  val url = "http://localhost:8080/items"

  override def persist(scores: RDD[(String, Score)]): Unit = {
    implicit val formats = org.json4s.DefaultFormats
    val payload = write(scores.collect().sortWith((a,b) => a._2.value > b._2.value).map(x => Item(x._1, x._2.value)))
    val client = HttpClientBuilder.create().build();
    client.execute(postRequest(payload))
  }

  private def postRequest(payload: String): HttpPost = {
    val post = new HttpPost(url)
    post.setEntity(new StringEntity(payload))
    post
  }
} 
开发者ID:VladMinzatu,项目名称:surfer,代码行数:29,代码来源:RestPersister.scala

示例2: JSONHTTP

//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders.dataaccess.web

import com.amazonaws.util.IOUtils
import com.fasterxml.jackson.core.`type`.TypeReference
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.net.URI
import javax.xml.ws.http.HTTPException
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.slf4j.LoggerFactory
import works.weave.socks.aws.orders.ProjectDefaultJacksonMapper

object JSONHTTP {

  val Log = LoggerFactory.getLogger(getClass)

  val objectMapper = ProjectDefaultJacksonMapper.build() // after (_.bla)

  def get[T : Manifest : NotNothing](uri : URI) : T = {
    val client = HttpClientBuilder.create.build
    val get = new HttpGet(uri)
    get.addHeader("Accept", "application/json")
    val response = client.execute(get)
    val responseString = IOUtils.toString(response.getEntity.getContent)
    Log.info(s"Got status ${response.getStatusLine.getStatusCode}")
    if (response.getStatusLine.getStatusCode == 200) {
      Log.info(s"Got response from URI $uri: $responseString")
      objectMapper.readValue(responseString, typeReference[T])
    } else {
      throw new HTTPException(response.getStatusLine.getStatusCode)
    }
  }

  def typeReference[T : Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  def typeFromManifest(m : Manifest[_]) : Type = {
    if (m.typeArguments.isEmpty) { m.runtimeClass }
    else new ParameterizedType {
      def getRawType = m.runtimeClass
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }

  // Trick to disable nothign for type param
  sealed trait NotNothing[-T]

  object NotNothing {
    implicit object notNothing extends NotNothing[Any]
    implicit object `The error is because the missing type parameter was resolved to Nothing` extends NotNothing[Nothing]
  }

} 
开发者ID:Compositional,项目名称:orders-aws,代码行数:57,代码来源:JSONHTTP.scala

示例3: healthCheckUrl

//设置package包名称以及导入依赖的类
package io.github.shogowada.scala.jsonrpc.example.test.utils

import java.net.ServerSocket

import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder

import scala.util.Try

trait BaseTarget {

  lazy val port = freePort()
  lazy val url = s"http://localhost:$port"
  lazy val jarLocation = System.getProperty("jarLocation")

  def healthCheckUrl = url

  def freePort(): Int = {
    val server = new ServerSocket(0)
    val localPort = server.getLocalPort
    server.close()
    localPort
  }

  private def startProcess(jarLocation: String, port: Int): Process = {
    new ProcessBuilder(
      "java", s"-Dport=$port", "-jar", jarLocation
    ).start()
  }

  private def waitUntilReady(): Unit = {
    Range(0, 10).toStream
        .map(_ => {
          Thread.sleep(1000)
          val client = HttpClientBuilder.create().build()
          val maybeCode = Try {
            val response = client.execute(new HttpGet(healthCheckUrl))
            val code = response.getStatusLine.getStatusCode
            response.close()
            code
          }.toOption
          client.close()
          maybeCode
        })
        .filter(code => code.contains(200))
        .head
  }

  val target = startProcess(jarLocation, port)

  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run(): Unit = {
      target.destroy()
    }
  })

  waitUntilReady()
} 
开发者ID:shogowada,项目名称:scala-json-rpc,代码行数:59,代码来源:BaseTarget.scala


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