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


Scala Http类代码示例

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


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

示例1: ScalajHttpAdapter

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

import org.springframework.beans.factory.annotation.{Autowired, Value}
import org.springframework.stereotype.Component

import scala.concurrent.{ExecutionContext, Future}
import scalaj.http.Http

@Component
class ScalajHttpAdapter @Autowired()(@Value("${http.timeouts:2000}") private val timeouts: Int) extends HttpAdapter {

  override def request(url: String, params: Map[String, String]): String =
    Http(url)
      .params(params)
      .timeout(timeouts, timeouts)
      .asString
      .body

  override def asyncRequest(url: String, params: Map[String, String])
                           (implicit executionContext: ExecutionContext): Future[String] =
    Future(request(url, params))
} 
开发者ID:piotrglazar,项目名称:receipt-lottery,代码行数:23,代码来源:ScalajHttpAdapter.scala

示例2: MCWhitelistHelper

//设置package包名称以及导入依赖的类
package com.amadornes.modcast.bot.helpers

import java.net.URLEncoder

import com.amadornes.modcast.bot.Actors
import com.amadornes.modcast.bot.database.{DB, UserMCAccount}
import com.amadornes.modcast.bot.servers.MCWhitelistServer
import com.google.gson.{Gson, JsonObject}
import sx.blah.discord.handle.obj.IUser

import scalaj.http.Http


object MCWhitelistHelper {
	def associateMCAccountWithUser(user: IUser, account: String): Unit = {
		val id = getMCAccountUUID(account)
		if (DB.query[UserMCAccount].whereEqual("user", user.getID).exists())
			deassociateMCAccountWithUser(user)
		Actors.servers.MCWhitelistServer ! MCWhitelistServer.WhitelistUser(id)
		DB.save(UserMCAccount(user.getID, id))
	}
	
	def deassociateMCAccountWithUser(user: IUser): Unit = {
		val account = DB.query[UserMCAccount].whereEqual("user", user.getID).fetchOne()
		if (account.isDefined) {
			Actors.servers.MCWhitelistServer ! MCWhitelistServer.UnWhitelistUser(account.get.account)
			DB.delete(account.get)
		}
	}
	
	def getMCAccountUUID(name: String): String = {
		val http = Http("http://mcapi.ca/profile/" + URLEncoder.encode(name)).asString
		if (http.code != 200)
			throw new IllegalArgumentException()
		new Gson().fromJson(http.body, classOf[JsonObject]).get("uuid_formatted").getAsString
	}
} 
开发者ID:Modcast,项目名称:ModcastBot,代码行数:38,代码来源:MCWhitelistHelper.scala

示例3: WarmupZeppelin

//设置package包名称以及导入依赖的类
package biz.meetmatch.modules.util

import biz.meetmatch.modules.Module
import biz.meetmatch.util.Utils
import com.google.gson.Gson
import org.apache.spark.sql.SparkSession
import org.rogach.scallop.Scallop
import org.slf4j.LoggerFactory

import scalaj.http.Http

object WarmupZeppelin extends Module {

  override def execute(scallopts: Scallop)(implicit sparkSession: SparkSession): Unit = {
    val interpreterId = Utils.getConfig("zeppelin.interpreter.id")

    logger.info(s"Restarting the spark interpreter using url http://localhost:8080/api/interpreter/setting/restart/$interpreterId...")
    val interpreterResponse = Http(s"http://localhost:8080/api/interpreter/setting/restart/$interpreterId")
      .method("put")
      .timeout(connTimeoutMs = 10000, readTimeoutMs = 60000)
      .asParamMap
    logger.info("Response code: " + interpreterResponse.code)

    if (interpreterResponse.code == 200) {
      val bootstrapNotebookId = Utils.getConfig("zeppelin.bootstrapNotebookId.id")

      logger.info(s"Executing the Bootstrap notebook using url http://localhost:8080/api/notebook/job/$bootstrapNotebookId...")
      val notebookResponse = Http(s"http://localhost:8080/api/notebook/job/$bootstrapNotebookId")
        .postForm
        .timeout(connTimeoutMs = 10000, readTimeoutMs = 60000)
        .asParamMap
      logger.info("Response code: " + notebookResponse.code)

      if (notebookResponse.code == 200) {
        def stillRunningParagraphs(): Boolean = {
          logger.info("Checking if the Bootstrap notebook has finished...")
          val checkNotebookResponse = Http("http://localhost:8080/api/notebook/job/2BKFC4D5W")
            .timeout(connTimeoutMs = 10000, readTimeoutMs = 120000)
            .asString
          logger.info("Response code: " + checkNotebookResponse.code)
          val notebookJobResponse = new Gson().fromJson(checkNotebookResponse.body, classOf[GetNotebookJobResponse])
          notebookJobResponse.body.exists(_.status != "FINISHED")
        }

        while (stillRunningParagraphs()) {
          logger.info("Keep on polling...")
          Thread.sleep(5000)
        }
        logger.info("The Bootstrap notebook has finished.")
      }
    }
  }

  case class GetNotebookJobResponse(status: String, body: Array[ParagraphStatus])

  case class ParagraphStatus(id: String, started: String, finished: String, status: String)

  override protected val logger = LoggerFactory.getLogger(this.getClass)
} 
开发者ID:tolomaus,项目名称:languagedetector,代码行数:60,代码来源:WarmupZeppelin.scala

示例4: WebAssetsCache

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

import java.io._
import java.net.URL

import org.apache.commons.io.IOUtils

import scala.util.Try
import scala.util.control.Exception
import scalaj.http.{Http, HttpOptions}

object WebAssetsCache {
  private val cacheDir = "target/cached-webassets"

  def inputStream(url: String): InputStream = {
    val cachedName: String = {
      val (host, file) = new URL(url) match { case u ?
        u.getHost ? u.getFile
      }

      s"${Integer.toHexString(host.hashCode)}/${Integer.toHexString(file.hashCode)}"
    }

    val cached: Option[File] = {
      Try(new File(s"$cacheDir/$cachedName"))
        .filter(f ? f.exists() && f.isFile && f.length() > 0)
        .toOption
    }

    cached match {
      case Some(file) ?
        new FileInputStream(file)

      case None ?
        val http = Http(url).options(HttpOptions.connTimeout(10000), HttpOptions.readTimeout(10000)).asBytes
        assert(http.isSuccess, s"Web asset download failed: $url")
        val bytes = http.body
        val outputStream = Try {
          val file = new File(s"$cacheDir/$cachedName")
          require(file.getParentFile.isDirectory || file.getParentFile.mkdirs(), s"Not a directory: ${file.getParentFile}")
          new FileOutputStream(file)
        }
        outputStream.foreach { stream ?
          Exception.allCatch.andFinally(IOUtils.closeQuietly(stream)) {
            IOUtils.write(bytes, stream)
          }
        }
        new ByteArrayInputStream(bytes)
    }
  }

  def text(url: String): String = {
    val inputStream = WebAssetsCache.inputStream(url)
    Exception.allCatch.andFinally(IOUtils.closeQuietly(inputStream)) {
      IOUtils.toString(inputStream, "UTF-8")
    }
  }
} 
开发者ID:Karasiq,项目名称:sbt-scalajs-bundler,代码行数:59,代码来源:WebAssetsCache.scala

示例5: OauthSpotifyEndpoint

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

import scalaj.http.{Http, HttpRequest}

abstract class OauthSpotifyEndpoint extends SpotifyEndpoint {

  private def getOauthBearer(authToken: String): Option[String] = authToken match {
    case "" => None
    case token => Some("Bearer " + token)
  }

  private def getAuthHeaders(authToken: String): Option[Map[String, String]] = getOauthBearer(authToken) match {
    case None => None
    case Some(authHeader) => Some(Map("Authorization" -> authHeader))
  }

  protected def createRequest(authToken:String, endpoint: String): Option[HttpRequest] = {
    getAuthHeaders(authToken).map(headers => {
      Some(Http(endpoint).headers(headers))
    }).getOrElse(None)
  }

  protected def createRequest(authToken:String,
                            endpoint: String,
                            params: Seq[(String, String)]) : Option[HttpRequest] = {
    getAuthHeaders(authToken).map(headers => {
      Some(Http(endpoint).headers(headers).params(params))
    }).getOrElse(None)
  }
} 
开发者ID:Jakeway,项目名称:spotify-web-api-scala,代码行数:31,代码来源:OauthSpotifyEndpoint.scala

示例6: Api

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

import Config.config

import io.circe.Json
import io.circe.generic.auto._
import io.circe.parser.parse
import io.circe.syntax._

import scala.language.postfixOps
import scalaj.http.{Http, HttpRequest}

object Api {
  private val cfg = config match {
    case Right(c) => c
    case Left(e) =>
      throw new Exception(e.messages.mkString("\n"))
  }

  private def request(endPoint: String): HttpRequest =
    Http("https://api.feedbin.com/v2/" + endPoint)
      .auth(cfg.email, cfg.password)
      .timeout(connTimeoutMs = cfg.connectionTimeout, readTimeoutMs = cfg.readTimeout)

  private def postJson(endPoint: String, data: Json): HttpRequest =
    request(endPoint)
      .postData(data.toString)
      .header("content-type", "application/json")

  
  def subscribe(feed_url: String): Option[Subscription] = {
    val data = Json.obj("feed_url" -> feed_url.asJson)
    val response = postJson("subscriptions.json", data).asString
    parse(response.body).flatMap(_.as[Subscription]).toOption
  }
} 
开发者ID:jarrodwb,项目名称:feedbin,代码行数:37,代码来源:Api.scala

示例7: FacebookApi

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

import com.aluxian.susucatbot.config.Config
import com.aluxian.susucatbot.models.{AddressedFacebookAction, AddressedFacebookMessage, FacebookUser, ImageMessage}
import com.twitter.util.{Future, FuturePool}
import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._

import scalaj.http.Http

object FacebookApi {

  def getUser(id: String): Future[FacebookUser] = FuturePool.unboundedPool {
    val fields = List("first_name", "last_name", "profile_pic", "gender", "locale", "timezone")
    val userJson = Http(Config.facebookApiUrl + id)
      .param("access_token", Config.facebookAccessToken)
      .param("fields", fields.mkString(","))
      .header("Content-Type", "application/json")
      .header("User-Agent", Config.userAgent)
      .asString
      .throwError
      .body
    println("got user json from fb:" + userJson)
    println(parse(userJson))
    val dec = decode[FacebookUser](userJson)
    if (dec.isLeft) {
      dec.leftMap(_.printStackTrace())
    }
      dec.toOption.get
  }

  def post(body: String): Future[Unit] = FuturePool.unboundedPool {
    println("sending: " + body)
    val r = Http(Config.facebookApiUrl + "me/messages")
      .postData(body)
      .param("access_token", Config.facebookAccessToken)
      .header("Content-Type", "application/json")
      .header("User-Agent", Config.userAgent)
      .asString
    println("sent: " + r.body)
  }

  def postMessage(addressedFacebookAction: AddressedFacebookAction): Future[Unit] =
    post(addressedFacebookAction.asJson.noSpaces)

  def postMessage(addressedFacebookMessage: AddressedFacebookMessage): Future[Unit] =
    if (addressedFacebookMessage.message.isInstanceOf[ImageMessage]) {
      post(addressedFacebookMessage.toAlt2.asJson.noSpaces)
    } else {
      post(addressedFacebookMessage.toAlt.asJson.noSpaces)
    }
} 
开发者ID:aluxian,项目名称:SusuCatBot,代码行数:54,代码来源:FacebookApi.scala

示例8: DeputadosService

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

import com.nakamura.camara.deputados.deputado.{Deputado}
import org.apache.spark.sql.{SaveMode, SparkSession}

import scala.util.{Failure, Success, Try}
import scalaj.http.{Http, HttpResponse}


class DeputadosService(spark: SparkSession) {
  // For implicit conversions like converting RDDs to DataFrames
  import spark.implicits._

  private val logger = org.log4s.getLogger
  private val obterDeputadosEndpoint = "http://www.camara.leg.br/SitCamaraWS/Deputados.asmx/ObterDeputados"

  def getDeputados(): Try[Seq[Deputado]] = {
    logger.info(s"Sending request for deputados...")
    val response: HttpResponse[String] = Http(obterDeputadosEndpoint).postForm.asString
    if (response.isSuccess) {
      Try {
        val xml = scala.xml.XML.loadString(response.body)
        val deputadosNode = xml \\ "deputado"
        val deputados = deputadosNode.map(DeputadoUtils.fromXml)
        logger.info(s"Found ${deputados.length} deputados")
        deputados
      }
    } else {
      Failure(new Error("Request failed."))
    }
  }

  def fetchAndStoreDeputadosData(saveMode: SaveMode = SaveMode.ErrorIfExists): Unit = {
    getDeputados() match {
      case Success(deputados) =>
        val deputadossDs = deputados.toDS()
        deputadossDs.write.mode(saveMode)
          .format("json")
          .save(s"./data/deputados/")
      case Failure(err) =>
        logger.error(s"Failed to save deputados with $err")
    }
  }
} 
开发者ID:akionakamura,项目名称:camara,代码行数:45,代码来源:DeputadosService.scala

示例9: VkCommentsTask

//设置package包名称以及导入依赖的类
package com.crawler.osn.vkontakte.tasks

import akka.actor.ActorRef
import com.mongodb.util.JSON
import com.mongodb.{BasicDBList, BasicDBObject}
import com.crawler.osn.common.{ResponseTask, SaveTask, VkontakteAccount, VkontakteTask}
import com.crawler.dao.{MemorySaverInfo, SaverInfo}

import scalaj.http.Http


case class VkCommentsTask(ownerId: String,
                          postId: String,
                          var count: Int = 1000000,
                          override val responseActor: AnyRef = null,
                          saverInfo: SaverInfo = MemorySaverInfo())(implicit app: String)
  extends VkontakteTask
    with ResponseTask
    with SaveTask {

  override def appname: String = app

  override def extract(account: VkontakteAccount) = {
    var end = false
    var offset = 0

    while(!end) {
      val request = Http("https://api.vk.com/method/wall.getComments?")
        .param("owner_id", ownerId.toString)
        .param("post_id", postId.toString)
        .param("count", "100")
        .param("need_likes", "1")
        .param("offset", offset.toString)
        .param("v", "5.8")
        .timeout(60 * 1000 * 10, 60 * 1000 * 10)

      val comments = JSON.parse(request.execute().body).asInstanceOf[BasicDBObject]
        .get("response").asInstanceOf[BasicDBObject]
        .get("items").asInstanceOf[BasicDBList].toArray()
        .map { case b:BasicDBObject =>
          b.append("key", s"${ownerId}_${postId}_${b.getString("from_id")}_${b.getString("id")}")
            .append("post_owner", ownerId)
            .append("post_id", postId)
        }

      offset += comments.length
      count -= comments.length

      if (comments.length < 100 || count <= 0) end = true

      save(comments)
//      response(responseActor, VkRepostsTaskResponse(this, comments))
    }
  }

  override def name: String = s"VkCommentsTask(item=$ownerId)"

} 
开发者ID:mxmptv,项目名称:osn.crawler,代码行数:59,代码来源:VkCommentsTask.scala

示例10: run

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

import java.util.Date

import com.crawler.dao.{MemorySaverInfo, SaverInfo}
import com.crawler.osn.common.{SaveTask, StateTask, YoutubeTask}
import com.crawler.util.Util
import com.mongodb.{BasicDBList, BasicDBObject}
import com.mongodb.util.JSON

import scalaj.http.Http


  override def run(network: AnyRef): Unit = {

    var offset = 0
    var pageToken = ""
    val nowDate = new Date()


    val request = Http("https://content.googleapis.com/youtube/v3/videos")
      .param("regionCode", country)
      .param("chart", "mostPopular")
      .param("part", "snippet,contentDetails,statistics")
      .param("maxResults", "50")
      .param("pageToken", pageToken)
      .timeout(60 * 1000 * 10, 60 * 1000 * 10)

    val json = exec(request)

    val result = JSON.parse(json).asInstanceOf[BasicDBObject]

    val videos = result
      .get("items").asInstanceOf[BasicDBList].toArray()
      .map { case b:BasicDBObject =>
        offset += 1
        b.append("key", s"${b.getString("id")}_${country}_${nowDate}")
         .append("county", country)
         .append("update_time", nowDate)
         .append("top_position", offset)
      }.toIterable

    save(videos)
  }
} 
开发者ID:mxmptv,项目名称:osn.crawler,代码行数:46,代码来源:YoutubeTopsTask.scala

示例11: StatusApiSpec

//设置package包名称以及导入依赖的类
package name.denyago.yasc.integration.httpapi

import name.denyago.yasc.Main
import org.scalatest.concurrent.Eventually
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.{Exceptional, FunSpec, Matchers, Outcome}

import scala.util.Try
import scalaj.http.Http

class StatusApiSpec extends FunSpec with Matchers with Eventually {
  override def withFixture(test: NoArgTest): Outcome = {
    val app = new Main(Array.empty[String])
    app.run()

    val outcome = Try { super.withFixture(test) }.recover {
      case t: Throwable => Exceptional(t)
    }.getOrElse(
      Exceptional(new RuntimeException("No test outcome present"))
    )

    app.stop()

    outcome
  }

  describe("Status HTTP API") {

    implicit val patienceConfig =
      PatienceConfig(timeout = scaled(Span(5, Seconds)), interval = scaled(Span(100, Millis)))

    it("should return OK status when the server up and running") {
      eventually {
        val response = Http("http://localhost:8888/status").asString

        response.code shouldEqual 200
        response.body shouldEqual "{\"status\":\"OK\"}"
      }
    }
  }
} 
开发者ID:denyago,项目名称:yet-another-simple-chat,代码行数:42,代码来源:StatusApiSpec.scala

示例12: StandaloneAdminHttpServerSpec

//设置package包名称以及导入依赖的类
package com.pagerduty.scheduler.admin.http

import com.pagerduty.metrics.NullMetrics
import com.pagerduty.scheduler._
import com.pagerduty.scheduler.admin.http.standalone.AdminHttpServer
import com.pagerduty.scheduler.model.Task
import com.pagerduty.scheduler.specutil.CassandraIntegrationSpec
import com.typesafe.config.ConfigFactory
import org.scalatest.{BeforeAndAfterAll, Matchers}

import scalaj.http.Http

class StandaloneAdminHttpServerSpec extends CassandraIntegrationSpec with Matchers with BeforeAndAfterAll {

  val config = ConfigFactory.load()

  def taskRunner(task: Task): Unit = {}

  val port = 4011
  val namespace = "scheduler"
  val aSettings = Settings(port, namespace)
  val settings = SchedulerSettings(ConfigFactory.load())
  val executorFactory = SchedulerIntegrationSpecBase.simpleTestExecutorFactory(taskRunner)
  val scheduler = new SchedulerImpl(settings, config, NullMetrics, cluster, keyspace, executorFactory)()
  val adminServer = new AdminHttpServer(aSettings, scheduler.adminService)
  adminServer.start()

  override def afterAll(): Unit = {
    adminServer.stop()
  }

  "The standalone admin HTTP server" - {

    "should initialize properly and serve requests" in {
      val response = Http(s"http://localhost:$port/$namespace/api/v1/status").asString
      response.code should equal(200)
      response.body should include("Scheduler Admin API is running")
    }
  }
} 
开发者ID:PagerDuty,项目名称:scheduler,代码行数:41,代码来源:StandaloneAdminHttpServerSpec.scala

示例13: LanguageDetectorAuth

//设置package包名称以及导入依赖的类
package com.microsoft.partnercatalyst.fortis.spark.transforms.language

import net.liftweb.json

import scalaj.http.Http

case class LanguageDetectorAuth(key: String, apiUrlBase: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class LanguageDetector(
  auth: LanguageDetectorAuth,
  minConfidence: Double = 0.75
) extends Serializable {

  def detectLanguage(text: String): Option[String] = {
    if (text.isEmpty) {
      return None
    }

    val textId = "0"
    val requestBody = buildRequestBody(text, textId)
    val response = callCognitiveServices(requestBody)
    parseResponse(response, textId)
  }

  protected def callCognitiveServices(requestBody: String): String = {
    Http(s"${auth.apiUrlBase}/text/analytics/v2.0/languages")
      .params(
        "numberOfLanguagesToDetect" -> "1")
      .headers(
        "Content-Type" -> "application/json",
        "Ocp-Apim-Subscription-Key" -> auth.key)
      .timeout(connTimeoutMs = 2500, readTimeoutMs = 2500)
      .postData(requestBody)
      .asString
      .body
  }

  protected def buildRequestBody(text: String, textId: String): String = {
    implicit val formats = json.DefaultFormats
    val requestBody = dto.JsonLanguageDetectionRequest(documents = List(dto.JsonLanguageDetectionRequestItem(id = textId, text = text)))
    json.compactRender(json.Extraction.decompose(requestBody))
  }

  protected def parseResponse(apiResponse: String, textId: String): Option[String] = {
    implicit val formats = json.DefaultFormats
    val response = json.parse(apiResponse).extract[dto.JsonLanguageDetectionResponse]
    val language = response.documents.find(_.id == textId).map(_.detectedLanguages).getOrElse(List()).headOption

    if (language.isEmpty || language.get.score < minConfidence) {
      None
    } else {
      Some(language.get.iso6391Name)
    }
  }
} 
开发者ID:CatalystCode,项目名称:project-fortis-spark,代码行数:57,代码来源:LanguageDetector.scala

示例14: SentimentDetectorAuth

//设置package包名称以及导入依赖的类
package com.microsoft.partnercatalyst.fortis.spark.transforms.sentiment

import net.liftweb.json

import scalaj.http.Http

case class SentimentDetectorAuth(key: String, apiUrlBase: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class CognitiveServicesSentimentDetector(
  language: String,
  auth: SentimentDetectorAuth
) extends DetectsSentiment {

  def detectSentiment(text: String): Option[Double] = {
    val textId = "0"
    val requestBody = buildRequestBody(text, textId)
    val response = callCognitiveServices(requestBody)
    parseResponse(response, textId)
  }

  protected def callCognitiveServices(requestBody: String): String = {
    Http(s"${auth.apiUrlBase}/text/analytics/v2.0/sentiment")
      .headers(
        "Content-Type" -> "application/json",
        "Ocp-Apim-Subscription-Key" -> auth.key)
      .postData(requestBody)
      .asString
      .body
  }

  protected def buildRequestBody(text: String, textId: String): String = {
    implicit val formats = json.DefaultFormats
    val requestBody = dto.JsonSentimentDetectionRequest(documents = List(dto.JsonSentimentDetectionRequestItem(
      id = textId,
      language = language,
      text = text)))
    json.compactRender(json.Extraction.decompose(requestBody))
  }

  protected def parseResponse(apiResponse: String, textId: String): Option[Double] = {
    implicit val formats = json.DefaultFormats
    val response = json.parse(apiResponse).extract[dto.JsonSentimentDetectionResponse]
    if (response.errors.exists(_.id == textId)) {
      None
    } else {
      response.documents.find(_.id == textId).map(_.score)
    }
  }
} 
开发者ID:CatalystCode,项目名称:project-fortis-spark,代码行数:51,代码来源:CognitiveServicesSentimentDetector.scala

示例15: ImageAnalysisAuth

//设置package包名称以及导入依赖的类
package com.microsoft.partnercatalyst.fortis.spark.transforms.image

import com.microsoft.partnercatalyst.fortis.spark.dto.{Analysis, Location, Tag}
import com.microsoft.partnercatalyst.fortis.spark.transforms.locations.client.FeatureServiceClient
import net.liftweb.json

import scalaj.http.Http

case class ImageAnalysisAuth(key: String, apiUrlBase: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class ImageAnalyzer(auth: ImageAnalysisAuth, featureServiceClient: FeatureServiceClient) extends Serializable {
  def analyze(imageUrl: String): Analysis = {
    val requestBody = buildRequestBody(imageUrl)
    val response = callCognitiveServices(requestBody)
    parseResponse(response)
  }

  protected def callCognitiveServices(requestBody: String): String = {
    Http(s"${auth.apiUrlBase}/vision/v1.0/analyze")
      .params(
        "details" -> "Celebrities,Landmarks",
        "visualFeatures" -> "Categories,Tags,Description,Faces")
      .headers(
        "Content-Type" -> "application/json",
        "Ocp-Apim-Subscription-Key" -> auth.key)
      .postData(requestBody)
      .asString
      .body
  }

  protected def buildRequestBody(imageUrl: String): String = {
    implicit val formats = json.DefaultFormats
    val requestBody = dto.JsonImageAnalysisRequest(url = imageUrl)
    json.compactRender(json.Extraction.decompose(requestBody))
  }

  protected def parseResponse(apiResponse: String): Analysis = {
    implicit val formats = json.DefaultFormats
    val response = json.parse(apiResponse).extract[dto.JsonImageAnalysisResponse]

    Analysis(
      summary = response.description.captions.map(x => x.text).headOption,
      entities = response.categories.flatMap(_.detail.flatMap(_.celebrities)).flatten(x => x).map(x => Tag(x.name, Some(x.confidence))),
      locations = landmarksToLocations(response.categories.flatMap(_.detail.flatMap(_.landmarks)).flatten(x => x)).toList,
      keywords = response.tags.map(x => Tag(x.name, Some(x.confidence))))
  }

  protected def landmarksToLocations(marks: Iterable[dto.JsonImageLandmark]): Iterable[Location] = {
    val landmarks = marks.map(landmark => landmark.name -> landmark.confidence).toMap
    val features = featureServiceClient.name(landmarks.keys)
    features.map(loc => Location(wofId = loc.id, confidence = landmarks.get(loc.name)))
  }
} 
开发者ID:CatalystCode,项目名称:project-fortis-spark,代码行数:55,代码来源:ImageAnalyzer.scala


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