Skip to content

Commit

Permalink
update docs for componentize-py based sdk (#1174)
Browse files Browse the repository at this point in the history
* update docs for componentize-py based sdk

Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 authored Feb 28, 2024
1 parent 8efe048 commit 887321d
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 254 deletions.
9 changes: 4 additions & 5 deletions content/spin/v2/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,21 @@ command = "npm run build"

{{ startTab "Python" }}

For Python applications, you must have the `py2wasm` Spin plugin installed:
For Python applications, you must have [`componentize-py`](https://pypi.org/project/componentize-py/) installed:

<!-- @selectiveCpy -->

```bash
$ spin plugins update
$ spin plugins install py2wasm --yes
$ pip3 install componentize-py
```

The build command then calls `spin py2wasm` on your application file:
The build command then calls `componentize-py` on your application file:

<!-- @nocpy -->

```toml
[component.hello.build]
command = "spin py2wasm app -o app.wasm"
command = "componentize-py -w spin-http componentize app -o app.wasm"
```

{{ blockEnd }}
Expand Down
12 changes: 5 additions & 7 deletions content/spin/v2/http-outbound.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ You can find a complete example of using outbound HTTP in the JavaScript SDK rep

{{ startTab "Python"}}

> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/v1/spin_http.html)
> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/http/index.html)
HTTP functions and classes are available in the `spin_http` module. The function name is [`http_send`](https://fermyon.github.io/spin-python-sdk/v1/spin_http.html#spin_sdk.spin_http.http_send). The [request type](https://fermyon.github.io/spin-python-sdk/v1/spin_http.html#spin_sdk.spin_http.Request) is `Request`, and the [response type](https://fermyon.github.io/spin-python-sdk/v1/spin_http.html#spin_sdk.spin_http.Response) is `Response`. For example:
HTTP functions and classes are available in the `http` module. The function name is [`send`](https://fermyon.github.io/spin-python-sdk/http/index.html#spin_sdk.http.send). The [request type](https://fermyon.github.io/spin-python-sdk/http/index.html#spin_sdk.http.Request) is `Request`, and the [response type](https://fermyon.github.io/spin-python-sdk/http/index.html#spin_sdk.http.Response) is `Response`. For example:

```python
from spin_http import Request, Response, http_send

response = http_send(
Request("GET", "https://random-data-api.fermyon.app/animals/json", {}, None))
from spin_sdk.http import Request, Response, send
response = send(Request("GET", "https://random-data-api.fermyon.app/animals/json", {}, None))
```

**Notes**
Expand All @@ -123,7 +121,7 @@ response = http_send(
* Request and response headers are dictionaries.
* Errors are signalled through exceptions.

You can find a complete example for using outbound HTTP in the [Python SDK repository on GitHub](https://github.com/fermyon/spin-python-sdk/tree/old-sdk/examples/outbound_http).
You can find a complete example for using outbound HTTP in the [Python SDK repository on GitHub](https://github.com/fermyon/spin-python-sdk/tree/main/examples/outgoing-request).

{{ blockEnd }}

Expand Down
20 changes: 12 additions & 8 deletions content/spin/v2/http-trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,21 @@ export const handleRequest: HandleRequest = async function(request: HttpRequest)

{{ startTab "Python"}}

> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/v1)
> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/)
In Python, the handler is identified by name. It must be called `handle_request`. It takes a request object and must return an instance of `Response`, defined in the `spin_http` package:
In Python, the application must define a top-level class named IncomingHandler which inherits from [IncomingHandler](https://fermyon.github.io/spin-python-sdk/http/index.html#spin_sdk.http.IncomingHandler), overriding the `handle_request` method.

```python
from spin_http import Response

def handle_request(request):
return Response(200,
[("content-type", "text/plain")],
bytes(f"Hello from the Python SDK", "utf-8"))
from spin_sdk import http
from spin_sdk.http import Request, Response

class IncomingHandler(http.IncomingHandler):
def handle_request(self, request: Request) -> Response:
return Response(
200,
{"content-type": "text/plain"},
bytes("Hello from Python!", "utf-8")
)
```

{{ blockEnd }}
Expand Down
69 changes: 33 additions & 36 deletions content/spin/v2/key-value-store-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $ spin new -t http-rust spin-key-value
```bash
$ spin new -t http-ts spin-key-value

# Reference: https://github.com/fermyon/spin-python-sdk/tree/old-sdk/examples/KV
# Reference: https://github.com/fermyon/spin-python-sdk/tree/main/examples/spin-kv
```

{{ blockEnd }}
Expand Down Expand Up @@ -275,41 +275,38 @@ export const handleRequest: HandleRequest = async function (request: HttpRequest
{{ startTab "Python" }}

```python
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)
if value:
status = 200
print(f"Found key {request.uri}")
else:
status = 404
print(f"Key {request.uri} not found")
return Response(status, {"content-type": "text/plain"}, value)
case "POST":
store.set(request.uri, request.body)
print(f"Stored key {request.uri}")
return Response(200, {"content-type": "text/plain"})
case "DELETE":
store.delete(request.uri)
print(f"Deleted key {request.uri}")
return Response(200, {"content-type": "text/plain"})
case "HEAD":
if store.exists(request.uri):
print(f"Found key {request.uri}")
return Response(200, {"content-type": "text/plain"})
print(f"Key not found {request.uri}")
return Response(404, {"content-type": "text/plain"})
case default:
return Response(405, {"content-type": "text/plain"})
```
from spin_sdk import http, key_value
from spin_sdk.http import Request, Response

class IncomingHandler(http.IncomingHandler):
def handle_request(self, request: Request) -> Response:
with key_value.open_default() as store:
match request.method:
case "GET":
value = store.get(request.uri)
if value:
status = 200
print(f"Found key {request.uri}")
else:
status = 404
print(f"Key {request.uri} not found")
return Response( status, {"content-type": "text/plain"}, value)
case "POST":
store.set(request.uri, request.body)
print(f"Stored key {request.uri}")
return Response(200, {"content-type": "text/plain"})
case "DELETE":
store.delete(request.uri)
print(f"Deleted key {request.uri}")
return Response(200, {"content-type": "text/plain"})
case "HEAD":
if store.exists(request.uri):
print(f"Found key {request.uri}")
return Response(200, {"content-type": "text/plain"})
print(f"Key not found {request.uri}")
return Response(404, {"content-type": "text/plain"})
case default:
return Response(405, {"content-type": "text/plain"})

{{ blockEnd }}

Expand Down
29 changes: 16 additions & 13 deletions content/spin/v2/kv-store-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,32 @@ export const handleRequest: HandleRequest = async function (request: HttpRequest
{{ startTab "Python"}}
> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/v1/spin_key_value.html)
> [**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-python-sdk/key_value.html)
The key value functions are provided through the `spin_key_value` module in the Python SDK. For example:
```python
from spin_http import Response
from spin_key_value import kv_open_default


def handle_request(request):

store = kv_open_default()
store.set("mykey", b"myvalue")
value = store.get("mykey")
//
return Response(status, [("content-type", "text/plain")], value)
from spin_sdk import http, key_value
from spin_sdk.http import Request, Response

class IncomingHandler(http.IncomingHandler):
def handle_request(self, request: Request) -> Response:
with key_value.open_default() as store:
store.set("test", bytes("hello world!", "utf-8"))
val = a.store("test")

return Response(
200,
{"content-type": "text/plain"},
val
)

```
**General Notes**
- The Python SDK doesn't surface the `close` operation. It automatically closes all stores at the end of the request; there's no way to close them early.
[`get` **Operation**](https://fermyon.github.io/spin-python-sdk/v1/spin_key_value.html#spin_sdk.spin_key_value.Store.get)
[`get` **Operation**](https://fermyon.github.io/spin-python-sdk/wit/imports/key_value.html#spin_sdk.wit.imports.key_value.Store.get)
- If a key does not exist, it returns `None`
{{ blockEnd }}
Expand Down
6 changes: 3 additions & 3 deletions content/spin/v2/language-support-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ This page contains information about language support for Spin features:
|-----|-----|
| **Triggers** |
| [HTTP](./python-components#a-simple-http-components-example) | Supported |
| Redis | Not Supported |
| Redis | Supported |
| **APIs** |
| [Outbound HTTP](./python-components#an-outbound-http-example) | Supported |
| [Configuration Variables](./dynamic-configuration#custom-config-variables) | Supported |
| [Key Value Storage](./kv-store-api-guide) | Supported |
| [SQLite Storage](./sqlite-api-guide) | Supported |
| MySQL | Not Supported |
| PostgreSQL | Not Supported |
| MySQL | Supported |
| PostgreSQL | Supported |
| [Outbound Redis](./python-components#an-outbound-redis-example) | Supported |
| [Serverless AI](./serverless-ai-api-guide) | Supported |
| **Extensibility** |
Expand Down
Loading

0 comments on commit 887321d

Please sign in to comment.