/api/v1/streaming/public/local

master
Charles Iliya Krempeaux 2024-08-07 08:05:46 -07:00
parent e380da4d0c
commit 4daf030d7e
4 changed files with 171 additions and 4 deletions

View File

@ -1,7 +1,132 @@
package local
import (
"encoding/json"
"fmt"
"github.com/reiver/go-httpsse"
"github.com/reiver/go-opt"
)
// Client represenrs a client for interacting with the "/api/v1/streaming/public/local" API end-point.
//
// That API end-point will return a series of events.
//
// Client will let you iterate through those events.
type Client = httpsse.Client
type internalClient struct {
sseclient httpsse.Client
data []string
err error
}
var _ Client = &internalClient{}
func (receiver *internalClient) Close() error {
if nil == receiver {
return errNilReceiver
}
var sseclient httpsse.Client = receiver.sseclient
if nil == sseclient {
return errNilHTTPSSEClient
}
return sseclient.Close()
}
func (receiver *internalClient) Decode(dst interface{}) error {
if nil == dst {
return errNilDestination
}
var event *Event
{
var casted bool
event, casted = dst.(*Event)
if !casted {
}
}
if len(receiver.data) <= 0 {
}
var datum string
{
datum, receiver.data = receiver.data[0], receiver.data[1:]
}
fmt.Printf("DATUM:\n%s \n", datum)
if len(datum) <= 0 {
}
switch datum[0] {
case '0','1','2','3','4','5','6','7','8','9':
event.Name="delete"
event.Status.ID = opt.Something(datum)
default:
event.Name="update"
var bytes []byte = []byte(datum)
{
err := json.Unmarshal(bytes, &(event.Status))
if nil != err {
return err
}
}
}
return nil
}
func (receiver *internalClient) Err() error {
if nil == receiver {
return errNilReceiver
}
if nil != receiver.err {
return receiver.err
}
return receiver.sseclient.Err()
}
func (receiver *internalClient) Next() bool {
if nil == receiver {
return false
}
if 0 < len(receiver.data) {
return true
}
more := receiver.sseclient.Next()
if !more {
return false
}
var event httpsse.Event
{
err := receiver.sseclient.Decode(&event)
if nil != err {
receiver.err = err
return false
}
}
receiver.data = append(receiver.data, event.EventData()...)
//@TODO: what if event.EventData() returns an empty array?
// which could happen.
return true
}

View File

@ -14,9 +14,23 @@ func DialHost(host string) (Client, error) {
Path:Path,
}
return httpsse.DialURL(urloc.String())
sseclient, err := httpsse.DialURL(urloc.String())
if nil != err {
return nil, err
}
return &internalClient{
sseclient:sseclient,
}, nil
}
func Dial(req *http.Request) (Client, error) {
return httpsse.Dial(req)
sseclient, err :=httpsse.Dial(req)
if nil != err {
return nil, err
}
return &internalClient{
sseclient:sseclient,
}, nil
}

View File

@ -0,0 +1,11 @@
package local
import (
"github.com/reiver/go-erorr"
)
const (
errNilDestination = erorr.Error("mstdn: nil destination")
errNilHTTPSSEClient = erorr.Error("mstdn: nil http-sse-client")
errNilReceiver = erorr.Error("mstdn: nil receiver")
)

View File

@ -1,7 +1,24 @@
package local
import (
"github.com/reiver/go-httpsse"
"github.com/reiver/go-mstdn/ent"
)
type Event = httpsse.Event
// The "/api/v1/streaming/public/local" API end-points returns a series of events.
// Event is used to represent one of those events.
//
// And event has a 'name'.
// Currently the possible names are:
//
// • "delete"
// • "update"
//
// And event also has a status.
//
// If the event is an "update", then all of (or most of) the 'status' should be filled in.
//
// If the event is a "delete", then only the 'ID' will be filled in.
type Event struct {
Name string
Status ent.Status
}