Skip to content

Commit

Permalink
refactor!: some old uncommitted code
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Apr 26, 2024
1 parent e89cef3 commit 70fcb0c
Show file tree
Hide file tree
Showing 21 changed files with 420 additions and 319 deletions.
11 changes: 6 additions & 5 deletions _test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/eludris-community/eludris.go/v2"
"github.com/eludris-community/eludris.go/v2/client"
"github.com/eludris-community/eludris.go/v2/events"
"github.com/eludris-community/eludris.go/v2/types"
)

func onMessage(msg *events.MessageCreate) {
Expand All @@ -24,7 +25,7 @@ func onMessage(msg *events.MessageCreate) {

switch msg.Content {
case "!speed":
_, err := c.SendMessage("googwuki", "I am the fastest ever.")
_, err := c.Rest().CreateMessage(types.MessageCreate{Author: "googwuki", Content: "I am the fastest ever."})

if err != nil {
panic(err)
Expand All @@ -36,12 +37,12 @@ func onMessage(msg *events.MessageCreate) {
}
defer file.Close()

data, err := c.UploadAttachment(file, false)
data, err := c.Rest().UploadAttachment(file, false)
if err != nil {
panic(err)
}

_, err = c.SendMessage("googwuki", fmt.Sprintf("https://cdn.eludris.gay/%s", data.Id))
log.Infof("%v", data)
_, err = c.Rest().CreateMessage(types.MessageCreate{Author: "googwuki", Content: fmt.Sprintf("https://cdn.eludris.gay/%s", data.Id)})
if err != nil {
panic(err)
}
Expand All @@ -56,7 +57,7 @@ func main() {

c, err := eludris.New(
client.WithEventManagerOpts(client.WithListenerFunc(onMessage)),
client.WithHttpUrl(HTTPUrl),
client.WithApiUrl(HTTPUrl),
client.WithDefaultGateway(),
)

Expand Down
57 changes: 14 additions & 43 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,30 @@ package client

import (
"context"
"io"
"net/http"

"github.com/eludris-community/eludris-api-types.go/v2/models"
"github.com/eludris-community/eludris.go/v2/gateway"
"github.com/eludris-community/eludris.go/v2/rest"
"github.com/eludris-community/eludris.go/v2/types"
)

// Client represents a client for Eludris, with functions to interact with the API.
type Client interface {
ConnectGateway(ctx context.Context) error
EventManager() EventManager

// SendMessage sends a message to the chat.
// Returns the message that was sent.
// Author must be between 1 and 32 characters long.
// Content must be between 1 and the instance's configured max message length.
SendMessage(author, content string) (models.Message, error)

// UploadAttachment uploads a file to the instance's attachments bucket.
UploadAttachment(file io.Reader, spoiler bool) (models.FileData, error)
// UploadFile uploads a file to a specific bucket.
// Currently only "attachments" exists.
UploadFile(bucket string, file io.Reader, spoiler bool) (models.FileData, error)
// FetchAttachment fetches a file from the instance's attachments bucket.
FetchAttachment(id string) (io.ReadCloser, error)
// FetchFile fetches a file from a specific bucket.
// Currently only "attachments" exists.
FetchFile(bucket, id string) (io.ReadCloser, error)
// FetchAttachmentData fetches a file from the instance's attachments bucket.
FetchAttachmentData(id string) (models.FileData, error)
// FetchFileData fetches a file from a specific bucket.
// Currently only "attachments" exists.
FetchFileData(bucket, id string) (models.FileData, error)
Rest() rest.Rest
}

type clientImpl struct {
httpUrl string
fileUrl string
httpClient http.Client
gateway gateway.Gateway
rest rest.Rest
eventManager EventManager
rateLimiter RateLimiter
}

// BuildClient builds a client from a configuration.
func BuildClient(config *Config) (Client, error) {
if config.HttpUrl == "" {
return nil, types.ErrNoHttpUrl
if config.ApiUrl == "" {
return nil, types.ErrNoApiUrl
}

client := &clientImpl{}
Expand All @@ -63,14 +38,10 @@ func BuildClient(config *Config) (Client, error) {
}
client.eventManager = config.EventManager

if config.RateLimiter == nil {
config.RateLimiter = NewRateLimiter(config.RateLimiterOpts...)
}
client.rateLimiter = config.RateLimiter

client.httpUrl = config.HttpUrl
client.httpUrl = config.ApiUrl
if config.WsUrl == "" || config.FileUrl == "" {
info, err := client.FetchInstanceInfo()
tmp := rest.New(config.ApiUrl, "")
info, err := tmp.GetInstanceInfo()
if err != nil {
return nil, err
}
Expand All @@ -82,6 +53,10 @@ func BuildClient(config *Config) (Client, error) {
config.FileUrl = info.EffisUrl
}
}
if config.Rest == nil {
config.Rest = rest.New(config.ApiUrl, config.FileUrl, config.RestOpts...)
}
client.rest = config.Rest

if config.Gateway == nil && len(config.GatewayOpts) > 0 {
config.GatewayOpts = append([]gateway.ConfigOpt{
Expand All @@ -95,12 +70,8 @@ func BuildClient(config *Config) (Client, error) {
return client, nil
}

// FetchInstanceInfo fetches information about the instance.
func (c *clientImpl) FetchInstanceInfo() (models.InstanceInfo, error) {
var res models.InstanceInfo
_, err := c.request(InstanceInfo.Compile(nil), Data{}, &res)

return res, err
func (c *clientImpl) Rest() rest.Rest {
return c.rest
}

func (c *clientImpl) EventManager() EventManager {
Expand Down
33 changes: 9 additions & 24 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client
import (
"github.com/eludris-community/eludris-api-types.go/v2/pandemonium"
"github.com/eludris-community/eludris.go/v2/gateway"
"github.com/eludris-community/eludris.go/v2/rest"
)

type ConfigOpt func(config *Config)
Expand All @@ -13,8 +14,8 @@ type ConfigOpt func(config *Config)
// This allows you to set the URLs for your chosen Eludris instance.
// These contain defaults, notably to the "official" Eludris instance https://eludris.gay.
type Config struct {
// HttpUrl is the URL for the HTTP API, A.K.A "oprish".
HttpUrl string
// ApiUrl is the URL for the HTTP API, A.K.A "oprish".
ApiUrl string
// WSUrl is the URL for the WebSocket gateway, A.K.A "pandemonium".
// This is obtainable from HTTPUrl if not set.
WsUrl string
Expand All @@ -25,12 +26,10 @@ type Config struct {
EventManager EventManager
// Options to apply to EventManager
EventManagerOpts []EventManagerOpt
// A rate limiter to use for the client.
RateLimiter RateLimiter
// Options to apply to RateLimiter
RateLimiterOpts []RateLimiterOpt
Gateway gateway.Gateway
GatewayOpts []gateway.ConfigOpt
Gateway gateway.Gateway
GatewayOpts []gateway.ConfigOpt
Rest rest.Rest
RestOpts []rest.ConfigOpt
}

// DefaultConfig returns a new Config with default values.
Expand All @@ -48,9 +47,9 @@ func (c *Config) Apply(opts []ConfigOpt) {
}

// WithHttpUrl returns a ConfigOpt that sets the HTTP URL.
func WithHttpUrl(url string) ConfigOpt {
func WithApiUrl(url string) ConfigOpt {
return func(config *Config) {
config.HttpUrl = url
config.ApiUrl = url
}
}

Expand Down Expand Up @@ -82,20 +81,6 @@ func WithEventManagerOpts(opts ...EventManagerOpt) ConfigOpt {
}
}

// WithRateLimiter returns a ConfigOpt that sets the rate limiter.
func WithRateLimiter(limiter RateLimiter) ConfigOpt {
return func(config *Config) {
config.RateLimiter = limiter
}
}

// WithRateLimiterOpts returns a ConfigOpt that sets the rate limiter options.
func WithRateLimiterOpts(opts ...RateLimiterOpt) ConfigOpt {
return func(config *Config) {
config.RateLimiterOpts = append(config.RateLimiterOpts, opts...)
}
}

// WithGateway lets you inject your own gateway.Gateway.
func WithGateway(gateway gateway.Gateway) ConfigOpt {
return func(config *Config) {
Expand Down
39 changes: 0 additions & 39 deletions client/endpoints.go

This file was deleted.

56 changes: 0 additions & 56 deletions client/files.go

This file was deleted.

Loading

0 comments on commit 70fcb0c

Please sign in to comment.