hikari icon indicating copy to clipboard operation
hikari copied to clipboard

Add OAuthCredentialsStrategy

Open theonlydoublee opened this issue 1 year ago • 8 comments

Summary

Add OAuthCredentialsStrategy to hikari\impl\rest.py similar to ClientCredentialsStrategy to allow simple OAuth2 token generation and refreshing.

I have used this code on my own project and it works from my testing.

I do not know how to make the tests for this except by copying from the ones for ClientCredentialsStrategy and I tried but I do not know how to use the mock stuff. I am also not sure which tests I will need. Don't be afraid to be harsh giving feedback on what to do or I did.

Idea from: https://discord.com/channels/574921006817476608/1088176194874257428

Checklist

  • [x] I have run nox and all the pipelines have passed.
  • [x] I have made unittests according to the code I have added/modified/deleted.

Related issues

  • None

theonlydoublee avatar Mar 24 '23 01:03 theonlydoublee

I ran into an issue with making the tests for OAuthCredentialsStrategy and the last tests I have done are getting the error TypeError: object Mock can't be used in 'await' expression. The code is the only tests that are erroring and all for that same error. Altered from ClientCredentialsStrategy

Code: https://pastebin.com/pdFk7Fxf

theonlydoublee avatar Mar 24 '23 17:03 theonlydoublee

Use a mock.AsyncMock instead of mock.Mock

Jonxslays avatar Mar 24 '23 19:03 Jonxslays

Getting an error of pytest.PytestUnraisableExceptionWarning: Exception ignored in: <coroutine object AsyncMockMixin._execute_mock_call at 0x00000267A82C7A70>

Full Nox Traceback: https://pastebin.com/f6qkWLkU Failing Test Code: https://pastebin.com/MzV1D7BX Updated Class: https://pastebin.com/TbHKr2Ad

theonlydoublee avatar Mar 24 '23 21:03 theonlydoublee

Thought I fixed those typing errors for mypy. Will fix when back home

theonlydoublee avatar Mar 24 '23 22:03 theonlydoublee

Everything should be ready

theonlydoublee avatar Mar 25 '23 02:03 theonlydoublee

I don't know how I feel about this tbh. The reason this wasn't implemented in the first place was since it felt like having this implemented as a standard thing would just be encouraging bad behaviour (which is still the case).

When handling Oauth2 tokens you'd really want to be storing the current version of the token (likely in a database) to persist it between bot restarts and only refreshing it after it's expired but this doesn't really allow for that.

Also I'm not sure what the use case is for this.

FasterSpeeding avatar Apr 01 '23 03:04 FasterSpeeding

I was thinking of using it for a restapp for interacting with user's information. If this isn't used, I'll use it for my personal project without it being in hikari itself.

theonlydoublee avatar Apr 01 '23 03:04 theonlydoublee

When handling Oauth2 tokens you'd really want to be storing the current version of the token (likely in a database) to persist it between bot restarts and only refreshing it after it's expired but this doesn't really allow for that.

Could that be fixed by just allowing passing the token and refresh token to the strategy?

davfsa avatar Apr 01 '23 11:04 davfsa