-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import argparse | ||
|
||
import requests | ||
|
||
RESTATE_URL = "http://localhost:8080" | ||
|
||
|
||
# Durable RPC call to the product service | ||
# Restate registers the request and makes sure it runs to completion exactly once | ||
# Could be part of a Flask app or any other Python application | ||
def reserve_product(product_id: str, reservation_id: str): | ||
url = f"{RESTATE_URL}/product/{product_id}/reserve" | ||
headers = { | ||
"idempotency-key": reservation_id, | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.post(url, headers=headers) | ||
print({"reserved": response.json()}) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("product_id", type=str, help="Product ID") | ||
parser.add_argument("reservation_id", type=str, help="Reservation ID") | ||
|
||
args = parser.parse_args() | ||
reserve_product(args.product_id, args.reservation_id) |
16 changes: 16 additions & 0 deletions
16
python/patterns-use-cases/src/durablerpc/product_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import restate | ||
from restate import ObjectContext, VirtualObject | ||
|
||
product_service = VirtualObject("product") | ||
|
||
|
||
@product_service.handler() | ||
async def reserve(ctx: ObjectContext) -> bool: | ||
if await ctx.get("reserved"): | ||
print(f"Product already reserved {ctx.key}") | ||
return False | ||
print(f"Reserving product {ctx.key()}") | ||
ctx.set("reserved", True) | ||
return True | ||
|
||
app = restate.app([product_service]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters