package host
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
type ScrobbleRetrieverGetFirstTimestampRequest struct {
Username string `json:"username"`
}
type ScrobbleRetrieverGetFirstTimestampResponse struct {
Result *int64 `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
type ScrobbleRetrieverGetLastTimestampRequest struct {
Username string `json:"username"`
}
type ScrobbleRetrieverGetLastTimestampResponse struct {
Result *int64 `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
type ScrobbleRetrieverGetScrobblesRequest struct {
Username string `json:"username"`
Options ScrobbleOptions `json:"options"`
}
type ScrobbleRetrieverGetScrobblesResponse struct {
Scrobbles []ScrobbleRef `json:"scrobbles,omitempty"`
Next *ScrobbleOptions `json:"next,omitempty"`
Error string `json:"error,omitempty"`
}
type ScrobbleRetrieverGetScrobbleCountRequest struct {
Username string `json:"username"`
Options ScrobbleCountOptions `json:"options"`
}
type ScrobbleRetrieverGetScrobbleCountResponse struct {
Result int64 `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
func RegisterScrobbleRetrieverHostFunctions(service ScrobbleRetrieverService) []extism.HostFunction {
return []extism.HostFunction{
newScrobbleRetrieverGetFirstTimestampHostFunction(service),
newScrobbleRetrieverGetLastTimestampHostFunction(service),
newScrobbleRetrieverGetScrobblesHostFunction(service),
newScrobbleRetrieverGetScrobbleCountHostFunction(service),
}
}
func newScrobbleRetrieverGetFirstTimestampHostFunction(service ScrobbleRetrieverService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"scrobbleretriever_getfirsttimestamp",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
var req ScrobbleRetrieverGetFirstTimestampRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
result, svcErr := service.GetFirstTimestamp(ctx, req.Username)
if svcErr != nil {
scrobbleretrieverWriteError(p, stack, svcErr)
return
}
resp := ScrobbleRetrieverGetFirstTimestampResponse{
Result: result,
}
scrobbleretrieverWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newScrobbleRetrieverGetLastTimestampHostFunction(service ScrobbleRetrieverService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"scrobbleretriever_getlasttimestamp",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
var req ScrobbleRetrieverGetLastTimestampRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
result, svcErr := service.GetLastTimestamp(ctx, req.Username)
if svcErr != nil {
scrobbleretrieverWriteError(p, stack, svcErr)
return
}
resp := ScrobbleRetrieverGetLastTimestampResponse{
Result: result,
}
scrobbleretrieverWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newScrobbleRetrieverGetScrobblesHostFunction(service ScrobbleRetrieverService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"scrobbleretriever_getscrobbles",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
var req ScrobbleRetrieverGetScrobblesRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
scrobbles, next, svcErr := service.GetScrobbles(ctx, req.Username, req.Options)
if svcErr != nil {
scrobbleretrieverWriteError(p, stack, svcErr)
return
}
resp := ScrobbleRetrieverGetScrobblesResponse{
Scrobbles: scrobbles,
Next: next,
}
scrobbleretrieverWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newScrobbleRetrieverGetScrobbleCountHostFunction(service ScrobbleRetrieverService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"scrobbleretriever_getscrobblecount",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
var req ScrobbleRetrieverGetScrobbleCountRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
result, svcErr := service.GetScrobbleCount(ctx, req.Username, req.Options)
if svcErr != nil {
scrobbleretrieverWriteError(p, stack, svcErr)
return
}
resp := ScrobbleRetrieverGetScrobbleCountResponse{
Result: result,
}
scrobbleretrieverWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func scrobbleretrieverWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
scrobbleretrieverWriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
func scrobbleretrieverWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
errResp := struct {
Error string `json:"error"`
}{Error: err.Error()}
respBytes, _ := json.Marshal(errResp)
respPtr, _ := p.WriteBytes(respBytes)
stack[0] = respPtr
}