docs(v4): update Python code snippets/links/details#1
Conversation
Signed-off-by: Vaughn Dice <vdice@akamai.com>
itowlson
left a comment
There was a problem hiding this comment.
This is great, thank you so much for getting it done. I had a few wording suggestions and there is one place I think we need to go async but otherwise looking really good!
| > [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-python-sdk/v4/http/index.html) | ||
|
|
||
| HTTP functions and classes are available in the `http` module. The function name is [`send`](https://spinframework.github.io/spin-python-sdk/v3/http/index.html#spin_sdk.http.send). The [request type](https://spinframework.github.io/spin-python-sdk/http/index.html#spin_sdk.http.Request) is `Request`, and the [response type](https://spinframework.github.io/spin-python-sdk/v3/http/index.html#spin_sdk.http.Response) is `Response`. For example: | ||
| HTTP functions and classes are available in the `http` module. The function name is [`send`](https://spinframework.github.io/spin-python-sdk/v4/http/index.html#spin_sdk.http.send). The [request type](https://spinframework.github.io/spin-python-sdk/http/index.html#spin_sdk.http.Request) is `Request`, and the [response type](https://spinframework.github.io/spin-python-sdk/v4/http/index.html#spin_sdk.http.Response) is `Response`. For example: |
There was a problem hiding this comment.
Should the request type link have a v4 in it?
| from spin_sdk.http import Request, Response, send | ||
| response = send(Request("GET", "https://random-data-api.fermyon.app/animals/json", {}, None)) | ||
|
|
||
| class WasiHttpHandler030Rc20260315(http.Handler): |
There was a problem hiding this comment.
I would drop this. I'd recommend just showing the send line, as the previous fragment did - I don't think we need to show a full request handler. (But if we do, I'd prefer to avoid such a distractingly long class name!)
There was a problem hiding this comment.
What is your stance now that the class name is HttpHandler? Although I appreciate that it is clearer/more obvious to just show the send line, it won't be runnable (or 'compilable') as is. Maybe we add 'snippet' indications? Also, most/many of the other Python code samples do include the full class/handler declarations and it would be nice to keep consistent one way or the other.
There was a problem hiding this comment.
For myself I am happier with snippets that show me the one thing the text is describing, so that I don't need to parse it out from the boilerplate. But I know many others feel differently, and it's certainly not a hill I intend to die on, at least not in the context of this PR. So I'm happy with whatever you prefer, and agree on the value of consistency.
| > [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-python-sdk/v4/) | ||
|
|
||
| In Python, the application must define a top-level class named IncomingHandler which inherits from [IncomingHandler](https://spinframework.github.io/spin-python-sdk/v3/http/index.html#spin_sdk.http.IncomingHandler), overriding the `handle_request` method. | ||
| In Python, the application must define a top-level class named `WasiHttpHandler030Rc20260315` which inherits from [http.Handler](https://spinframework.github.io/spin-python-sdk/v4/http/index.html#spin_sdk.http.Handler), overriding the `handle_request` method. |
There was a problem hiding this comment.
WasiHttpHandler030Rc20260315
EGAD
Do we really have to call it that?
| {{ startTab "Python"}} | ||
|
|
||
| In Python, the handler needs to implement the [`InboundRedis`](https://spinframework.github.io/spin-python-sdk/v3/wit/exports/index.html#spin_sdk.wit.exports.InboundRedis) class, and override the `handle_message` method: | ||
| In Python, the handler needs to implement the [`SpinRedisInboundRedis300`](https://spinframework.github.io/spin-python-sdk/v4/wit/exports/index.html#spin_sdk.wit.exports.SpinRedisInboundRedis300) class, and override the `handle_message` method: |
There was a problem hiding this comment.
Oh this one only needs to implement the base class? The application class doesn't need a specific name itself the way we said HTTP did?
| {{ startTab "Python"}} | ||
|
|
||
| In Python, the handler needs to implement the [`InboundRedis`](https://spinframework.github.io/spin-python-sdk/v3/wit/exports/index.html#spin_sdk.wit.exports.InboundRedis) class, and override the `handle_message` method: | ||
| In Python, the handler needs to implement the [`SpinRedisInboundRedis300`](https://spinframework.github.io/spin-python-sdk/v4/wit/exports/index.html#spin_sdk.wit.exports.SpinRedisInboundRedis300) class, and override the `handle_message` method: |
There was a problem hiding this comment.
Is it at all possible to alias or wrap this? We know it's Spin because this is the Spin SDK, the 300 is clutter, and SpinRedisInboundRedis is a heck of a lot of Redises to pack into four words!
| async def handle_request(self, request: Request) -> Response: | ||
| with await sqlite.open_default() as db: | ||
| result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)]) | ||
| rows = result.rows |
There was a problem hiding this comment.
This still works because I added the async stuff backward compatibly in a 3.x interface, but we should really be encouraging people to use the async forms (and ideally the SDK would only surface the async forms, i.e. SDK execute maps to WIT execute-async).
| **General Notes** | ||
| * The `execute` method returns [a `QueryResult` object](https://spinframework.github.io/spin-python-sdk/v3/wit/imports/sqlite.html#spin_sdk.wit.imports.sqlite.QueryResult) with `rows` and `columns` methods. `columns` returns a list of strings representing column names. `rows` is an array of rows, each of which is an array of [`RowResult`](https://spinframework.github.io/spin-python-sdk/v3/wit/imports/sqlite.html#spin_sdk.wit.imports.sqlite.RowResult) in the same order as `columns`. | ||
| * The connection object doesn't surface the `close` function. | ||
| * The `execute` method returns [a `QueryResult` object](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.QueryResult) with `rows` and `columns` methods. `columns` returns a list of strings representing column names. `rows` is an array of rows, each of which is an array of [`RowResult`](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.RowResult) in the same order as `columns`. |
There was a problem hiding this comment.
We should be sending people to execute_async (and wrapping it in the SDK so they don't need to use the wart).
Co-authored-by: itowlson <github@hestia.cc> Signed-off-by: Vaughn Dice <vdice@akamai.com>
a6afd55 to
6f97b0a
Compare
|
@itowlson I added all of your code suggestions and then fixed up other feedback in the subsequent commit. Let me know if I missed anything or if new items come up. |
Signed-off-by: Vaughn Dice <vdice@akamai.com>
6f97b0a to
8e2bf4c
Compare
|
@itowlson shall I look at updating v4/python-components.md next, now that you've added specific notes around async? Or did you have some changes in the works already? |
That would be brilliant - thank you! |
Signed-off-by: Vaughn Dice <vdice@akamai.com>
itowlson
left a comment
There was a problem hiding this comment.
LGTM - I suggest we get this merged and update things like SQLite async as they come - is that okay?
|
@itowlson yes, please merge when convenient. Will look into spinframework/spin-python-sdk#146 tomorrow. |
TODO (either here or in follow-ups):