コード例 #1
0
ファイル: conftest.py プロジェクト: dfm/aiohttp_spotify
def client(loop, aiohttp_client, aiohttp_unused_port):
    port = aiohttp_unused_port()
    api_url = f"http://localhost:{port}/api"

    client_id = secrets.token_urlsafe()
    client_secret = secrets.token_urlsafe()

    app = web.Application()

    app["api_app"] = api_app = mock_api_app(client_id, client_secret,
                                            "/spotify/callback")

    app["spotify_app"] = spotify_app = aiohttp_spotify.spotify_app(
        client_id=client_id,
        client_secret=client_secret,
        redirect_uri="/spotify/callback",
        auth_url=f"{api_url}/authorize",
        token_url=f"{api_url}/token",
        api_url=f"{api_url}/api",
    )

    app.add_subapp("/spotify", spotify_app)
    app.add_subapp("/api", api_app)

    return loop.run_until_complete(
        aiohttp_client(app, server_kwargs={"port": port}))
コード例 #2
0
def app_factory(client_id: str, client_secret: str,
                redirect_uri: str) -> web.Application:
    app = web.Application()

    # Add the main routes to the app
    app.router.add_get("/", index, name="index")
    app.router.add_get("/me", me, name="me")

    # Set up the Spotify app to instigate the OAuth flow
    app["spotify_app"] = aiohttp_spotify.spotify_app(
        client_id=client_id,
        client_secret=client_secret,
        redirect_uri=redirect_uri,
        handle_auth=update_auth,
        default_redirect=app.router["index"].url_for(),
    )
    app.add_subapp("/spotify", app["spotify_app"])

    # Set up the user session
    secret_key = base64.urlsafe_b64decode(fernet.Fernet.generate_key())
    aiohttp_session.setup(app, EncryptedCookieStorage(secret_key))

    # Add the ClientSession to the app
    app.cleanup_ctx.append(client_session)

    return app
コード例 #3
0
def app_factory(config: Mapping[str, Any]) -> web.Application:
    app = web.Application(
        middlewares=[views.error_middleware,
                     web.normalize_path_middleware()])

    # load the configuration file
    app["config"] = config

    # Add the client session for pooling outgoing connections
    app.cleanup_ctx.append(client_session)

    # Connect the database and set up a map of websockets
    app["db"] = db.Database(config["database_filename"])

    # And the routes for the main app
    app.add_routes(views.routes)

    # Add the API app
    app.add_subapp("/api", api.api_app())

    # Set up the user session for cookies
    aiohttp_session.setup(
        app,
        EncryptedCookieStorage(
            base64.urlsafe_b64decode(app["config"]["session_key"])),
    )

    # Set up the templating engine and the static endpoint
    env = aiohttp_jinja2.setup(app,
                               loader=jinja2.FileSystemLoader(
                                   get_resource_path("templates")))
    env.globals.update(jinja2_helpers.GLOBALS)
    app["static_root_url"] = "/assets"
    app.router.add_static("/assets", get_resource_path("assets"))

    # Set up the Spotify app to instigate the OAuth flow
    app["spotify_app"] = aiohttp_spotify.spotify_app(
        client_id=config["spotify_client_id"],
        client_secret=config["spotify_client_secret"],
        redirect_uri=config["spotify_redirect_uri"],
        handle_auth=auth.handle_auth,
        default_redirect=app.router["play"].url_for(),
        scope=[
            "streaming",
            "user-read-email",
            "user-read-private",
            "user-modify-playback-state",
            "user-read-playback-state",
            "user-read-currently-playing",
        ],
    )
    app["spotify_app"]["main_app"] = app
    app.add_subapp("/spotify", app["spotify_app"])

    # Attach the socket.io interface
    api.sio.attach(app)

    return app