protonmail: add Client.Debug

This prints HTTP requests and responses.
This commit is contained in:
Simon Ser 2020-03-07 18:12:53 +01:00
parent 4ede51e2b0
commit d97056a61a
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 20 additions and 9 deletions

View File

@ -26,10 +26,13 @@ import (
smtpbackend "github.com/emersion/hydroxide/smtp"
)
var debug bool
func newClient() *protonmail.Client {
return &protonmail.Client{
RootURL: "https://mail.protonmail.com/api",
AppVersion: "Web_3.16.6",
Debug: debug,
}
}
@ -134,7 +137,7 @@ Flags:
CardDAV port on which hydroxide listens, defaults to 8080`
func main() {
debug := flag.Bool("debug", false, "Enable debug logs")
flag.BoolVar(&debug, "debug", false, "Enable debug logs")
smtpHost := flag.String("smtp-host", "127.0.0.1", "Allowed SMTP email hostname on which hydroxide listens, defaults to 127.0.0.1")
smtpPort := flag.String("smtp-port", "1025", "SMTP port on which hydroxide listens, defaults to 1025")
@ -335,12 +338,12 @@ func main() {
case "smtp":
addr := *smtpHost + ":" + *smtpPort
authManager := auth.NewManager(newClient)
log.Fatal(listenAndServeSMTP(addr, *debug, authManager))
log.Fatal(listenAndServeSMTP(addr, debug, authManager))
case "imap":
addr := *imapHost + ":" + *imapPort
authManager := auth.NewManager(newClient)
eventsManager := events.NewManager()
log.Fatal(listenAndServeIMAP(addr, *debug, authManager, eventsManager))
log.Fatal(listenAndServeIMAP(addr, debug, authManager, eventsManager))
case "carddav":
addr := *carddavHost + ":" + *carddavPort
authManager := auth.NewManager(newClient)
@ -356,10 +359,10 @@ func main() {
done := make(chan error, 3)
go func() {
done <- listenAndServeSMTP(smtpAddr, *debug, authManager)
done <- listenAndServeSMTP(smtpAddr, debug, authManager)
}()
go func() {
done <- listenAndServeIMAP(imapAddr, *debug, authManager, eventsManager)
done <- listenAndServeIMAP(imapAddr, debug, authManager, eventsManager)
}()
go func() {
done <- listenAndServeCardDAV(carddavAddr, authManager, eventsManager)

View File

@ -55,6 +55,7 @@ func (err *APIError) Error() string {
type Client struct {
RootURL string
AppVersion string
Debug bool
HTTPClient *http.Client
ReAuth func() error
@ -77,7 +78,9 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
return nil, err
}
//log.Printf(">> %v %v\n", method, path)
if c.Debug {
log.Printf(">> %v %v\n", req.Method, req.URL.Path)
}
req.Header.Set("X-Pm-Appversion", c.AppVersion)
req.Header.Set(headerAPIVersion, strconv.Itoa(Version))
@ -92,13 +95,15 @@ func (c *Client) newJSONRequest(method, path string, body interface{}) (*http.Re
}
b := buf.Bytes()
//log.Printf(">> %v %v\n%v", method, path, string(b))
req, err := c.newRequest(method, path, bytes.NewReader(b))
if err != nil {
return nil, err
}
if c.Debug {
log.Print(string(b))
}
req.Header.Set("Content-Type", "application/json")
req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(b)), nil
@ -157,7 +162,10 @@ func (c *Client) doJSON(req *http.Request, respData interface{}) error {
return err
}
//log.Printf("<< %v %v\n%#v", req.Method, req.URL.Path, respData)
if c.Debug {
log.Printf("<< %v %v", req.Method, req.URL.Path)
log.Printf("%#v", respData)
}
if maybeError, ok := respData.(maybeError); ok {
if err := maybeError.Err(); err != nil {