You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

3 years ago
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. )
  7. func (client *Client) GetHandlers(ctx context.Context) ([]models.EventHandler, error) {
  8. handlers := make([]models.EventHandler, 0, 16)
  9. err := client.Fetch(ctx, "GET", "/api/event-handlers", &handlers, nil)
  10. if err != nil {
  11. return nil, err
  12. }
  13. return handlers, nil
  14. }
  15. func (client *Client) GetHandler(ctx context.Context, id int) (*models.EventHandler, error) {
  16. var handler models.EventHandler
  17. err := client.Fetch(ctx, "GET", fmt.Sprintf("/api/event-handlers/%d", id), &handler, nil)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &handler, nil
  22. }
  23. func (client *Client) PutHandler(ctx context.Context, handler *models.EventHandler) (*models.EventHandler, error) {
  24. var response models.EventHandler
  25. err := client.Fetch(ctx, "PUT", fmt.Sprintf("/api/event-handlers/%d", handler.ID), &response, handler)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &response, nil
  30. }
  31. func (client *Client) AddHandler(ctx context.Context, handler models.EventHandler) (*models.EventHandler, error) {
  32. var result models.EventHandler
  33. err := client.Fetch(ctx, "POST", "/api/event-handlers", &result, handler)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &result, nil
  38. }
  39. func (client *Client) DeleteHandler(ctx context.Context, id int) (*models.EventHandler, error) {
  40. var handler models.EventHandler
  41. err := client.Fetch(ctx, "DELETE", fmt.Sprintf("/api/event-handlers/%d", id), &handler, nil)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &handler, nil
  46. }