Skip to content

Commit d97056a

Browse files
committed
protonmail: add Client.Debug
This prints HTTP requests and responses.
1 parent 4ede51e commit d97056a

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

cmd/hydroxide/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ import (
2626
smtpbackend "github.com/emersion/hydroxide/smtp"
2727
)
2828

29+
var debug bool
30+
2931
func newClient() *protonmail.Client {
3032
return &protonmail.Client{
3133
RootURL: "https://mail.protonmail.com/api",
3234
AppVersion: "Web_3.16.6",
35+
Debug: debug,
3336
}
3437
}
3538

@@ -134,7 +137,7 @@ Flags:
134137
CardDAV port on which hydroxide listens, defaults to 8080`
135138

136139
func main() {
137-
debug := flag.Bool("debug", false, "Enable debug logs")
140+
flag.BoolVar(&debug, "debug", false, "Enable debug logs")
138141

139142
smtpHost := flag.String("smtp-host", "127.0.0.1", "Allowed SMTP email hostname on which hydroxide listens, defaults to 127.0.0.1")
140143
smtpPort := flag.String("smtp-port", "1025", "SMTP port on which hydroxide listens, defaults to 1025")
@@ -335,12 +338,12 @@ func main() {
335338
case "smtp":
336339
addr := *smtpHost + ":" + *smtpPort
337340
authManager := auth.NewManager(newClient)
338-
log.Fatal(listenAndServeSMTP(addr, *debug, authManager))
341+
log.Fatal(listenAndServeSMTP(addr, debug, authManager))
339342
case "imap":
340343
addr := *imapHost + ":" + *imapPort
341344
authManager := auth.NewManager(newClient)
342345
eventsManager := events.NewManager()
343-
log.Fatal(listenAndServeIMAP(addr, *debug, authManager, eventsManager))
346+
log.Fatal(listenAndServeIMAP(addr, debug, authManager, eventsManager))
344347
case "carddav":
345348
addr := *carddavHost + ":" + *carddavPort
346349
authManager := auth.NewManager(newClient)
@@ -356,10 +359,10 @@ func main() {
356359

357360
done := make(chan error, 3)
358361
go func() {
359-
done <- listenAndServeSMTP(smtpAddr, *debug, authManager)
362+
done <- listenAndServeSMTP(smtpAddr, debug, authManager)
360363
}()
361364
go func() {
362-
done <- listenAndServeIMAP(imapAddr, *debug, authManager, eventsManager)
365+
done <- listenAndServeIMAP(imapAddr, debug, authManager, eventsManager)
363366
}()
364367
go func() {
365368
done <- listenAndServeCardDAV(carddavAddr, authManager, eventsManager)

protonmail/protonmail.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (err *APIError) Error() string {
5555
type Client struct {
5656
RootURL string
5757
AppVersion string
58+
Debug bool
5859

5960
HTTPClient *http.Client
6061
ReAuth func() error
@@ -77,7 +78,9 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
7778
return nil, err
7879
}
7980

80-
//log.Printf(">> %v %v\n", method, path)
81+
if c.Debug {
82+
log.Printf(">> %v %v\n", req.Method, req.URL.Path)
83+
}
8184

8285
req.Header.Set("X-Pm-Appversion", c.AppVersion)
8386
req.Header.Set(headerAPIVersion, strconv.Itoa(Version))
@@ -92,13 +95,15 @@ func (c *Client) newJSONRequest(method, path string, body interface{}) (*http.Re
9295
}
9396
b := buf.Bytes()
9497

95-
//log.Printf(">> %v %v\n%v", method, path, string(b))
96-
9798
req, err := c.newRequest(method, path, bytes.NewReader(b))
9899
if err != nil {
99100
return nil, err
100101
}
101102

103+
if c.Debug {
104+
log.Print(string(b))
105+
}
106+
102107
req.Header.Set("Content-Type", "application/json")
103108
req.GetBody = func() (io.ReadCloser, error) {
104109
return ioutil.NopCloser(bytes.NewReader(b)), nil
@@ -157,7 +162,10 @@ func (c *Client) doJSON(req *http.Request, respData interface{}) error {
157162
return err
158163
}
159164

160-
//log.Printf("<< %v %v\n%#v", req.Method, req.URL.Path, respData)
165+
if c.Debug {
166+
log.Printf("<< %v %v", req.Method, req.URL.Path)
167+
log.Printf("%#v", respData)
168+
}
161169

162170
if maybeError, ok := respData.(maybeError); ok {
163171
if err := maybeError.Err(); err != nil {

0 commit comments

Comments
 (0)