-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagedetail.go
51 lines (43 loc) · 1.45 KB
/
messagedetail.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
package sipengine
import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
"io"
"net/http"
"time"
)
//MessageDetail is the default implementation of the CDR type data structure that will be placed
//Into the message, but can be replaced with anything that meets the requirements. It would make
//Most sense to embed this struct into another that overrides the interface methods to achieve
//both a uniform data structure (plus additions) as well as desired behaviors for CDR generation
//and transmission
type MessageDetail struct {
CallID string `json:"call_id"`
From string `json:"from"`
To string `json:"to"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
billingSystem struct {
Address string
}
}
//GetCDR is the MessageDetail variant that simply prints the struct out in pretty JSON form
func (m MessageDetail) GetCDR() (io.Reader, error) {
jsonbytes, err := json.MarshalIndent(m, "", " ")
return bytes.NewReader(jsonbytes), err
}
//SendCDR for the MessageDetail type is a basic implementation of the interface that posts the json content
//Of the message to a remote source
func (m MessageDetail) SendCDR() error {
if cdr, err := m.GetCDR(); err == nil {
resp, err := http.Post(m.billingSystem.Address, "application/json", cdr)
if err != nil {
return errors.Wrap(err, "error occurred during billing request")
}
if resp.StatusCode != 200 {
return errors.Wrap(err, "response code not ok from billing system")
}
}
return nil
}