
spice.http.content.FormDataContent.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spice-core_3 Show documentation
Show all versions of spice-core_3 Show documentation
Core functionality leveraged and shared by most other sub-projects of YouI.
package spice.http.content
import java.io.File
import spice.http.Headers
import spice.net.ContentType
case class FormDataContent(data: List[FormData]) extends Content {
override def length: Long = -1
override def lastModified: Long = -1
override def contentType: ContentType = ContentType.`multipart/form-data`
override def withContentType(contentType: ContentType): Content = this
override def withLastModified(lastModified: Long): Content = this
def fileOption(key: String): Option[FormDataEntry.FileEntry] = data.find(_.key == key).map(_.entries.head.asInstanceOf[FormDataEntry.FileEntry])
def stringOption(key: String): Option[FormDataEntry.StringEntry] = data.find(_.key == key).map(_.entries.head.asInstanceOf[FormDataEntry.StringEntry])
def file(key: String): FormDataEntry.FileEntry = fileOption(key).getOrElse(throw new RuntimeException(s"Not found: $key in $this."))
def string(key: String): FormDataEntry.StringEntry = stringOption(key).getOrElse(throw new RuntimeException(s"Not found: $key in $this."))
def withFile(key: String, fileName: String, file: File, headers: Headers = Headers.empty): FormDataContent = {
val entry = FormDataEntry.FileEntry(fileName, file, headers)
withEntry(key, entry)
}
def withString(key: String, value: String, headers: Headers = Headers.empty): FormDataContent = {
val entry = FormDataEntry.StringEntry(value, headers)
withEntry(key, entry)
}
def withEntry(key: String, entry: FormDataEntry): FormDataContent = {
val formData = data.find(_.key == key).getOrElse(FormData(key, Nil))
val updated = formData.copy(entries = formData.entries ::: List(entry))
copy(data = data.filterNot(_.key == key) ::: List(updated))
}
override def toString: String = s"FormDataContent(${data.map(_.key).mkString(", ")})"
override def asString: String = toString
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy