diff --git a/examples/KV/.gitignore b/examples/KV/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/examples/KV/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/examples/KV/README.md b/examples/KV/README.md new file mode 100644 index 0000000..6e80433 --- /dev/null +++ b/examples/KV/README.md @@ -0,0 +1,3 @@ +# Spin python Key Value + +A simple example showcasing the usage of Spin Key Value in the Spin Python SDK. \ No newline at end of file diff --git a/examples/KV/app.py b/examples/KV/app.py new file mode 100644 index 0000000..56973c7 --- /dev/null +++ b/examples/KV/app.py @@ -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")]) diff --git a/examples/KV/spin.toml b/examples/KV/spin.toml new file mode 100644 index 0000000..a8c96fe --- /dev/null +++ b/examples/KV/spin.toml @@ -0,0 +1,15 @@ +spin_version = "1" +authors = ["Fermyon Engineering "] +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" diff --git a/examples/external_lib/.gitignore b/examples/external_lib/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/examples/external_lib/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/examples/hello/Pipfile b/examples/external_lib/Pipfile similarity index 100% rename from examples/hello/Pipfile rename to examples/external_lib/Pipfile diff --git a/examples/hello/Pipfile.lock b/examples/external_lib/Pipfile.lock similarity index 100% rename from examples/hello/Pipfile.lock rename to examples/external_lib/Pipfile.lock diff --git a/examples/external_lib/README.md b/examples/external_lib/README.md new file mode 100644 index 0000000..177e514 --- /dev/null +++ b/examples/external_lib/README.md @@ -0,0 +1,3 @@ +# External Libraries With Spin Python + +A simple example showcasing the usage of an external library with the Spin Python SDK. \ No newline at end of file diff --git a/examples/external_lib/app.py b/examples/external_lib/app.py new file mode 100644 index 0000000..2542dad --- /dev/null +++ b/examples/external_lib/app.py @@ -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")) diff --git a/examples/external_lib/spin.toml b/examples/external_lib/spin.toml new file mode 100644 index 0000000..c9b72e5 --- /dev/null +++ b/examples/external_lib/spin.toml @@ -0,0 +1,14 @@ +spin_version = "1" +authors = ["Fermyon Engineering "] +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" diff --git a/examples/hello/app.py b/examples/hello/app.py deleted file mode 100644 index de72d5e..0000000 --- a/examples/hello/app.py +++ /dev/null @@ -1,41 +0,0 @@ -from spin_http import Request, Response, http_send -from spin_redis import redis_del, redis_get, redis_incr, redis_set, redis_sadd, redis_srem, redis_smembers -from spin_config import config_get -from os import environ -import toml - -def handle_request(request): - print(f"Got request URI: {request.uri}") - - print(f"Here's my environment: {environ}") - - some_toml = """ - title = "foo" - - [bar] - baz = "wow" - such = "toml" - """ - print(f"And here's some TOML: {toml.loads(some_toml)}") - - my_file = open("/foo.txt", "r") - print(f"And here's the content of foo.txt: {my_file.read()}") - - response = http_send(Request("GET", "https://some-random-api.ml/facts/dog", [], None)) - print(f"Got dog fact: {str(response.body, 'utf-8')}") - - 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"]) - - assert value == b"bar", f"expected \"bar\", got \"{str(value, 'utf-8')}\"" - - return Response(200, - [("content-type", "text/plain")], - bytes(f"Hello from Python! Got request URI: {request.uri}", "utf-8")) diff --git a/examples/hello/static-assets/foo.txt b/examples/hello/static-assets/foo.txt deleted file mode 100644 index 051f33f..0000000 --- a/examples/hello/static-assets/foo.txt +++ /dev/null @@ -1 +0,0 @@ -jabberwocky \ No newline at end of file diff --git a/examples/hello_world/.gitignore b/examples/hello_world/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/examples/hello_world/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md new file mode 100644 index 0000000..3211b19 --- /dev/null +++ b/examples/hello_world/README.md @@ -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. \ No newline at end of file diff --git a/examples/hello_world/app.py b/examples/hello_world/app.py new file mode 100644 index 0000000..0dffcf1 --- /dev/null +++ b/examples/hello_world/app.py @@ -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")) diff --git a/examples/hello_world/spin.toml b/examples/hello_world/spin.toml new file mode 100644 index 0000000..af90e32 --- /dev/null +++ b/examples/hello_world/spin.toml @@ -0,0 +1,14 @@ +spin_version = "1" +authors = ["Fermyon Engineering "] +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" diff --git a/examples/outbound_http/.gitignore b/examples/outbound_http/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/examples/outbound_http/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/examples/outbound_http/README.md b/examples/outbound_http/README.md new file mode 100644 index 0000000..04c2046 --- /dev/null +++ b/examples/outbound_http/README.md @@ -0,0 +1,3 @@ +# Spin python Outbound HTTP + +A simple example showcasing an outbound HTTP request in Spin Python SDK. \ No newline at end of file diff --git a/examples/outbound_http/app.py b/examples/outbound_http/app.py new file mode 100644 index 0000000..7831b06 --- /dev/null +++ b/examples/outbound_http/app.py @@ -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")) diff --git a/examples/hello/spin.toml b/examples/outbound_http/spin.toml similarity index 50% rename from examples/hello/spin.toml rename to examples/outbound_http/spin.toml index 682cbb9..7c07acc 100644 --- a/examples/hello/spin.toml +++ b/examples/outbound_http/spin.toml @@ -1,18 +1,16 @@ spin_version = "1" authors = ["Fermyon Engineering "] -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" diff --git a/examples/outbound_redis/.gitignore b/examples/outbound_redis/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/examples/outbound_redis/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/examples/outbound_redis/README.md b/examples/outbound_redis/README.md new file mode 100644 index 0000000..4f03e67 --- /dev/null +++ b/examples/outbound_redis/README.md @@ -0,0 +1,3 @@ +# Spin python Outbound Redis + +A simple example showcasing outbound Redis command execution using the Spin Python SDK \ No newline at end of file diff --git a/examples/outbound_redis/app.py b/examples/outbound_redis/app.py new file mode 100644 index 0000000..8fcca3d --- /dev/null +++ b/examples/outbound_redis/app.py @@ -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")) diff --git a/examples/outbound_redis/spin.toml b/examples/outbound_redis/spin.toml new file mode 100644 index 0000000..ec89950 --- /dev/null +++ b/examples/outbound_redis/spin.toml @@ -0,0 +1,15 @@ +spin_version = "1" +authors = ["Fermyon Engineering "] +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"