Coder Social home page Coder Social logo

Comments (4)

mental32 avatar mental32 commented on May 30, 2024 2

@frafra It was my intention that the codeblock I posted be combined with your one haha

would anyone like to PR this in themselves? Don't worry if not, I can add this is either way :)

from spotify.py.

mental32 avatar mental32 commented on May 30, 2024 1

Actually I wouldn't recommend that @frafra spotify.Client is made to me created once and carried around for the lifetime of the program. So creating and destroying instances on every route handler is a very expensive thing to do.

I'd suggesting using the contextvars module:

import asyncio
from contextvars import ContextVar

from aiohttp.web import AppRunner, TCPSite

CLIENT: ContextVar("spotify_client")

async def callback(request):
    client = CLIENT.get()
    user = await spotify.User.from_code(
        client=client,
        code=request.query['code'],
         redirect_uri=REDIRECT_URI,
    )
    tracks = await user.library.get_all_tracks()
    return web.HTTPFound(repr(tracks))


async def main() -> None:
    async with spotify.Client(CLIENT_ID, CLIENT_SECRET) as client:
        CLIENT.set(client)
        runner = AppRunner(app)
        await runner.setup()
        site = TCPSite(runner, 'localhost', 8080)
        await site.start()

if __name__ == "__main__":
    asyncio.run(main())

from spotify.py.

frafra avatar frafra commented on May 30, 2024

Here is an example I wrote:

#!/usr/bin/env python3

from aiohttp import web
import os
import spotify

CLIENT_ID = os.environ['CLIENT_ID']
CLIENT_SECRET = os.environ['CLIENT_SECRET']
REDIRECT_URI = os.environ['REDIRECT_URI']

async def auth(request):
    async with spotify.Client(CLIENT_ID, CLIENT_SECRET) as client:
        url = client.oauth2_url(
            redirect_uri=REDIRECT_URI,
            scopes=['user-library-read'],
        )
        return web.HTTPFound(url)

async def callback(request):
    async with spotify.Client(CLIENT_ID, CLIENT_SECRET) as client:
        user = await spotify.User.from_code(
            client=client,
            code=request.query['code'],
            redirect_uri=REDIRECT_URI,
            )
    tracks = await user.library.get_all_tracks()
    return web.HTTPFound(repr(tracks))

app = web.Application()
app.add_routes([
    web.get('/', auth),
    web.get('/callback', callback),
])

if __name__ == '__main__':
    web.run_app(app)

from spotify.py.

frafra avatar frafra commented on May 30, 2024

Using ContextVar is a good suggestion indeed :-)
The code looks incomplete to me now, as the auth part with the initial request and scopes is missing.

from spotify.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.