Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(example): examples overhaul #309

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions integrations/tortoise-ORM/guest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from uagents import Agent, Context
from models import RoomRequest, RoomResponse

HOTEL_ADDRESS = "agent1qvsmzq0awkv0mptpuw4fgr4a4fqqdejsxuju4auk08aqmq98hzuhgcgfspp"
GUEST_SEED = "put_guest_seed_phrase_here"

guest = Agent(
name="guest",
port=8000,
seed=GUEST_SEED,
endpoint=["http://127.0.0.1:8000/submit"],
)

request = RoomRequest(max_price=70)

@guest.on_event("startup")
async def startup(ctx: Context):
ctx.storage.set("completed", False)

@guest.on_interval(period=8.0)
async def send_message(ctx: Context):

completed = ctx.storage.get("completed")

if not completed:
ctx.logger.info(f"Sending room booking request: {request}")
await ctx.send(HOTEL_ADDRESS, request)


@guest.on_message(RoomResponse, replies=set())
async def booking_handler(ctx: Context, _sender: str, msg: RoomResponse):
if msg.success:
ctx.logger.info("Booking was successful")
else:
ctx.logger.info("Booking was unsuccessful")

ctx.storage.set("completed", True)


if __name__ == "__main__":
guest.run()
54 changes: 54 additions & 0 deletions integrations/tortoise-ORM/hotel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from uagents import Agent, Context, Model
from tortoise import Tortoise
from models import Availability, RoomRequest, RoomResponse


HOTEL_SEED = "put_hotel_seed_phrase_here"

hotel = Agent(
name="hotel",
port=8001,
seed=HOTEL_SEED ,
endpoint=["http://127.0.0.1:8001/submit"],
)

print(f"hotel's agent address: {hotel.address}")

@hotel.on_event("startup")
async def startup(_ctx: Context):
await Tortoise.init(
db_url="sqlite://db.sqlite3", modules={"models": ["models"]}
)
await Tortoise.generate_schemas()

await Availability.create(
room_available=True,
min_price=50,
)


@hotel.on_event("shutdown")
async def shutdown(_ctx: Context):
await Tortoise.close_connections()

@hotel.on_message(model=RoomRequest)
async def message_handler(ctx: Context, sender: str, msg: RoomRequest):
availability = await Availability.first()
success = False
if availability.room_available:
ctx.logger.info(f"Room available, attempting to book")
if availability.min_price <= msg.max_price:
success = True
ctx.logger.info(f"Offer of ${msg.max_price} accepted!")
availability.room_available = False
await availability.save()
else:
ctx.logger.info(f"Offer of ${msg.max_price} was to low, won't accept ")
else:
ctx.logger.info(f"Room unavailable")

await ctx.send(sender, RoomResponse(success=success))


if __name__ == "__main__":
hotel.run()
14 changes: 14 additions & 0 deletions integrations/tortoise-ORM/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from uagents import Model
from tortoise import fields, models

class RoomRequest(Model):
max_price: int

class RoomResponse(Model):
success: bool

class Availability(models.Model):
id = fields.IntField(pk=True)
room_available = fields.BooleanField(default=True)
min_price = fields.FloatField(default=0.0)

1,457 changes: 1,457 additions & 0 deletions integrations/tortoise-ORM/poetry.lock

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions integrations/tortoise-ORM/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "tortoise-db"
version = "0.1.0"
description = ""
authors = ["Alejandro-Morales <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
uagents = "*"
black = "^24.3.0"
tortoise-orm = "^0.17.4"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
12 changes: 0 additions & 12 deletions python/examples/01-first-agent/main.py

This file was deleted.

22 changes: 0 additions & 22 deletions python/examples/03-multiple-agents/main.py

This file was deleted.

19 changes: 0 additions & 19 deletions python/examples/04-storage/main.py

This file was deleted.

28 changes: 0 additions & 28 deletions python/examples/05-send-msg/main.py

This file was deleted.

67 changes: 0 additions & 67 deletions python/examples/07-msg-verification/main.py

This file was deleted.

Empty file.
42 changes: 0 additions & 42 deletions python/examples/09-booking-protocol-demo/protocols/book.py

This file was deleted.

64 changes: 0 additions & 64 deletions python/examples/09-booking-protocol-demo/protocols/query.py

This file was deleted.

Loading
Loading