-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (59 loc) · 1.67 KB
/
main.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
package main
import (
"net/http"
logger "github.com/inconshreveable/log15"
"io/ioutil"
"io"
"os"
)
type credentials struct {
Username string
Password string
}
var (
creds credentials
requestURL = "http://localhost:8088/message/retort.wav"
tempFilePath = "voicemails/"
)
func main() {
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
logger.Error("Unable to create request", "error", err)
return
}
creds.Username = "a_username"
creds.Password = "a_password"
req.SetBasicAuth(creds.Username, creds.Password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Error("Failed to make authenticated request", "error", err)
return
}
if resp.StatusCode != 200 {
logger.Error("status code error", "statuscode", resp.StatusCode)
}
logger.Debug("client http response", "raw resp", resp)
defer resp.Body.Close()
//create temporary file to store mailbox message audio file
wavFile, err := ioutil.TempFile(tempFilePath, "msg-*.wav")
logger.Debug("Created temp file", "filename", wavFile.Name())
if err != nil {
logger.Error("Failed to create temp file", "error", err)
return
}
defer os.Remove(wavFile.Name())
var numWritten int64
if numWritten, err = io.Copy(wavFile, resp.Body); err != nil {
logger.Error("Failed to copy mailbox message file to tmp file", "error", err)
return
}
logger.Debug("copied length from resp body", "bytes written", numWritten)
// TODO: can delete this Stat call and just call .Name() on the file object for same result
fileInfo, err := wavFile.Stat()
if err != nil {
logger.Error("unable to get stats of file: %v", err)
return
}
logger.Debug("File info", "file info", fileInfo)
}