-
Notifications
You must be signed in to change notification settings - Fork 3
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
Ergonomics (accessing environment in rate limit instances) #6
Comments
Hi @hasufell, thanks for the feedback about this! I can see how being able to access the Servant context would be useful and it is a pretty easy change. I have pushed 81a3fd5 and 8dd878b which implement this for I have also extended the README and the tests with an example that consumes a value from the context in a Let me know if these changes work for you. |
@mbg I tried something similar I think and I got stuck with that because now in order to make useful use of the Context, I need to specialize the |
Maybe I should expand a little more:
|
The In your case, it seems that you wouldn't really need a Instances of the |
Yes, my key type is What I mean is that my |
I do need a backend. There's just additional logic that will return The only workaround I see is moving that logic into the Backend itself and abusing
|
It's a bit tricky for me to visualise what exactly your code looks like at the moment, sorry. My current understanding is that you have something along the lines of this: instance HasContextEntry ctx JWTData => HasRateLimitStrategy ctx CustomStrategy where
strategyValue ctx backend getKey = case getContextEntry ctx of
JWTDataCtr0 k0 -> ...
JWTDataCtr1 k1 -> ... Where Would parameterising I would definitely try to avoid moving things into a |
Yes, this does work: class HasRateLimitStrategy (ctx :: [Type]) strategy key where
strategyValue ::
Context ctx -> Backend key -> (Request -> IO key) -> Strategy |
Thanks, I guess this needs a major PVP version bump, because we broken the class API. |
Yes indeed. I am hoping to include the changes to |
I'm trying to access application environment specifically in the
HasRateLimitStrategy
instance (but also possibly in theHasRateLimitPolicy
) one.To access application environment there are two ways in servant afais:
Context
which is specifically for the combinatorsI tried to shoehorn both into the instances, but I think it's impossible. The 3rd possibility (which is what sadly the README even suggests) is using
unsafePerformIO
.In the end I went with overlapping the
HasServer (RateLimit foo bar :> api) ctx
instance, copy pasting and adjusting the internal code, then adding my ownHasContextEntry
constraint on top. This works ok-ish, but overall... this is rather unergonomic and hard to figure out.If you have better suggestions, please share. Otherwise I suggest to add a more complicated example to the readme that does this.
The text was updated successfully, but these errors were encountered: