Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint-Specific Middleware #614

Merged

Conversation

lewisjkl
Copy link
Contributor

Adds the ability to provide a middleware that can provide HttpApp[F] => HttpApp[F] conversions based on the data contained in the service and endpoint (such as Hints). This will allow more idiomatic implementations of things such as authentication/authorization and other mechanisms that rely on inspecting traits on a per-endpoint basis.

More docs to come as part of the PR.

@lewisjkl
Copy link
Contributor Author

@Baccata if you are happy with the contents of this PR, then I will move forward with adding tests in the project and docs for the microsite. Want to make sure the interface is solid before I move on to that though. Let me know!

@lewisjkl
Copy link
Contributor Author

Ah yeah... cats.effect.kernel.Unique wasn't a thing until CE3 right? I am going to need to investigate how we may be able to implement this with the older version of http4s + CE2. Maybe just some compat classes that handle the differences.. hopefully.

Copy link
Member

@kubukoz kubukoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool! I have a couple nitpicks as always.

@lewisjkl lewisjkl marked this pull request as ready for review November 21, 2022 21:10
@lewisjkl
Copy link
Contributor Author

Alright, I addressed comments, added tests, and added a guide to the docs. Give me your feedback on what needs to improve/change/etc so we can get this merged 🚀

): HttpApp[IO] => HttpApp[IO] = { inputApp =>
HttpApp[IO] { request =>
val hasTag: (Hints, String) => Boolean = (hints, tagName) =>
hints.get[smithy.api.Tags].exists(_.value.contains(tagName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first time I'm seeing anyone use this hint for anything in Scala

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah yeah it was just a really easy way I could think to test that the hints were being sent through properly. I don't have any real use cases for it atm. Though I guess it could come in handy for some arbitrary middleware thing 🤷

@kubukoz
Copy link
Member

kubukoz commented Nov 21, 2022

amazing stuff :)

)(implicit effect: EffectCompat[F]) extends SmithyHttp4sClientEndpoint[F, Op, I, E, O, SI, SO] {
// format: on

private val transformedClient: Client[F] =
Client.fromHttpApp[F](middleware(client.toHttpApp))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so unfortunately, I don't think we should rely on fromHttpApp in non-test code, because it's a non-trivial transformation and I'm somewhat scared that we'd be giving a footgun to the users, as it's easy to mess up the request lifecycle in client-side middleware.

Essentially the main problem is that the HttpApp abstraction should work against Resource[F, *] instead of F. I believe there's an issue somewhere in http4s, but the impact will be quite heavy on the ecosystem so I think they're waiting for further stabilisation before pulling the trigger on that.

So, we need to require the middleware to be provided as a Client[F] => Client[F], which unfortunately implies creating another construct to echo the EndpointSpecificMiddleware, which may mean that we'd have to rename that middleware to `EndpointSpecificServerMiddleware which is a bit of a mouthful.

How about ServerEndpointMiddleware and ClientEndpointMiddleware ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 75313f5

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Baccata I think fromHttpApp is not even as bad, the real evil is toHttpApp. But I agree, the main usecase is tests and we should probably stick to that

@Baccata Baccata merged commit c76de5f into disneystreaming:series/0.17 Nov 23, 2022
@Baccata Baccata added this to the 0.17.0 milestone Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants