Dan Anglin
ccdd8b6530
Add a new internal printer package for printing resources to the screen or pager. With the new printer in place, most of the settings such as the pager command, colour theme, whether or not colour output is disabled, etc are defined in one place which saves us the trouble of passing an increasing number of parameters to an increasing number of Display methods throughout the code base. The old Displayer interface and associated Display methods in the model package are removed as this is now handled by the printer. The format functions in the utilities package has essentially been rewritten as methods to the Printer type. Additional changes: - All indentation when displaying information about resources (e.g. statuses, instance, accounts) are removed. - The application's build information now has colour output.
175 lines
4.3 KiB
Go
175 lines
4.3 KiB
Go
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
)
|
|
|
|
const (
|
|
listPath string = "/api/v1/lists"
|
|
)
|
|
|
|
func (g *Client) GetAllLists() ([]model.List, error) {
|
|
url := g.Authentication.Instance + listPath
|
|
|
|
var lists []model.List
|
|
|
|
if err := g.sendRequest(http.MethodGet, url, nil, &lists); err != nil {
|
|
return nil, fmt.Errorf(
|
|
"received an error after sending the request to get the list of lists: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return lists, nil
|
|
}
|
|
|
|
func (g *Client) GetList(listID string) (model.List, error) {
|
|
url := g.Authentication.Instance + listPath + "/" + listID
|
|
|
|
var list model.List
|
|
|
|
if err := g.sendRequest(http.MethodGet, url, nil, &list); err != nil {
|
|
return model.List{}, fmt.Errorf(
|
|
"received an error after sending the request to get the list: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return list, nil
|
|
}
|
|
|
|
type CreateListForm struct {
|
|
Title string `json:"title"`
|
|
RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
|
|
}
|
|
|
|
func (g *Client) CreateList(form CreateListForm) (model.List, error) {
|
|
data, err := json.Marshal(form)
|
|
if err != nil {
|
|
return model.List{}, fmt.Errorf("unable to marshal the form: %w", err)
|
|
}
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
url := g.Authentication.Instance + "/api/v1/lists"
|
|
|
|
var list model.List
|
|
|
|
if err := g.sendRequest(http.MethodPost, url, requestBody, &list); err != nil {
|
|
return model.List{}, fmt.Errorf(
|
|
"received an error after sending the request to create the list: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return list, nil
|
|
}
|
|
|
|
func (g *Client) UpdateList(listToUpdate model.List) (model.List, error) {
|
|
form := struct {
|
|
Title string `json:"title"`
|
|
RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
|
|
}{
|
|
Title: listToUpdate.Title,
|
|
RepliesPolicy: listToUpdate.RepliesPolicy,
|
|
}
|
|
|
|
data, err := json.Marshal(form)
|
|
if err != nil {
|
|
return model.List{}, fmt.Errorf("unable to marshal the form: %w", err)
|
|
}
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
url := g.Authentication.Instance + listPath + "/" + listToUpdate.ID
|
|
|
|
var updatedList model.List
|
|
|
|
if err := g.sendRequest(http.MethodPut, url, requestBody, &updatedList); err != nil {
|
|
return model.List{}, fmt.Errorf(
|
|
"received an error after sending the request to update the list: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return updatedList, nil
|
|
}
|
|
|
|
func (g *Client) DeleteList(listID string) error {
|
|
url := g.Authentication.Instance + "/api/v1/lists/" + listID
|
|
|
|
return g.sendRequest(http.MethodDelete, url, nil, nil)
|
|
}
|
|
|
|
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 {
|
|
return fmt.Errorf("unable to marshal the form: %w", err)
|
|
}
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
url := g.Authentication.Instance + listPath + "/" + listID + "/accounts"
|
|
|
|
if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil {
|
|
return fmt.Errorf(
|
|
"received an error after sending the request to add the accounts to the list: %w",
|
|
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 {
|
|
return fmt.Errorf("unable to marshal the form: %w", err)
|
|
}
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
url := g.Authentication.Instance + listPath + "/" + listID + "/accounts"
|
|
|
|
if err := g.sendRequest(http.MethodDelete, url, requestBody, nil); err != nil {
|
|
return fmt.Errorf(
|
|
"received an error after sending the request to remove the accounts from the list: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *Client) GetAccountsFromList(listID string, limit int) ([]model.Account, error) {
|
|
path := fmt.Sprintf("%s/%s/accounts?limit=%d", listPath, listID, limit)
|
|
url := g.Authentication.Instance + path
|
|
|
|
var accounts []model.Account
|
|
|
|
if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil {
|
|
return nil, fmt.Errorf(
|
|
"received an error after sending the request to get the accounts from the list: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return accounts, nil
|
|
}
|