Nothing Special   »   [go: up one dir, main page]

Skip to content
Fate 怒 edited this page Mar 29, 2022 · 4 revisions

Examples

In this section we'll see how you can use aiobungie with popular Python packages such as quart.

Quart RESTful JSON API.

While flask is less-performant in async support, It is recommended to use quart instead.

This example will show how you can setup a backend RESTful webserver.

Installing

  • pip install aiobungie
  • pip install quart
import quart
import aiobungie
import attrs
import typing

app = quart.Quart(__name__)
client = aiobungie.Client("token")

# Root path.
@app.route("/", methods=["GET"])
async def main():
    return {"Hello": "There"}

# Search for Destiny memberships.
@app.route("/search/<username>", methods=["GET"])
async def search_memberships(username: str):
    users = await client.search_users(username)

    if not users:
        return {"error": "No users found."}

    memberships = []

    # Filter the memberships to return only the users the matches the provided username.
    async for user in users.filter(lambda usr: any(m.name == username for m in usr.memberships)):
        # Extend each user's membership as dict to the list.
        # Here "attrs.asdict" will convert the membership into a dict to return it as JSON object.
        memberships.extend(attrs.asdict(membership) for membership in user.memberships)

    return memberships
Clone this wiki locally