本文整理汇总了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
}
示例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
示例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)
}
示例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))))
}
}
}
}
}
}
}
}
示例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())
}
}
示例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)
}