Red-DiscordBot/docs/framework_apikeys.rst
Vuks b45e62f354
[Core] add [p]set api list and [p]set api remove (#4370)
* add get_shared_api_keys

* add set listapi command

* fix typo

* refactor `[p]set listapi` into `[p]set api list`

* remove unnecessary check

* fix decorators

* corrected wording

* add remove_shared_api_services

* add `set api remove`

* further typo sniping

* bugsniping

* minor typo

* aaaaaaaaa

* Apply suggestions from code review

Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com>

* update api docstring to reflect new subcommands

* Apply suggestions from code review

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* update framework_apikeys.rst with new methods

* Update redbot/core/bot.py

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* rewrite get_shared_api_services into a special case of
get_shared_api_tokens

* rewrite api_list to include keys and tokens in response

* don't show secrets

* better api_remove response

* black

* remove nonexistent method

* remove unnecessary import

* fix wording

* Improve docstrings and type hints

- added overloads to help out developers a bit more
  * this wasn't necessary, but development tools work better
    with this information
- fixed type hint to use Union as now
  its return type depends on whether `service_name` is passed
- updated docstrings to contain information about the added behavior

* Use `humanize_list()` rather than `str.join()`

This is done for consistency within Red
and it makes the list have the last element joined
with `and` (or its equivalent in chosen locale)

* Use `.format()` after translation is applied

If `.format()` is used before `_()`, the search for translation is done
with the string that already has the dynamic text added,
which results in no matches.

* Add plural support

* Improve error message

Updated message to be more specific
(used phrases: "None of the services" and "had any keys set")
and a bit more nice to the user (judging word "anyway" removed)

* Improve readability of `[p]set api list`

It's a lot clearer when the sublists are indented,
especially on mobile where code blocks aren't colored at all.

New look:
https://user-images.githubusercontent.com/6032823/94614944-3bbd7c80-02a7-11eb-89e4-64bdf06c650b.png

Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com>
Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
2020-10-05 22:10:14 +02:00

78 lines
2.2 KiB
ReStructuredText

.. V3 Shared API Key Reference
===============
Shared API Keys
===============
Red has a central API key storage utilising the core bots config. This allows cog creators to add a single location to store API keys for their cogs which may be shared between other cogs.
There needs to be some consistency between cog creators when using shared API keys between cogs. To help make this easier service should be all **lowercase** and the key names should match the naming convention of the API being accessed.
Example:
Twitch has a client ID and client secret so a user should be asked to input
``[p]set api twitch client_id,1234ksdjf client_secret,1234aldlfkd``
and when accessed in the code it should be done by
.. code-block:: python
await self.bot.get_shared_api_tokens("twitch")
Each service has its own dict of key, value pairs for each required key type. If there's only one key required then a name for the key is still required for storing and accessing.
Example:
``[p]set api youtube api_key,1234ksdjf``
and when accessed in the code it should be done by
.. code-block:: python
await self.bot.get_shared_api_tokens("youtube")
***********
Basic Usage
***********
.. code-block:: python
class MyCog:
@commands.command()
async def youtube(self, ctx, user: str):
youtube_keys = await self.bot.get_shared_api_tokens("youtube")
if youtube_keys.get("api_key") is None:
return await ctx.send("The YouTube API key has not been set.")
# Use the API key to access content as you normally would
***************
Event Reference
***************
.. function:: on_red_api_tokens_update(service_name, api_tokens)
Dispatched when service's api keys are updated.
:param service_name: Name of the service.
:type service_name: :class:`str`
:param api_tokens: New Mapping of token names to tokens. This contains api tokens that weren't changed too.
:type api_tokens: Mapping[:class:`str`, :class:`str`]
*********************
Additional References
*********************
.. py:currentmodule:: redbot.core.bot
.. automethod:: Red.get_shared_api_tokens
.. automethod:: Red.set_shared_api_tokens
.. automethod:: Red.remove_shared_api_tokens
.. automethod:: Red.remove_shared_api_services