@@ -4,10 +4,12 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
+ "io"
7
8
"io/ioutil"
8
9
"net/http"
9
10
"net/url"
10
11
"strings"
12
+ "sync/atomic"
11
13
12
14
"github.com/containrrr/shoutrrr/pkg/types"
13
15
"github.com/containrrr/shoutrrr/pkg/util"
@@ -17,6 +19,7 @@ type client struct {
17
19
apiURL url.URL
18
20
accessToken string
19
21
logger types.StdLogger
22
+ counter uint64
20
23
}
21
24
22
25
func newClient (host string , disableTLS bool , logger types.StdLogger ) (c * client ) {
@@ -41,6 +44,10 @@ func newClient(host string, disableTLS bool, logger types.StdLogger) (c *client)
41
44
return c
42
45
}
43
46
47
+ func (c * client ) txId () uint64 {
48
+ return atomic .AddUint64 (& c .counter , 1 )
49
+ }
50
+
44
51
func (c * client ) useToken (token string ) {
45
52
c .accessToken = token
46
53
c .updateAccessToken ()
@@ -127,6 +134,10 @@ func (c *client) sendToJoinedRooms(message string) (errors []error) {
127
134
return append (errors , fmt .Errorf ("failed to get joined rooms: %w" , err ))
128
135
}
129
136
137
+ if len (joinedRooms ) == 0 {
138
+ return append (errors , fmt .Errorf ("no rooms has been joined" ))
139
+ }
140
+
130
141
// Send to all rooms that are joined
131
142
for _ , roomID := range joinedRooms {
132
143
c .logf ("Sending message to '%v'...\n " , roomID )
@@ -148,63 +159,49 @@ func (c *client) joinRoom(room string) (roomID string, err error) {
148
159
149
160
func (c * client ) sendMessageToRoom (message string , roomID string ) error {
150
161
resEvent := apiResEvent {}
151
- return c .apiPost (fmt .Sprintf (apiSendMessage , roomID ), apiReqSend {
162
+ return c .apiPut (fmt .Sprintf (apiSendMessage , roomID , c . txId () ), apiReqSend {
152
163
MsgType : msgTypeText ,
153
164
Body : message ,
154
165
}, & resEvent )
155
166
}
156
167
157
168
func (c * client ) apiGet (path string , response interface {}) error {
158
- c .apiURL .Path = path
159
-
160
- var err error
161
- var res * http.Response
162
- res , err = http .Get (c .apiURL .String ())
163
- if err != nil {
164
- return err
165
- }
166
-
167
- var body []byte
168
- defer res .Body .Close ()
169
- body , err = ioutil .ReadAll (res .Body )
170
-
171
- if res .StatusCode >= 400 {
172
- resError := & apiResError {}
173
- if err == nil {
174
- if err = json .Unmarshal (body , resError ); err == nil {
175
- return resError
176
- }
177
- }
178
-
179
- return fmt .Errorf ("got HTTP %v" , res .Status )
180
- }
169
+ return c .apiReq (path , "GET" , nil , response )
170
+ }
181
171
182
- if err != nil {
183
- return err
184
- }
172
+ func ( c * client ) apiPost ( path string , request interface {}, response interface {}) error {
173
+ return c . apiReq ( path , "POST" , request , response )
174
+ }
185
175
186
- return json .Unmarshal (body , response )
176
+ func (c * client ) apiPut (path string , request interface {}, response interface {}) error {
177
+ return c .apiReq (path , "PUT" , request , response )
187
178
}
188
179
189
- func (c * client ) apiPost (path string , request interface {}, response interface {}) error {
180
+ func (c * client ) apiReq (path string , method string , request interface {}, response interface {}) error {
190
181
c .apiURL .Path = path
191
182
192
- var err error
193
- var body []byte
183
+ var payload io.Reader = nil
184
+ if request != nil {
185
+ body , err := json .Marshal (request )
186
+ if err != nil {
187
+ return err
188
+ }
189
+ payload = bytes .NewReader (body )
190
+ }
194
191
195
- body , err = json . Marshal ( request )
192
+ req , err := http . NewRequest ( method , c . apiURL . String (), payload )
196
193
if err != nil {
197
194
return err
198
195
}
196
+ req .Header .Set ("Content-Type" , contentType )
199
197
200
- var res * http.Response
201
- res , err = http .Post (c .apiURL .String (), contentType , bytes .NewReader (body ))
198
+ res , err := http .DefaultClient .Do (req )
202
199
if err != nil {
203
200
return err
204
201
}
205
202
206
203
defer res .Body .Close ()
207
- body , err = ioutil .ReadAll (res .Body )
204
+ body , err : = ioutil .ReadAll (res .Body )
208
205
209
206
if res .StatusCode >= 400 {
210
207
resError := & apiResError {}
0 commit comments