Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dfm/aiohttp_spotify
An async Python interface to the Spotify API using aiohttp
https://github.com/dfm/aiohttp_spotify
aiohttp async-python-interface asyncio oauth oauth2 python spotify-api
Last synced: 23 days ago
JSON representation
An async Python interface to the Spotify API using aiohttp
- Host: GitHub
- URL: https://github.com/dfm/aiohttp_spotify
- Owner: dfm
- License: mit
- Created: 2020-03-26T02:25:09.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-06-12T18:15:33.000Z (over 4 years ago)
- Last Synced: 2024-09-17T12:58:36.523Z (about 2 months ago)
- Topics: aiohttp, async-python-interface, asyncio, oauth, oauth2, python, spotify-api
- Language: Python
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
An async Python interface to the Spotify API using [aiohttp](https://docs.aiohttp.org).
*Note: This is alpha software. Use at your own risk.*
Installation
------------To install, use pip:
```bash
python -m pip install aiohttp_spotify
```It's best if you also install and use [aiohttp-session](https://github.com/aio-libs/aiohttp-session).
Usage
-----To add the OAuth flow to your app:
```python
from aiohttp import web
import aiohttp_spotifyasync def handle_auth(request: web.Request, auth: aiohttp_spotify.SpotifyAuth):
# Store the `auth` object for use laterapp = web.Application()
app["spotify_app"] = aiohttp_spotify.spotify_app(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
handle_auth=handle_auth,
)
app.add_subapp("/spotify", app["spotify_app"])
```Then you can make calls to the API as follows:
```python
from aiohttp import ClientSessionasync def call_api(request: web.Request) -> web.Response:
async with ClientSession() as session:
response = app["spotify_app"]["spotify_client"].request(
session, auth, "/me"
)
# The auth object will be updated as tokens expire so you should
# update this however you have it stored:
if response.auth_changed:
await handle_auth(request, response.auth)
return web.json_request(response.json())
```where `auth` is the `SpotifyAuth` object from above.
Take a look at [the demo directory](/demo) for a more complete example.