mirror of
https://github.com/sky22333/qqbot.git
synced 2026-09-06 07:56:45 +08:00
优化sdk
This commit is contained in:
@@ -6,27 +6,37 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/sky22333/qqbot/config"
|
||||
"github.com/sky22333/qqbot/internal/notifier"
|
||||
"github.com/sky22333/qqbot/internal/bootstrap"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
notifier *notifier.Notifier
|
||||
components *bootstrap.Components
|
||||
}
|
||||
|
||||
type ClientOptions struct {
|
||||
StartCollector bool
|
||||
}
|
||||
|
||||
func New(cfg Config) (*Client, error) {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return NewWithOptions(cfg, ClientOptions{
|
||||
StartCollector: true,
|
||||
})
|
||||
}
|
||||
|
||||
func NewWithOptions(cfg Config, opts ClientOptions) (*Client, error) {
|
||||
if err := cfg.ValidateForSDK(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
n, err := notifier.New(cfg, logger)
|
||||
components, err := bootstrap.New(cfg, logger, bootstrap.Options{StartCollector: opts.StartCollector})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Client{notifier: n}, nil
|
||||
return &Client{components: components}, nil
|
||||
}
|
||||
|
||||
func NewFromConfigFile(path string) (*Client, error) {
|
||||
cfg, err := config.Load(path)
|
||||
cfg, err := config.LoadSDK(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,17 +44,17 @@ func NewFromConfigFile(path string) (*Client, error) {
|
||||
}
|
||||
|
||||
func (c *Client) Send(ctx context.Context, req PushRequest) (PushResult, error) {
|
||||
return c.notifier.Send(ctx, req)
|
||||
return c.components.Notifier.Send(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) Enqueue(ctx context.Context, req PushRequest) (string, error) {
|
||||
return c.notifier.Enqueue(ctx, req)
|
||||
return c.components.Notifier.Enqueue(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) GetStatus(requestID string) (DeliveryStatus, bool) {
|
||||
return c.notifier.GetStatus(requestID)
|
||||
return c.components.Notifier.GetStatus(requestID)
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
c.notifier.Close()
|
||||
c.components.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user