Git Product home page Git Product logo

Comments (13)

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Tuesday Jul 05, 2016 at 10:59 GMT


Thanks for the ticket, I agree it's better to provide a directive whcih does this "safely" than assuming people will do the right thing (and get 20 "almost correct" impls ;-))

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Tuesday Jul 05, 2016 at 11:00 GMT


PRs very welcome to this one, should be rather simple :)

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by Hawstein
Tuesday Jul 12, 2016 at 17:32 GMT


@Joe-Edwards @ktoso I will work on this if you don't mind. And AFAICS, just adding a extractStrictRequestEntity method to BasicDirectives solves this issue. Correct me if I miss something.

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by Joe-Edwards
Tuesday Jul 12, 2016 at 18:36 GMT


Go for it. If it's any help, what I'd come up with so far was:

  def extractStrictEntity(timeout: FiniteDuration): Directive1[HttpEntity.Strict] = {
    Directive { inner => ctx =>
      ctx.request.entity match {
        case strict: HttpEntity.Strict =>
          inner(Tuple1(strict))(ctx)
        case nonStrict =>
          import ctx.{executionContext, materializer}
          nonStrict.toStrict(timeout).flatMap(strict => inner(Tuple1(strict))(ctx.mapRequest(_.copy(entity = strict))))
      }
    }
  }

I'm not sure if the optimization in the strict case is really necessary (perhaps using FastFuture will make it redundant anyway?)

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Tuesday Jul 12, 2016 at 21:08 GMT


The optimisation is not needed, toStrict does the right thing (returns "this") if the entity is Strict already.

I was more thinking of making a directive that works via mapRequest, i.e. it transforms the entity transparently.

strictEntity { // toStricts, but we can do various things inside
  extractDataBytes { // works since strict already
    // .. 
  } ~ get { 
    extractRequest { r => r.entity.dataBytes .... } 
  }
}

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Tuesday Jul 12, 2016 at 21:09 GMT


For inspiration look at mapRequest

val route =
      mapRequest(transformToPostRequest) {
        extractRequest { req =>
          complete(s"The request method was ${req.method.name}")
        }
      }

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by Hawstein
Wednesday Jul 13, 2016 at 20:00 GMT


@ktoso just make a PR for this issue.

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by Joe-Edwards
Thursday Jul 14, 2016 at 09:42 GMT


The benefit of extracting the entity is that you get don't need to match/cast it. Usually you want a strict entity so you can get the data explicitly:

strictEntity(...) {
  extractRequest { request =>
    val entity: HttpEntity = request.entity // don't know it's strict yet :(
    entity match {
      case strict: HttpEntity.Strict => strict.data // now we do, but have an non-exhaustive match :(
    }
  }
}

vs

extractStrictEntity(...) { strict =>
  strict.data // immediately available in a type safe way
}

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Thursday Jul 14, 2016 at 09:59 GMT


Sure, however it does not compose as nicely.
My proposal composes more nicely though, and is beneficial for other directives as well (e.g. formFields which people keep getting wrong), instead of just this single case.

I stand by the need of doing a directive like I proposed, and optionally adding a extractStrictEntity.

from akka-http.

akka-ci avatar akka-ci commented on August 26, 2024

Comment by ktoso
Friday Jul 22, 2016 at 09:33 GMT


Thanks a lot @Hawstein for implementing this feature :-)

from akka-http.

catalin-ursachi avatar catalin-ursachi commented on August 26, 2024

Was this not already implemented by akka/akka#20953?

from akka-http.

ktoso avatar ktoso commented on August 26, 2024

Correct, thanks a lot for noticing @catalin-ursachi !

from akka-http.

ktoso avatar ktoso commented on August 26, 2024

Duplicate of akka/akka#20881 (already solved)

from akka-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.