2024-02-27 19:52:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-07-09 00:25:43 +01:00
|
|
|
baseListPath string = "/api/v1/lists"
|
2024-02-27 19:52:59 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
func (g *Client) GetAllLists() ([]model.List, error) {
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath
|
2024-02-27 19:52:59 +00:00
|
|
|
|
|
|
|
var lists []model.List
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &lists,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-02-27 19:52:59 +00:00
|
|
|
return nil, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to get the list of lists: %w",
|
2024-02-27 19:52:59 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lists, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) GetList(listID string) (model.List, error) {
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath + "/" + listID
|
2024-02-27 19:52:59 +00:00
|
|
|
|
|
|
|
var list model.List
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &list,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-02-27 19:52:59 +00:00
|
|
|
return model.List{}, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to get the list: %w",
|
2024-02-27 19:52:59 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2024-05-30 20:04:55 +01:00
|
|
|
type CreateListForm struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
|
|
|
|
}
|
2024-02-27 19:52:59 +00:00
|
|
|
|
2024-05-30 20:04:55 +01:00
|
|
|
func (g *Client) CreateList(form CreateListForm) (model.List, error) {
|
2024-05-19 11:48:36 +01:00
|
|
|
data, err := json.Marshal(form)
|
2024-02-27 19:52:59 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return model.List{}, fmt.Errorf("unable to marshal the form: %w", err)
|
2024-02-27 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath
|
2024-02-27 19:52:59 +00:00
|
|
|
|
|
|
|
var list model.List
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: &list,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-02-27 19:52:59 +00:00
|
|
|
return model.List{}, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to create the list: %w",
|
2024-02-27 19:52:59 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) UpdateList(listToUpdate model.List) (model.List, error) {
|
2024-05-19 11:48:36 +01:00
|
|
|
form := struct {
|
2024-02-27 19:52:59 +00:00
|
|
|
Title string `json:"title"`
|
|
|
|
RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
|
|
|
|
}{
|
|
|
|
Title: listToUpdate.Title,
|
|
|
|
RepliesPolicy: listToUpdate.RepliesPolicy,
|
|
|
|
}
|
|
|
|
|
2024-05-19 11:48:36 +01:00
|
|
|
data, err := json.Marshal(form)
|
2024-02-27 19:52:59 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return model.List{}, fmt.Errorf("unable to marshal the form: %w", err)
|
2024-02-27 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath + "/" + listToUpdate.ID
|
2024-02-27 19:52:59 +00:00
|
|
|
|
|
|
|
var updatedList model.List
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPut,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: &updatedList,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-02-27 19:52:59 +00:00
|
|
|
return model.List{}, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to update the list: %w",
|
2024-02-27 19:52:59 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) DeleteList(listID string) error {
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath + "/" + listID
|
2024-02-27 19:52:59 +00:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodDelete,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"received an error after sending the request to delete the list: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-02-27 19:52:59 +00:00
|
|
|
}
|
2024-05-19 11:48:36 +01:00
|
|
|
|
|
|
|
func (g *Client) AddAccountsToList(listID string, accountIDs []string) error {
|
|
|
|
form := struct {
|
|
|
|
AccountIDs []string `json:"account_ids"`
|
|
|
|
}{
|
|
|
|
AccountIDs: accountIDs,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(form)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to marshal the form: %w", err)
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath + "/" + listID + "/accounts"
|
2024-05-19 11:48:36 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-05-19 11:48:36 +01:00
|
|
|
return fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to add the accounts to the list: %w",
|
2024-05-19 11:48:36 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) RemoveAccountsFromList(listID string, accountIDs []string) error {
|
|
|
|
form := struct {
|
|
|
|
AccountIDs []string `json:"account_ids"`
|
|
|
|
}{
|
|
|
|
AccountIDs: accountIDs,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(form)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to marshal the form: %w", err)
|
2024-05-19 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-07-09 00:25:43 +01:00
|
|
|
url := g.Authentication.Instance + baseListPath + "/" + listID + "/accounts"
|
2024-05-19 11:48:36 +01:00
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodDelete,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-05-19 11:48:36 +01:00
|
|
|
return fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to remove the accounts from the list: %w",
|
2024-05-19 11:48:36 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) GetAccountsFromList(listID string, limit int) ([]model.Account, error) {
|
2024-07-09 00:25:43 +01:00
|
|
|
path := fmt.Sprintf("%s/%s/accounts?limit=%d", baseListPath, listID, limit)
|
2024-05-19 11:48:36 +01:00
|
|
|
url := g.Authentication.Instance + path
|
|
|
|
|
|
|
|
var accounts []model.Account
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
|
|
|
output: &accounts,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-05-19 11:48:36 +01:00
|
|
|
return nil, fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"received an error after sending the request to get the accounts from the list: %w",
|
2024-05-19 11:48:36 +01:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts, nil
|
|
|
|
}
|