Skip to content

Instantly share code, notes, and snippets.

@jonoabroad
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonoabroad/a6465efe48b2825cc28a to your computer and use it in GitHub Desktop.
Save jonoabroad/a6465efe48b2825cc28a to your computer and use it in GitHub Desktop.
Suggested implementation of MultiPart for http4s
final case class Name(value:String) extends AnyVal
sealed trait Part
final case class FormData(name:Name,
content: Entity,
contentType: Option[ContentType] = None ) extends Part
final case class MultiPart(parts: Seq[Part]) {
def boundary = ??? // leaving out implementation for clarity
def header = ??? // leaving out implementation for clarity
}
/*
E.g of creating a simple multipart
*/
val txtToEntity: String => Entity = ???
val entity = txtToEntity("I Am a textual entity.")
val field1 = FormData("field1", entity)
val multiPart = MultiPart(List(field1))
/*
Usuage will depend on whether changes to EntityEncoder are possible.
Without changes to EntityEncoder :
*/
request.withHeaders(multiPart.headers).withBody(multiPart)
/*
With changes to EntityEncoder :
*/
request.withBody(multiPart)
/*
* The reason to change EntityEncoder, is that the request headers and entity body are dependent
* on the Multipart boundary, which is currently only available to the 'toEntity' method.
*
*/
@bryce-anderson
Copy link

Allowing the toEntity method to generate headers seams reasonable, in theory. I think that would be a parallel or followup issue/PR.

@jonoabroad
Copy link
Author

Makes sense. I'll try and get a PR to you for the current work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment