You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductExportRepository.scala 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package de.codingchallenge.repositories
  2. import akka.actor.ActorSystem
  3. import akka.http.scaladsl.Http
  4. import akka.http.scaladsl.model.HttpEntity.Chunked
  5. import akka.http.scaladsl.model._
  6. import akka.stream.scaladsl.{Flow, Source}
  7. import akka.util.ByteString
  8. import de.codingchallenge.csv.CsvOps._
  9. import de.codingchallenge.models.ProductExport
  10. import scala.concurrent.Future
  11. class ProductExportRepository(actorSystem: ActorSystem) {
  12. val headerLine = "produktId|name|beschreibung|preis|summeBestand\n"
  13. val baseUrl = "http://localhost:8080"
  14. def add(p: Source[ProductExport, _], articlesSize: Int): Future[HttpResponse] = {
  15. // it did not work with charset information
  16. val entity = Chunked.fromData(
  17. ContentType.WithMissingCharset(MediaTypes.`text/csv`),
  18. p.via(csvFlow)
  19. .prepend(Source.single(ByteString(headerLine)))
  20. )
  21. Http()(actorSystem)
  22. .singleRequest(
  23. HttpRequest(
  24. method = HttpMethods.PUT,
  25. uri = s"$baseUrl/products/$articlesSize",
  26. entity = entity))
  27. }
  28. private val csvFlow: Flow[ProductExport, ByteString, Any] =
  29. Flow.fromFunction(p => ByteString(p.toCsvLine))
  30. }