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


Scala ContentType类代码示例

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


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

示例1: exportReply

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

import java.io.StringWriter

import akka.http.scaladsl.model.{ContentType, HttpEntity, HttpResponse}
import akka.util.ByteString
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.common.TextFormat
import io.prometheus.client.hotspot.DefaultExports


  def exportReply: HttpResponse = {
    val writer = new StringWriter
    TextFormat.write004(writer, registry.metricFamilySamples())
    val string = writer.toString

    HttpResponse(entity = HttpEntity.Strict(HttpExport.contentType, ByteString(string)))
  }
}

object HttpExport {
  val contentType: ContentType = ContentType.parse(TextFormat.CONTENT_TYPE_004).right.get
} 
开发者ID:Norwae,项目名称:ignifera,代码行数:24,代码来源:HttpExport.scala

示例2: SuccessfulOut

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

import akka.http.documenteddsl.documentation.RouteDocumentation
import akka.http.scaladsl.model.{ContentType, StatusCode, StatusCodes}
import akka.http.scaladsl.server.Directive
import org.coursera.autoschema.AutoSchema
import play.api.libs.json.{JsValue, Writes}

import scala.reflect.runtime.{universe => ru}

trait UnmarshallingDDirectives {

  final class SuccessfulOut[T : ru.TypeTag](status: StatusCode, example: Option[JsValue]) extends DDirective0 {
    def describe(w: RouteDocumentation)(implicit as: AutoSchema): RouteDocumentation = w.outSuccess[T](status.intValue, example)
    def delegate = Directive.Empty
  }

  final class ErrorOut(status: StatusCode, contentType: Option[String], description: Option[String]) extends DDirective0 {
    def describe(w: RouteDocumentation)(implicit as: AutoSchema): RouteDocumentation = w.outError(status, contentType, description)
    def delegate = Directive.Empty
  }

  object Out {
    def apply[T : ru.TypeTag]: SuccessfulOut[T] = new SuccessfulOut(StatusCodes.OK, None)
    def apply[T : ru.TypeTag](status: StatusCode): SuccessfulOut[T] = new SuccessfulOut(status, None)
    def apply[T : ru.TypeTag](example: T)(implicit writes: Writes[T]): SuccessfulOut[T] = new SuccessfulOut(StatusCodes.OK, Some(writes writes example))
    def apply[T : ru.TypeTag](status: StatusCode, example: T)(implicit writes: Writes[T]): SuccessfulOut[T] = new SuccessfulOut(status, Some(writes writes example))
    def success(status: StatusCode): SuccessfulOut[Nothing] = new SuccessfulOut(status, None)
    def error(status: StatusCode): ErrorOut = new ErrorOut(status, None, None)
    def apply(status: StatusCode, description: String): ErrorOut = new ErrorOut(status, None, Some(description))
    def apply(status: StatusCode, contentType: ContentType, description: String): ErrorOut = new ErrorOut(status, Some(contentType.toString()), Some(description))
  }

}

object UnmarshallingDDirectives extends UnmarshallingDDirectives 
开发者ID:evolution-gaming,项目名称:akka-http-documenteddsl,代码行数:37,代码来源:UnmarshallingDDirectives.scala

示例3: 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

示例4: GetRetreiveRoute

//设置package包名称以及导入依赖的类
package org.nephtys.keepaseat.internal

import akka.http.scaladsl.model.{ContentType, HttpEntity, HttpResponse, MediaTypes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import org.nephtys.keepaseat.Databaseable
import org.nephtys.keepaseat.filter.XSSCleaner
import org.nephtys.keepaseat.internal.configs.{Authenticators, PasswordConfig}
import upickle.default._



class GetRetreiveRoute(implicit passwordConfig: PasswordConfig, database: Databaseable,
                       xssCleaner: XSSCleaner) {

  def receivePathWithoutSlashes = """events"""

  def receivePath: String = "/" + receivePathWithoutSlashes

  def extractRoute: Route = path(receivePathWithoutSlashes) {
      Authenticators.BasicAuthOrPass(passwordConfig, onlySuperusers = false) { () =>
        parameters('from.as[Long], 'to.as[Long]) { (from, to) => {
          onSuccess(database.retrieve(from, to)) { a => {
            complete {
              HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`application/json`), write(a.sortBy(_.elements.map(_.from).min).map(_.cleanHTML))))
            }
          }
          }
        }
        }
      }
  }


} 
开发者ID:n3phtys,项目名称:keep-a-seat,代码行数:36,代码来源:GetRetreiveRoute.scala

示例5: 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

示例6: healthCheck

//设置package包名称以及导入依赖的类
package io.pixelart.ambry.client.infrastructure.service

import java.nio.file.{ Files, Path }
import akka.http.scaladsl.model.ContentType
import akka.stream.IOResult
import akka.stream.scaladsl.{ Source, FileIO }
import com.typesafe.scalalogging.StrictLogging
import io.pixelart.ambry.client.application.{ ActorImplicits, AbstractAmbryClientService }
import io.pixelart.ambry.client.domain.model.httpModel._
import io.pixelart.ambry.client.infrastructure.adapter.{ AmbryClient }
import scala.concurrent.Future


protected[client] trait AmbryService extends AbstractAmbryClientService with StrictLogging with ActorImplicits {
  this: AmbryClient =>

  private[client] val ambryUri: AmbryUri

  override def healthCheck: Future[AmbryHealthStatusResponse] =
    healthCheckRequest

  override def getFileProperty(ambryId: AmbryId): Future[AmbryBlobInfoResponse] =
    getBlobInfoRequest(ambryId)

  //todo: override def getFileUserMetadata(ambryId: AmbryId): AmbryUMResponse = ???

  override def getFile(ambryId: AmbryId): Future[AmbryGetBlobResponse] =
    getBlobRequest(ambryId)

  override def getFile(ambryId: AmbryId, localPath: Path): Future[IOResult] =
    getBlobRequest(ambryId)
      .flatMap { result =>
        result
          .blob
          .runWith(FileIO.toPath(localPath))
      }

  //todo: ttl to DateTime
  override def postFileFromPath(
    localPath: Path,
    serviceId: AmbryServiceId,
    contentType: ContentType,
    ttl: Long = -1,
    prvt: Boolean = false,
    ownerId: AmbryOwnerId
  ): Future[AmbryBlobUploadResponse] = {

    val fileSource = FileIO.fromPath(localPath)
    val size = Files.size(localPath)
    uploadBlobRequest(UploadBlobRequestData(fileSource, size, serviceId, contentType, ttl, prvt, ownerId))
  }

  override def postFile(uploadBlobRequestData: UploadBlobRequestData): Future[AmbryBlobUploadResponse] =
    uploadBlobRequest(uploadBlobRequestData)

  override def deleteFile(ambryId: AmbryId): Future[Boolean] =
    deleteBlobRequest(ambryId)
} 
开发者ID:OutOfAxis,项目名称:ambry-client-akkahttp,代码行数:59,代码来源:AmbryService.scala


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