Skip to content

Commit

Permalink
📦 updates MDK for v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Jun 12, 2023
1 parent 948e2bd commit 6aa9a01
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ tasks:
env:
#TAG: "v0.0.1"
#TAG: "v0.0.2"
TAG: "v0.0.3" # current release
#TAG: "v0.0.4" # current release
TAG: "v0.0.4" # current release
#TAG: "v0.0.5" # it will be the next release

cmds:
- echo "📦 Generating release..."
- git add .
Expand Down
11 changes: 10 additions & 1 deletion capsule.dk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "errors"
const isFailure = rune('F')
const isSuccess = rune('S')


func success(buffer []byte) uint64 {
return copyBufferToMemory(append([]byte(string(isSuccess)), buffer...))
}
Expand All @@ -15,6 +14,16 @@ func failure(buffer []byte) uint64 {
return copyBufferToMemory(append([]byte(string(isFailure)), buffer...))
}

// Success function
func Success(buffer []byte) uint64 {
return success(buffer)
}

// Failure function
func Failure(buffer []byte) uint64 {
return failure(buffer)
}

// Result function
func Result(data []byte,) ([]byte, error) {
if data[0] == byte(isSuccess) {
Expand Down
65 changes: 65 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 🧰 Helpers

> 🚧 this is a work in progress
## Success and Failure
> introduced in v0.0.4
`Success` and `Failure` copy an `[]byte` to the shared memory, and return the position and the length of the value "packed" into a single value (`uint64`).

If you use `Success`, the `[]byte` is prefixed by `rune('S')` before being copied to the shared memory.

If you use `Failure`, the `[]byte` is prefixed by `rune('F')` before being copied to the shared memory.

This value will be used by the function `CallHandleFunction` of the [Host SDK](https://github.com/bots-garden/capsule-host-sdk/blob/main/runtime.go) that will use the [`Result` function](https://github.com/bots-garden/capsule-host-sdk/blob/main/capsule.dk.go) to extract the result status (success or failure) and the result value (value or error).

```golang
// Package main
package main

import (
"strconv"
"github.com/bots-garden/capsule-module-sdk"
)

// OnHealthCheck function
//export OnHealthCheck
func OnHealthCheck() uint64 {
capsule.Print("⛑️ OnHealthCheck")

response := capsule.HTTPResponse{
JSONBody: `{"message": "OK"}`,
Headers: `{"Content-Type": "application/json; charset=utf-8"}`,
StatusCode: 200,
}

return capsule.Success([]byte(capsule.StringifyHTTPResponse(response)))
}

func main() {
capsule.SetHandleHTTP(func (param capsule.HTTPRequest) (capsule.HTTPResponse, error) {
return capsule.HTTPResponse{
TextBody: "👋 Hey",
Headers: `{"Content-Type": "text/plain; charset=utf-8"}`,
StatusCode: 200,
}, nil

})
}
```
> 👋 don't forget to export the `OnHealthCheck` function
## StringifyHTTPResponse
> introduced in v0.0.4
`StringifyHTTPResponse` converts a `capsule.HTTPResponse` into a string.

```golang
response := capsule.HTTPResponse{
JSONBody: `{"message": "OK"}`,
Headers: `{"Content-Type": "application/json; charset=utf-8"}`,
StatusCode: 200,
}

str := capsule.StringifyHTTPResponse(response)
```
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Capsule Module SDK

!!! info "What's new?"
- `v0.0.4`: ✨ Add `Success` and `Failure` functions (public functionc to call `success` and `failure`) and `StringifyHTTPResponse`
- `v0.0.3`: ✨ Encode `retValue.TextBody` to avoid special characters in jsonString
- `v0.0.2`: ✨ Redis support
- `v0.0.1`: 🎉 first release

Expand Down
40 changes: 31 additions & 9 deletions handle.http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,40 @@ import (
"github.com/valyala/fastjson"
)



var handleHTTPFunction func(param HTTPRequest) (HTTPResponse, error)

// SetHandleHTTP sets the handle function
func SetHandleHTTP(function func(param HTTPRequest) (HTTPResponse, error)) {
handleHTTPFunction = function
}

// StringifyHTTPResponse converts a HTTPResponse to a string
func StringifyHTTPResponse(response HTTPResponse) string {

var jsonBody string
if len(response.JSONBody) == 0 {
jsonBody = "{}"
} else {
jsonBody = response.JSONBody
}

var textBody string
if len(response.TextBody) == 0 {
textBody = ""
} else {
// avoid special characters in jsonString
textBody = base64.StdEncoding.EncodeToString([]byte(response.TextBody))
//textBody = retValue.TextBody
}

jsonHTTPResponse := `{"JSONBody":` + jsonBody + `,"TextBody":"` + textBody + `","Headers":` + response.Headers + `,"StatusCode":` + strconv.Itoa(response.StatusCode) + `}`

return jsonHTTPResponse
}

//export callHandleHTTP
func callHandleHTTP(JSONDataPos *uint32, JSONDataSize uint32) uint64 {

parser := fastjson.Parser{}

JSONDataBuffer := readBufferFromMemory(JSONDataPos, JSONDataSize)
Expand All @@ -30,9 +52,9 @@ func callHandleHTTP(JSONDataPos *uint32, JSONDataSize uint32) uint64 {
Body: string(JSONData.GetStringBytes("Body")),
//JSONBody: string(JSONData.GetStringBytes("JSONBody")), //! to use in the future
//TextBody: string(JSONData.GetStringBytes("TextBody")), //! to use in the future
URI: string(JSONData.GetStringBytes("URI")),
Method: string(JSONData.GetStringBytes("Method")),
Headers: string(JSONData.GetStringBytes("Headers")),
URI: string(JSONData.GetStringBytes("URI")),
Method: string(JSONData.GetStringBytes("Method")),
Headers: string(JSONData.GetStringBytes("Headers")),
}

// call the handle function
Expand All @@ -49,7 +71,7 @@ func callHandleHTTP(JSONDataPos *uint32, JSONDataSize uint32) uint64 {
} else {
jsonBody = retValue.JSONBody
}

var textBody string
if len(retValue.TextBody) == 0 {
textBody = ""
Expand All @@ -59,9 +81,9 @@ func callHandleHTTP(JSONDataPos *uint32, JSONDataSize uint32) uint64 {
//textBody = retValue.TextBody
}

jsonHTTPResponse := `{"JSONBody":`+jsonBody+`,"TextBody":"`+textBody+`","Headers":`+retValue.Headers+`,"StatusCode":`+strconv.Itoa(retValue.StatusCode)+`}`
jsonHTTPResponse := `{"JSONBody":` + jsonBody + `,"TextBody":"` + textBody + `","Headers":` + retValue.Headers + `,"StatusCode":` + strconv.Itoa(retValue.StatusCode) + `}`

// first byte == 82
return success([]byte(jsonHTTPResponse))

}
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nav:
- First CLI module: first-cli-module.md
- First HTTP module: first-http-module.md
- 🛠️ Host functions: host-functions.md
- 🧰 Helpers: helpers.md

theme:
name: material
Expand Down
4 changes: 4 additions & 0 deletions samples/http-say-hello/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/bots-garden/capsule-module-sdk v0.0.3 h1:a0TRgwdiJjc+9raOe4zQoWPnzxKegqmInVmwzlaDuug=
github.com/bots-garden/capsule-module-sdk v0.0.3/go.mod h1:DtKYwanz4YvBwSpu0GuuhtkeFE6+jDgEucBOTWVBMy8=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
10 changes: 5 additions & 5 deletions samples/http-say-hello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ func main() {
capsule.SetHandleHTTP(Handle)
}

// Handle function
// Handle function
func Handle(param capsule.HTTPRequest) (capsule.HTTPResponse, error) {

capsule.Print("📝: " + param.Body)
capsule.Print("🔠: " + param.Method)
capsule.Print("🌍: " + param.URI)
capsule.Print("👒: " + param.Headers)

var p fastjson.Parser
jsonBody, err := p.Parse(param.Body)
if err != nil {
Expand All @@ -29,8 +29,8 @@ func Handle(param capsule.HTTPRequest) (capsule.HTTPResponse, error) {
capsule.Log(message)

response := capsule.HTTPResponse{
JSONBody: `{"message": "`+message+`"}`,
Headers: `{"Content-Type": "application/json; charset=utf-8"}`,
JSONBody: `{"message": "` + message + `"}`,
Headers: `{"Content-Type": "application/json; charset=utf-8"}`,
StatusCode: 200,
}

Expand Down

0 comments on commit 6aa9a01

Please sign in to comment.