Skip to content

Commit

Permalink
refactor!: use eludris-api-types.go (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 authored Mar 21, 2023
1 parent 689da6b commit 46ff1ae
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 105 deletions.
5 changes: 2 additions & 3 deletions _test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (

"github.com/eludris-community/eludris.go/client"
"github.com/eludris-community/eludris.go/events"
"github.com/eludris-community/eludris.go/interfaces"
)

func onMessage(msg *events.MessageEvent) {
c := msg.Client()

func onMessage(msg *events.MessageEvent, c interfaces.Client) {
if msg.Author == "hello" {
return
}
Expand Down
14 changes: 7 additions & 7 deletions client/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
"strconv"
"strings"

"github.com/eludris-community/eludris.go/types"
"github.com/eludris-community/eludris-api-types.go/effis"
)

// UploadAttachment uploads an attachment to the file server.
func (c clientImpl) UploadAttachment(file io.Reader, spoiler bool) (types.FileData, error) {
func (c clientImpl) UploadAttachment(file io.Reader, spoiler bool) (effis.FileData, error) {
return c.UploadFile("attachments", file, spoiler)
}

// UploadStaticFile uploads a file to the file server with the chosen "bucket".
func (c clientImpl) UploadFile(bucket string, file io.Reader, spoiler bool) (types.FileData, error) {
var res types.FileData
func (c clientImpl) UploadFile(bucket string, file io.Reader, spoiler bool) (effis.FileData, error) {
var res effis.FileData
_, err := c.request(
Effis,
"POST",
Expand Down Expand Up @@ -46,13 +46,13 @@ func (c clientImpl) FetchFile(bucket, id string) (io.ReadCloser, error) {
}

// FetchAttachmentData fetches the metadata of an attachment.
func (c clientImpl) FetchAttachmentData(id string) (types.FileData, error) {
func (c clientImpl) FetchAttachmentData(id string) (effis.FileData, error) {
return c.FetchFileData("attachments", id)
}

// FetchFileData fetches the metadata of a file.
func (c clientImpl) FetchFileData(bucket, id string) (types.FileData, error) {
var res types.FileData
func (c clientImpl) FetchFileData(bucket, id string) (effis.FileData, error) {
var res effis.FileData
_, err := c.request(Effis, "GET", fmt.Sprintf("/%s/%s", bucket, id), Data{}, &res)

return res, err
Expand Down
5 changes: 3 additions & 2 deletions client/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/eludris-community/eludris.go/events"
"github.com/eludris-community/eludris.go/interfaces"
"github.com/gorilla/websocket"
)

Expand All @@ -21,13 +22,13 @@ func (c clientImpl) Connect() error {
done := make(chan struct{})
pongs := make(chan struct{})

dispatchPing := func(*events.PongEvent) {
dispatchPong := func(*events.PongEvent, interfaces.Client) {
pongs <- struct{}{}
}

go func() {
defer close(done)
events.Subscribe(c.eventManager, dispatchPing)
events.Subscribe(c.eventManager, dispatchPong)
go ping(conn, pongs)
defer conn.Close()

Expand Down
8 changes: 3 additions & 5 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/eludris-community/eludris.go/types"
)

type RequestType int
Expand Down Expand Up @@ -109,9 +108,8 @@ func (c clientImpl) request(reqType RequestType, method, path string, data Data,
json.NewDecoder(res.Body).Decode(&obj)
return res, nil
case 429:
var ratelimit types.RateLimit
json.NewDecoder(res.Body).Decode(&ratelimit)
retry_after := ratelimit.Data.RetryAfter
// TODO: Better ratelimiting using proper headers.
retry_after, _ := strconv.Atoi(res.Header.Get("X-RateLimit-Reset"))
time.Sleep(time.Duration(retry_after) * time.Millisecond)
default:
return nil, fmt.Errorf("error sending message: %s", res.Status)
Expand Down
8 changes: 4 additions & 4 deletions client/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

package client

import "github.com/eludris-community/eludris.go/types"
import "github.com/eludris-community/eludris-api-types.go/oprish"

// SendMessage sends a message to Eludris.
func (c clientImpl) SendMessage(author, content string) (types.Message, error) {
msg := types.Message{Author: author, Content: content}
func (c clientImpl) SendMessage(author, content string) (oprish.Message, error) {
msg := oprish.Message{Author: author, Content: content}

var res types.Message
var res oprish.Message
_, err := c.request(Oprish, "POST", "/messages/", Data{Json: msg}, &res)

return res, err
Expand Down
28 changes: 15 additions & 13 deletions events/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ import (
)

type EventListener interface {
Handle(Event)
Handle(Event, interfaces.Client)
Op() string
Func() func(Event)
Func() func(Event, interfaces.Client)
}

type eventListener[E Event] struct {
op string
f func(E)
f func(E, interfaces.Client)
}

func (e eventListener[E]) Op() string {
return e.op
}

func (e eventListener[E]) Func() func(Event) {
// THe pain behind it all.
return any(e.f).(func(Event))
func (e eventListener[E]) Func() func(Event, interfaces.Client) {
// Turn the inner function into a normal `Event` from `E`.
return any(e.f).(func(Event, interfaces.Client))
}

func (l eventListener[E]) Handle(event Event) {
func (l eventListener[E]) Handle(event Event, client interfaces.Client) {
if event, ok := event.(E); ok {
l.f(event)
l.f(event, client)
}
}

Expand All @@ -50,10 +50,11 @@ type managerImpl struct {

// Subscribe allows you to subscribe to an event to the given manager.
// This infers the event type from the function signature.
func Subscribe[E Event](m EventManager, subscriber func(E)) {
func Subscribe[E Event](m EventManager, subscriber func(E, interfaces.Client)) {
t := reflect.TypeOf(subscriber)
eventT := t.In(0)

// Get the actual Op() method.
for i := 0; i < eventT.NumMethod(); i++ {
method := eventT.Method(i)
if method.Name == "Op" {
Expand All @@ -66,20 +67,23 @@ func Subscribe[E Event](m EventManager, subscriber func(E)) {
}

func (m *managerImpl) Subscribe(listener EventListener) {
// Create the slice if it doesn't exist.
if _, ok := m.subscribers[listener.Op()]; !ok {
m.subscribers[listener.Op()] = []EventListener{}
m.subscribers[listener.Op()] = make([]EventListener, 0)
}

m.subscribers[listener.Op()] = append(m.subscribers[listener.Op()], listener)
}

func (m *managerImpl) Dispatch(client interfaces.Client, data []byte) {
// Treat it as a map at first.
var msg map[string]any
json.NewDecoder(bytes.NewBuffer(data)).Decode(&msg)

op := msg["op"].(string)
innerData := msg["d"]
if innerData == nil {
// This is the actual data we want to decode.
innerData = make(map[string]any)
}

Expand Down Expand Up @@ -107,10 +111,8 @@ func (m *managerImpl) Dispatch(client interfaces.Client, data []byte) {
return
}

event.SetClient(client)

for _, subscriber := range subscribers {
subscriber.Handle(event)
subscriber.Handle(event, client)
}
}

Expand Down
23 changes: 1 addition & 22 deletions events/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,13 @@

package events

import (
"github.com/eludris-community/eludris.go/interfaces"
)

// Event represents the base fields for all events.
type Event interface {
Op() string
Client() interfaces.Client
SetClient(interfaces.Client)
}

type event struct {
client interfaces.Client
}

func (e event) Client() interfaces.Client {
return e.client
}

func (e *event) SetClient(client interfaces.Client) {
e.client = client
}

// MessageEvent represents a received message from Eludris.
type MessageEvent struct {
event
Content string `mapstructure:"content"`
Author string `mapstructure:"author"`
}
Expand All @@ -36,9 +17,7 @@ func (*MessageEvent) Op() string {
return "MESSAGE_CREATE"
}

type PongEvent struct {
event
}
type PongEvent struct{}

func (*PongEvent) Op() string {
return "PONG"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ replace github.com/eludris-community/eludris.go => ./
require github.com/gorilla/websocket v1.5.0

require github.com/mitchellh/mapstructure v1.5.0

require github.com/eludris-community/eludris-api-types.go v1.2.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/eludris-community/eludris-api-types.go v1.2.0 h1:cDyGO9u4+g+xQ8paN2ef3+Rji0BdN+Q2kksDN7G76kA=
github.com/eludris-community/eludris-api-types.go v1.2.0/go.mod h1:YUpXUc6HGfrX8Em8oXjuT63kO+tAc9J3mTHNZ4ZqPRo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
13 changes: 7 additions & 6 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ package interfaces
import (
"io"

"github.com/eludris-community/eludris.go/types"
"github.com/eludris-community/eludris-api-types.go/effis"
"github.com/eludris-community/eludris-api-types.go/oprish"
)

// Client represents a client for Eludris, with functions to interact with the API.
type Client interface {
Connect() error

SendMessage(author, content string) (types.Message, error)
SendMessage(author, content string) (oprish.Message, error)

UploadAttachment(file io.Reader, spoiler bool) (types.FileData, error)
UploadFile(bucket string, file io.Reader, spoiler bool) (types.FileData, error)
UploadAttachment(file io.Reader, spoiler bool) (effis.FileData, error)
UploadFile(bucket string, file io.Reader, spoiler bool) (effis.FileData, error)
FetchAttachment(id string) (io.ReadCloser, error)
FetchFile(bucket, id string) (io.ReadCloser, error)
FetchAttachmentData(id string) (types.FileData, error)
FetchFileData(bucket, id string) (types.FileData, error)
FetchAttachmentData(id string) (effis.FileData, error)
FetchFileData(bucket, id string) (effis.FileData, error)
}
19 changes: 0 additions & 19 deletions types/file.go

This file was deleted.

9 changes: 0 additions & 9 deletions types/message.go

This file was deleted.

11 changes: 0 additions & 11 deletions types/ratelimit.go

This file was deleted.

4 changes: 0 additions & 4 deletions types/types.go

This file was deleted.

0 comments on commit 46ff1ae

Please sign in to comment.