All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netaporter.precanned.StrictHttpEntityOps.scala Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
package com.netaporter.precanned

import akka.http.scaladsl.model.{ ContentType, HttpEntity }

/**
 * Adds convenient methods to Akka HTTP HttpEntity using the assumption that all the instances of it are
 * already strict.
 */
trait StrictHttpEntityOps {
  implicit class RichHttpEntity(private val inner: HttpEntity) {
    private def asStrict: HttpEntity.Strict = inner.asInstanceOf[HttpEntity.Strict]

    def asString: String = {
      val strict = asStrict
      strict.contentType match {
        case nonBinary: ContentType.NonBinary =>
          strict.data.decodeString(nonBinary.charset.nioCharset())
        case _: ContentType.WithMissingCharset =>
          strict.data.decodeString("UTF-8")
        case binary: ContentType.Binary =>
          sys.error(s"unable to read as a string entity with a binary content type $binary")
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy