-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
150 lines (116 loc) · 3.15 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// a go package to interact with Nextcloud Deck API
package ncdeck
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type Client struct {
BaseURL string
username string
password string
Client *http.Client
}
// Create new Nextcloud Deck Client
func NewClient(username, password, baseURL string) *Client {
return &Client{
BaseURL: baseURL + "/index.php/apps/deck/api/v1.2",
username: username,
password: password,
Client: &http.Client{Timeout: time.Second * 60},
}
}
// Execute HTTP requests
func (c Client) do(req *http.Request, obj interface{}) error {
req.Header.Add("OCS-APIRequest", "true")
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(c.username, c.password)
res, err := c.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("status code: %d\nbody: %v", res.StatusCode, res.Body)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// log.Printf("URL: %v\nResponse body: %v\n", req.URL, string(body))
return json.Unmarshal(body, &obj)
}
// Get Stacks of Board ID
func (c Client) GetStacks(boardid, stackid int) (Stacks, error) {
board := Board{ID: boardid}
board.Stacks = append(board.Stacks, Stack{ID: stackid, BoardID: boardid, client: &c})
return board.GetStacks()
}
// Get cards from a stack
func (c Client) GetCards(boardid, stackid int) (Cards, error) {
board := Board{ID: boardid}
board.Stacks = append(board.Stacks, Stack{ID: stackid, BoardID: boardid, client: &c})
return board.Stacks[0].GetCards()
}
// Create card on specific board and stack
func (c Client) CreateCard(boardid, stackid int, title, description string, order int, duedate string) (Card, error) {
board := Board{ID: boardid}
board.Stacks = append(board.Stacks, Stack{ID: stackid, BoardID: boardid, client: &c})
card, err := board.Stacks[0].CreateCard(title, description, order, duedate)
if err != nil {
return Card{}, err
}
return card, err
}
// Create a new board
func (c Client) CreateBoard(title, color string) (Board, error) {
var url = c.BaseURL + "/boards"
body := fmt.Sprintf(`{"title":"%v", "color": "%v"}`, title, color)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return Board{}, err
}
req.Body = io.NopCloser(strings.NewReader(body))
var board Board
err = c.do(req, &board)
if err != nil {
return Board{}, err
}
board.client = &c
return board, nil
}
// Get all boards
func (c Client) GetBoards() (Boards, error) {
var url = c.BaseURL + "/boards"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Boards{}, err
}
var boards Boards
err = c.do(req, &boards)
if err != nil {
return Boards{}, err
}
for _, board := range boards {
board.client = &c
}
return boards, nil
}
// Get board by id
func (c Client) GetBoard(boardid int) (Board, error) {
var url = c.BaseURL + fmt.Sprintf("/boards/%v", boardid)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Board{}, err
}
var board Board
board.client = &c
err = c.do(req, &board)
if err != nil {
return Board{}, err
}
return board, nil
}