diff --git a/protonmail/auth.go b/protonmail/auth.go index 3b27054..83ae8f3 100644 --- a/protonmail/auth.go +++ b/protonmail/auth.go @@ -156,22 +156,22 @@ func (c *Client) Auth(username, password, twoFactorCode string, info *AuthInfo) } type authRefreshReq struct { - ClientID string - UID string `json:"Uid"` + ClientID string + UID string `json:"Uid"` RefreshToken string // Unused but required ResponseType string - GrantType string - RedirectURI string - State string + GrantType string + RedirectURI string + State string } func (c *Client) AuthRefresh(expiredAuth *Auth) (*Auth, error) { reqData := &authRefreshReq{ - ClientID: c.ClientID, - UID: expiredAuth.UID, - RefreshToken: expiredAuth.RefreshToken, + ClientID: c.ClientID, + UID: expiredAuth.UID, + RefreshToken: expiredAuth.RefreshToken, } req, err := c.newJSONRequest(http.MethodPost, "/auth/refresh", reqData) diff --git a/protonmail/contacts.go b/protonmail/contacts.go index f6d5240..9d68fbd 100644 --- a/protonmail/contacts.go +++ b/protonmail/contacts.go @@ -5,47 +5,112 @@ import ( ) type Contact struct { - ID string - Name string + ID string + Name string LabelIDs []string Emails []*ContactEmail - Data []*ContactData + Data []*ContactData } type ContactEmail struct { - ID string - Name string - Email string - Type string - Encrypt int - Order int + ID string + Name string + Email string + Type string + Encrypt int + Order int ContactID string - LabelIDs []string + LabelIDs []string } type ContactDataType int +const ( + ContactDataEncrypted ContactDataType = 1 +) + type ContactData struct { Type ContactDataType Data string } -type contactsResp struct { - resp - Contacts []*Contact -} - -func (c *Client) Contacts() ([]*Contact, error) { +func (c *Client) ListContacts() ([]*Contact, error) { req, err := c.newRequest(http.MethodGet, "/contacts", nil) if err != nil { return nil, err } - var respData contactsResp + var respData struct { + resp + Contacts []*Contact + } if err := c.doJSON(req, &respData); err != nil { return nil, err } return respData.Contacts, nil } + +func (c *Client) ListContactsEmails() ([]*ContactEmail, error) { + req, err := c.newRequest(http.MethodGet, "/contacts/emails", nil) + if err != nil { + return nil, err + } + + var respData struct { + resp + Contacts []*ContactEmail + } + if err := c.doJSON(req, &respData); err != nil { + return nil, err + } + + return respData.Contacts, nil +} + +func (c *Client) GetContact(id string) (*Contact, error) { + req, err := c.newRequest(http.MethodGet, "/contacts/"+id, nil) + if err != nil { + return nil, err + } + + var respData struct { + resp + Contact *Contact + } + if err := c.doJSON(req, &respData); err != nil { + return nil, err + } + + return respData.Contact, nil +} + +type CreateContactResp struct { + Input *Contact + Response struct { + resp + Contact *Contact + } +} + +func (c *Client) CreateContacts(contacts []*Contact) ([]*CreateContactResp, error) { + reqData := struct { + Contacts []*Contact + }{contacts} + + req, err := c.newJSONRequest(http.MethodPost, "/contacts", &reqData) + if err != nil { + return nil, err + } + + var respData struct { + resp + Responses []*CreateContactResp + } + if err := c.doJSON(req, &respData); err != nil { + return nil, err + } + + return respData.Responses, nil +} diff --git a/protonmail/protonmail.go b/protonmail/protonmail.go index 10df4c9..58dad80 100644 --- a/protonmail/protonmail.go +++ b/protonmail/protonmail.go @@ -49,9 +49,9 @@ type Client struct { HTTPClient *http.Client - uid string + uid string accessToken string - keyRing openpgp.EntityList + keyRing openpgp.EntityList } func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, error) { @@ -65,7 +65,7 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, if c.uid != "" && c.accessToken != "" { req.Header.Set("X-Pm-Uid", c.uid) - req.Header.Set("Authorization", "Bearer " + c.accessToken) + req.Header.Set("Authorization", "Bearer "+c.accessToken) } return req, nil