Upgrade to Pro — share decks privately, control downloads, hide ads and more …

There's an API for That!

Mariatta
December 15, 2023

There's an API for That!

You might have seen bots like GitHub bots, Discord bots, Slack bots. How do these bots work? There's an API for that! You can use third-party APIs to build web apps and automations, including bots. Come learn the best practices on how to work with APIs using Python.

Mariatta

December 15, 2023
Tweet

More Decks by Mariatta

Other Decks in Programming

Transcript

  1. Also called Developer Docs Start with Tutorials if you are

    new API endpoints are listed under Resources Read the Documentation Mariatta fosstodon.org/@mariatta
  2. Also called Developer Docs Start with Tutorials if you are

    new API endpoints are listed under Resources Read the Documentation Mariatta fosstodon.org/@mariatta
  3. # Don’t do this TOKEN = “my token” # Do

    this TOKEN = os.environ[”TOKEN”] API keys or OAuth2 Keep your credentials secret!! Don’t store as plain text Don’t commit to your codebase Use environment variables Authentication Mariatta fosstodon.org/@mariatta
  4. Helper library for working with third-party APIs Also called toolkit

    or SDKs or wrappers Abstraction and simplification Hides details of request authorization, headers, signature verification Write code in your favorite language (e.g. Python) No client library? Write your own Use Swagger Client if they have OpenAPI Use Client Libraries Mariatta fosstodon.org/@mariatta
  5. Use your favorite web framework Django REST framework, Flask, FastAPI,

    aiohttp, etc Always verify incoming requests Use webhook secret Pass a specific request header The client library should handle this. If not, file a feature request! Webhooks Mariatta fosstodon.org/@mariatta
  6. Dev Docs: https://docs.github.com/en/developers Authenticate using Personal Access Token, or OAuth2

    Official client libraries: in Ruby, .NET, JavaScript Third Party Python library: https://gidgethub.readthedocs.io My GitHub App tutorial: https://github-app-tutorial.readthedocs.io >>> python3 -m pip install gidgethub GitHub APIs Mariatta fosstodon.org/@mariatta
  7. API for opening an issue response = await gh.post( f'/repos/{owner}/{repo}/issues',

    data={ 'title': 'Found a bug!', 'body': 'Use more emoji!', } ) GitHub APIs with gidgethub Mariatta fosstodon.org/@mariatta
  8. Webhook event handler @router.register("issue", action="opened") async def issue_opened(event, gh, *args,

    **kwargs): ... response = await gh.post( f"{issue_url}/comments", data={"body": msg}, oauth_token=installation_access_token["token"], ) GitHub APIs with gidgethub Mariatta fosstodon.org/@mariatta
  9. Mariatta fosstodon.org/@mariatta Flickr API Upload photos of aquarium/ potato garden

    using Flickr API https://github.com/Mariatta/raspberry_pi_webcam_py Source Code https://www.flickr.com/photos/134275259@N03/ Flickr Album