Understanding Spray: Demistification

て烟熏妆下的殇ゞ 提交于 2019-12-24 14:13:27

问题


Can someone help me get a hold on the spray code to better understand how to spray ? I'm the context of sending a file as multipart data.

The code that was suggested to me is :

import akka.actor.ActorSystem
import spray.client.pipelining._
import spray.http.{MediaTypes, BodyPart, MultipartFormData}

object UploadFileExample extends App {
  implicit val system = ActorSystem("simple-spray-client")
  import system.dispatcher // execution context for futures below

  val pipeline = sendReceive
  val payload = MultipartFormData(Seq(BodyPart(new File("/tmp/test.pdf"), "datafile", MediaTypes.`application/pdf`)))
  val request =
    Post("http://localhost:8080/file-upload", payload)

  pipeline(request).onComplete { res =>
    println(res)
    system.shutdown()
  }
}

Which is fine and works of course.

However i want to understand what is under the hood so i can do things by myself:

Here are the confusion coming from this code:

BodyPart(new File("/tmp/test.pdf"), "datafile", MediaTypes.`application/pdf`)

is the first issue, indeed, BodyPart only has one apply method that is closely match:

def apply(file: File, fieldName: String, contentType: ContentType): BodyPart = 
 apply(HttpEntity(contentType, HttpData(file)), fieldName, Map.empty.updated("filename", file.getName)) 

it takes a contentType and not a MediaType.

However i found in contentType

object ContentType { 

  private[http] case object `; charset=` extends SingletonValueRenderable 

  def apply(mediaType: MediaType, charset: HttpCharset): ContentType = apply(mediaType, Some(chars  et)) 

  implicit def apply(mediaType: MediaType): ContentType = apply(mediaType, None) }

But ContentType is not in the scope of the Main (see first code at the top.) Hence i don't know where that implicit conversion comes from?

Then the last thing that i do not understand here is

val payload = MultipartFormData(Seq(BodyPart(new File("/tmp/test.pdf"),
"datafile", MediaTypes.`application/pdf`)))
Post("http://localhost:8080/file-upload", payload)

The problem here is that, it is based on the RequestBuilding (as can be found in the RequestBuilding Source)

val Post = new RequestBuilder(POST)

with an object that contain the apply method:

def apply[T: Marshaller](uri: String, content: Option[T]): HttpRequest = apply(Uri(uri), content)
....
....
def apply[T: Marshaller](uri: Uri, content: Option[T]): HttpRequest = {
      val ctx = new CollectingMarshallingContext {
        override def startChunkedMessage(entity: HttpEntity, ack: Option[Any],
                                         headers: Seq[HttpHeader])(implicit sender: ActorRef) =
          sys.error("RequestBuilding with marshallers producing chunked requests is not supported")
      }
      content match {
        case None ⇒ HttpRequest(method, uri)
        case Some(value) ⇒ marshalToEntityAndHeaders(value, ctx) match {
          case Right((entity, headers)) ⇒ HttpRequest(method, uri, headers.toList, entity)
          case Left(error)              ⇒ throw error
        }
      }
    }

MultiPartFormData is not a Marshaler , hence i do not understand how does it works.

Can someone help me figure that out ?

Many thanks,

M

来源:https://stackoverflow.com/questions/28466280/understanding-spray-demistification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!