Skip to content

Instantly share code, notes, and snippets.

@Zwiterrion
Last active March 21, 2024 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zwiterrion/8325294418c129d7ef8842f240bb84dc to your computer and use it in GitHub Desktop.
Save Zwiterrion/8325294418c129d7ef8842f240bb84dc to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"errors"
"github.com/buger/jsonparser"
"github.com/extism/go-pdk"
)
//export proxy_plugin_map_set
func _ProxyPluginMapSet(context uint64, contextSize uint64) uint64
//export proxy_plugin_map_get
func _ProxyPluginMapGet(context uint64, contextSize uint64) uint64
func AddNewValueToCache(key string, value string) {
context := []byte(`{
"key": "` + key + `",
"value": ` + value + `
}`)
_ProxyPluginMapSet(ByteArrPtr(context), uint64(len(context)))
}
func GetValueFromCache(key string) (string, bool) {
value := pointerToBytes(
_ProxyPluginMapGet(StringBytePtr(key), uint64(len(key))))
if len(value) > 0 {
return string(value), true
} else {
return "", false
}
}
func StringBytePtr(msg string) uint64 {
mem := pdk.AllocateString(msg)
return mem.Offset()
}
func ByteArrPtr(arr []byte) uint64 {
mem := pdk.AllocateBytes(arr)
return mem.Offset()
}
func pointerToBytes(p uint64) []byte {
responseMemory := pdk.FindMemory(p)
buf := make([]byte, int(responseMemory.Length()))
responseMemory.Load(buf)
return buf
}
//export GetCompleteAddress
func GetCompleteAddress() int32 {
input := pdk.Input()
var value, dataType, offset, err = jsonparser.Get([]byte(input), "request", "query", "q")
_ = dataType
_ = offset
if err != nil {
mem := pdk.AllocateString(`{
status: 400,
error: {\"error\": \"missing query param q\"}
}`)
pdk.OutputMemory(mem)
return 0
}
var queries []string
if err := json.Unmarshal([]byte(value), &queries); err != nil {
mem := pdk.AllocateString(`{
status: 400,
error: {\"error\": \"failed to convert param to strings list\"}
}`)
pdk.OutputMemory(mem)
return 0
}
search := string(queries[0])
var output_address = string("")
if cachedAddress, found := GetValueFromCache(search); found {
output_address = cachedAddress
} else {
address, err := GetAddress(search)
if err != nil {
mem := pdk.AllocateString(`{
status: 400,
error: {\"body_str\": \"Failed fetching address with given input\"}
}`)
pdk.OutputMemory(mem)
return 0
}
out, err := json.Marshal(address)
if err != nil {
mem := pdk.AllocateString(`{
status: 400,
error: {\"body_str\": \"Failed to marshal response\"}
}`)
pdk.OutputMemory(mem)
return 0
}
AddNewValueToCache(search, string(out))
output_address = string(out)
}
output := `{
"status": 200,
"body_json": ` + output_address + `
}`
mem := pdk.AllocateString(output)
pdk.OutputMemory(mem)
return 0
}
func GetAddress(search string) (string, error) {
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://api-adresse.data.gouv.fr/search/?q="+search)
req.SetHeader("q", search)
res := req.Send()
if res.Status() != 200 {
return "", errors.New("Failed fetching address with given input")
}
body := res.Body()
return string(body), nil
}
func main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment