Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions examples/KV/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
*.wasm
.spin
3 changes: 3 additions & 0 deletions examples/KV/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spin python Key Value

A simple example showcasing the usage of Spin Key Value in the Spin Python SDK.
24 changes: 24 additions & 0 deletions examples/KV/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from spin_http import Response
from spin_key_value import kv_open_default


def handle_request(request):

store = kv_open_default()

match request.method:
case "GET":
value = store.get(request.uri)
return Response(200, [("content-type", "text/plain")], value)
case "POST":
store.set(request.uri, request.body)
return Response(200, [("content-type", "text/plain")])
case "DELETE":
store.delete(request.uri)
return Response(200, [("content-type", "text/plain")])
case "HEAD":
if store.exists(request.uri):
return Response(200, [("content-type", "text/plain")])
return Response(404, [("content-type", "text/plain")])
case default:
return Response(405, [("content-type", "text/plain")])
15 changes: 15 additions & 0 deletions examples/KV/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "Spin Python SDK KV"
name = "spin-py-KV"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "python-sdk-example"
source = "app.wasm"
key_value_stores = ["default"]
[component.trigger]
route = "/..."
[component.build]
command = "spin py2wasm app -o app.wasm"
3 changes: 3 additions & 0 deletions examples/external_lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
*.wasm
.spin
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/external_lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# External Libraries With Spin Python

A simple example showcasing the usage of an external library with the Spin Python SDK.
15 changes: 15 additions & 0 deletions examples/external_lib/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from spin_http import Response
import toml

def handle_request(request):

some_toml = """
title = "foo"
[bar]
baz = "wow"
such = "toml"
"""

return Response(200,
[("content-type", "text/plain")],
bytes(f"Toml content:{toml.loads(some_toml)}", "utf-8"))
14 changes: 14 additions & 0 deletions examples/external_lib/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "Spin Python SDK example using external library"
name = "spin-py-external-library"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "python-sdk-example"
source = "app.wasm"
[component.trigger]
route = "/..."
[component.build]
command = "spin py2wasm app -o app.wasm"
41 changes: 0 additions & 41 deletions examples/hello/app.py

This file was deleted.

1 change: 0 additions & 1 deletion examples/hello/static-assets/foo.txt

This file was deleted.

3 changes: 3 additions & 0 deletions examples/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
*.wasm
.spin
3 changes: 3 additions & 0 deletions examples/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spin python Hello World

A simple example showcasing the return of a simple response to a request in the Spin Python SDK.
8 changes: 8 additions & 0 deletions examples/hello_world/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from spin_http import Response


def handle_request(request):

return Response(200,
[("content-type", "text/plain")],
bytes(f"Hello from Python!", "utf-8"))
14 changes: 14 additions & 0 deletions examples/hello_world/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "Spin Python SDK hello world"
name = "spin-py-hello-world"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "python-sdk-example"
source = "app.wasm"
[component.trigger]
route = "/..."
[component.build]
command = "spin py2wasm app -o app.wasm"
3 changes: 3 additions & 0 deletions examples/outbound_http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
*.wasm
.spin
3 changes: 3 additions & 0 deletions examples/outbound_http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spin python Outbound HTTP

A simple example showcasing an outbound HTTP request in Spin Python SDK.
11 changes: 11 additions & 0 deletions examples/outbound_http/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from spin_http import Request, Response, http_send


def handle_request(request):

response = http_send(
Request("GET", "https://some-random-api.ml/facts/dog", [], None))

return Response(200,
[("content-type", "text/plain")],
bytes(f"Here is a dog fact: {str(response.body, 'utf-8')}", "utf-8"))
12 changes: 5 additions & 7 deletions examples/hello/spin.toml → examples/outbound_http/spin.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "Spin Python SDK example"
name = "python-sdk-example"
description = "Spin Python Outbound HTTP Example"
name = "Spin-python-outbound-http"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "python-sdk-example"
environment = { hello = "teapot" }
config = { redis_address = "redis://127.0.0.1:6379" }
files = [{ source = "static-assets/", destination = "/" }]
source = "app.wasm"
allowed_http_hosts = ["insecure:allow-all"]
environment = { hello = "teapot" }
allowed_http_hosts = ["https://some-random-api.ml"]
[component.trigger]
route = "/..."
[component.build]
command = "../../target/release/spin-python app -o app.wasm"
command = "spin py2wasm app -o app.wasm"
3 changes: 3 additions & 0 deletions examples/outbound_redis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
*.wasm
.spin
3 changes: 3 additions & 0 deletions examples/outbound_redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spin python Outbound Redis

A simple example showcasing outbound Redis command execution using the Spin Python SDK
24 changes: 24 additions & 0 deletions examples/outbound_redis/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from spin_http import Response
from spin_redis import redis_del, redis_get, redis_incr, redis_set, redis_sadd, redis_srem, redis_smembers, redis_execute
from spin_config import config_get


def handle_request(request):

redis_address = config_get("redis_address")
redis_set(redis_address, "foo", b"bar")
value = redis_get(redis_address, "foo")
redis_del(redis_address, ["testIncr"])
redis_incr(redis_address, "testIncr")

redis_sadd(redis_address, "testSets", ["hello", "world"])
content = redis_smembers(redis_address, "testSets")
redis_srem(redis_address, "testSets", ["hello"])
redis_execute("redis_address", "set", [b"foo", b"hello"])
redis_execute("redis_address", "append", [b"foo", b", world!"])

assert value == b"bar", f"expected \"bar\", got \"{str(value, 'utf-8')}\""

return Response(200,
[("content-type", "text/plain")],
bytes(f"Executed outbound Redis commands", "utf-8"))
15 changes: 15 additions & 0 deletions examples/outbound_redis/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "Spin Python Outbound Redis example"
name = "spin-python-outbound-redis"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "python-sdk-outbound-redis"
config = { redis_address = "redis://127.0.0.1:6379" }
source = "app.wasm"
[component.trigger]
route = "/..."
[component.build]
command = "spin py2wasm app -o app.wasm"