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


Scala MediaType类代码示例

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


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

示例1: DynamoClientImpl

//设置package包名称以及导入依赖的类
package akka.stream.alpakka.dynamodb.impl

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.MediaType.NotCompressible
import akka.http.scaladsl.model.{ContentType, MediaType}
import akka.stream.Materializer
import akka.stream.alpakka.dynamodb.AwsOp
import akka.stream.alpakka.dynamodb.impl.AwsClient.{AwsConnect, AwsRequestMetadata}
import akka.stream.scaladsl.{Sink, Source}
import com.amazonaws.AmazonServiceException
import com.amazonaws.http.HttpResponseHandler

class DynamoClientImpl(
    val settings: DynamoSettings,
    val errorResponseHandler: HttpResponseHandler[AmazonServiceException]
)(implicit protected val system: ActorSystem, implicit protected val materializer: Materializer)
    extends AwsClient[DynamoSettings] {

  override protected val service = "dynamodb"
  override protected val defaultContentType =
    ContentType.Binary(MediaType.customBinary("application", "x-amz-json-1.0", NotCompressible))
  override protected implicit val ec = system.dispatcher

  override protected val connection: AwsConnect =
    if (settings.port == 443)
      Http().cachedHostConnectionPoolHttps[AwsRequestMetadata](settings.host)(materializer)
    else
      Http().cachedHostConnectionPool[AwsRequestMetadata](settings.host, settings.port)(materializer)

  def single(op: AwsOp) = Source.single(op).via(flow).map(_.asInstanceOf[op.B]).runWith(Sink.head)

} 
开发者ID:akka,项目名称:alpakka,代码行数:34,代码来源:DynamoClientImpl.scala

示例2: MediaTypeFormat

//设置package包名称以及导入依赖的类
package com.jatescher.layer.marshalling.v1

import akka.http.scaladsl.model.MediaType
import spray.json._

object MediaTypeFormat extends DefaultJsonProtocol {

  implicit val MediaTypeJsonFormat: JsonFormat[MediaType] = new JsonFormat[MediaType] {
    def write(mediaType: MediaType) = mediaType.toString.toJson
    def read(value: JsValue) = value match {
      case JsString(mediaTypeString) => MediaType.parse(mediaTypeString) match {
        case Right(mediaType) => mediaType
        case _ => deserializationError("Valid media type expected")
      }
      case _ => deserializationError("Media type cannot be blank")
    }
  }

} 
开发者ID:jtescher,项目名称:layer-scala,代码行数:20,代码来源:MediaTypeFormat.scala

示例3: MessageParts

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

import akka.http.scaladsl.model.{ MediaType, MediaTypes }

sealed trait MessagePart {
  val mimeType: MediaType
}

object MessageParts {

  // Simple text message part
  case class TextMessagePart(body: String) extends MessagePart {
    val mimeType = MediaTypes.`text/plain`
  }

  // Custom content message part
  case class ImageMessagePart(content: MessagePartContent) extends MessagePart {
    val mimeType = content.mimeType
  }

} 
开发者ID:jtescher,项目名称:layer-scala,代码行数:22,代码来源:MessagePart.scala

示例4: MessagePartContents

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

import akka.http.scaladsl.model.MediaType

sealed trait MessagePartContent {
  val id: String
  val mimeType: MediaType
}

object MessagePartContents {

  // Rich content message part
  case class ImageMessagePartContent(
    id: String,
    downloadUrl: String,
    expiration: String,
    refreshUrl: String,
    size: Long,
    mimeType: MediaType
  ) extends MessagePartContent

} 
开发者ID:jtescher,项目名称:layer-scala,代码行数:23,代码来源:MessagePartContent.scala

示例5: MediaTypeFormatSpec

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

import akka.http.scaladsl.model.{ MediaType, MediaTypes }
import com.jatescher.layer.marshalling.v1.MediaTypeFormat._
import org.scalatest.{ Matchers, WordSpec }
import spray.json._

class MediaTypeFormatSpec extends WordSpec with Matchers {
  val mediaTypeString = "image/png"
  val mediaType: MediaType = MediaTypes.`image/png`

  "#write" should {
    "marshall the media type as string" in {
      mediaType.toJson shouldBe mediaTypeString.toJson
    }
  }

  "#read" should {
    "unmarshall media types" in {
      JsString(mediaTypeString).convertTo[MediaType] shouldBe mediaType
    }

    "raise an exception if the media type is not a string" in {
      intercept[DeserializationException] {
        JsNumber(1).convertTo[MediaType]
      }
    }

    "raise an exception if the media type is not valid" in {
      intercept[DeserializationException] {
        JsString("non-media-type").convertTo[MediaType]
      }
    }
  }

} 
开发者ID:jtescher,项目名称:layer-scala,代码行数:37,代码来源:MediaTypeFormatSpec.scala

示例6: PrometheusService

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

import java.io.{ OutputStreamWriter, PipedInputStream, PipedOutputStream }

import scala.concurrent.{ ExecutionContext, Future }

import akka.http.scaladsl.model.{ HttpCharsets, HttpEntity, MediaType }
import akka.http.scaladsl.server.{ Directives, Route }
import akka.stream.scaladsl.StreamConverters
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.common.TextFormat

object PrometheusService extends Directives {

  lazy val prometheusTextType =
    MediaType.customWithFixedCharset("text", "plain", HttpCharsets.`UTF-8`, params = Map("version" -> "0.0.4"))

  def route(implicit executionContext: ExecutionContext): Route = {
    path("metrics") {
      complete {
        val in = new PipedInputStream
        val out = new OutputStreamWriter(new PipedOutputStream(in), HttpCharsets.`UTF-8`.value)
        val byteSource = StreamConverters.fromInputStream(() => in)
        Future {
          try {
            TextFormat.write004(out, CollectorRegistry.defaultRegistry.metricFamilySamples())
            out.flush()
          } finally {
            out.close()
          }
        }
        HttpEntity(prometheusTextType, byteSource)
      }
    }
  }
} 
开发者ID:pjfanning,项目名称:prometheus-akka-sample,代码行数:37,代码来源:PrometheusService.scala

示例7: scalaPBFromRequestUnmarshaller

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

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.MediaType.Compressible
import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity, MediaType}
import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import akka.http.scaladsl.util.FastFuture
import com.google.protobuf.CodedInputStream
import com.trueaccord.scalapb.json.JsonFormat
import com.trueaccord.scalapb.{GeneratedMessage, GeneratedMessageCompanion, Message}

import scala.concurrent.Future

trait ScalaPBMarshalling {
  private val protobufContentType = ContentType(MediaType.applicationBinary("octet-stream", Compressible, "proto"))
  private val applicationJsonContentType = ContentTypes.`application/json`

  def scalaPBFromRequestUnmarshaller[O <: GeneratedMessage with Message[O]](companion: GeneratedMessageCompanion[O]): FromEntityUnmarshaller[O] = {
    Unmarshaller.withMaterializer[HttpEntity, O](_ => implicit mat => {
      case [email protected](`applicationJsonContentType`, data) =>
        val charBuffer = Unmarshaller.bestUnmarshallingCharsetFor(entity)
        FastFuture.successful(JsonFormat.fromJsonString(data.decodeString(charBuffer.nioCharset().name()))(companion))
      case [email protected](`protobufContentType`, data) =>
        FastFuture.successful(companion.parseFrom(CodedInputStream.newInstance(data.asByteBuffer)))
      case entity =>
        Future.failed(UnsupportedContentTypeException(applicationJsonContentType, protobufContentType))
    })
  }

  implicit def scalaPBToEntityMarshaller[U <: GeneratedMessage]: ToEntityMarshaller[U] = {
    def jsonMarshaller(): ToEntityMarshaller[U] = {
      val contentType = applicationJsonContentType
      Marshaller.withFixedContentType(contentType) { value =>
        HttpEntity(contentType, JsonFormat.toJsonString(value))
      }
    }

    def protobufMarshaller(): ToEntityMarshaller[U] = {
      Marshaller.withFixedContentType(protobufContentType) { value =>
        HttpEntity(protobufContentType, value.toByteArray)
      }
    }

    Marshaller.oneOf(jsonMarshaller(), protobufMarshaller())
  }

} 
开发者ID:kalamara,项目名称:akka-cassandra-hazelcast-cluster,代码行数:49,代码来源:ScalaPBMarshalling.scala


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