-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
79 lines (59 loc) · 1.51 KB
/
message.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
// Copyright 2019 nic.at GmbH. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rc0go
import (
"encoding/json"
"errors"
)
type MessageService service
type Message struct {
ID int `json:"id"`
Domain string `json:"domain"`
Date string `json:"date"`
Type string `json:"type"`
Comment string `json:"comment"`
}
// Retrieves the oldest unacknowledged message from the message queue
//
// rcode0 API doc: https://my.rcodezero.at/api-doc/#api-message-queue-poll-message-get
func (s *MessageService) GetLatest()(*Message, error) {
resp, err := s.client.NewRequest().
Get(
s.client.BaseURL.String() +
s.client.APIVersion +
RC0Messages,
)
if err != nil {
return nil, err
}
var message *Message
err = json.Unmarshal(resp.Body(), &message)
if err != nil {
return nil, err
}
return message, nil
}
// Acknowlegdes (and deletes) the message with the given id
//
// rcode0 API doc: https://my.rcodezero.at/api-doc/#api-message-queue-ack-message-delete
func (s *MessageService) AckAndDelete(id int) (*StatusResponse, error){
resp, err := s.client.NewRequest().
Delete(
s.client.BaseURL.String() +
s.client.APIVersion +
RC0AckMessage,
)
if resp.StatusCode() == 404 {
status, err := s.client.ResponseToRC0StatusResponse(resp)
if err != nil {
return nil, err
}
return nil, errors.New(status.Message)
}
if err != nil {
return nil, err
}
return s.client.ResponseToRC0StatusResponse(resp)
}