hydroxide-push/protonmail/contacts.go

186 lines
3.4 KiB
Go
Raw Normal View History

2017-08-22 13:40:27 +03:00
package protonmail
import (
"net/http"
2017-09-03 21:11:01 +03:00
"net/url"
"strconv"
2017-08-22 13:40:27 +03:00
)
type Contact struct {
2017-08-22 14:22:27 +03:00
ID string
Name string
2017-09-03 21:11:01 +03:00
UID string
Size int
CreateTime int
ModifyTime int
2017-08-22 13:40:27 +03:00
LabelIDs []string
2017-09-03 21:11:01 +03:00
// Not when using ListContacts
ContactEmails []*ContactEmail
Cards []*ContactCard
2017-08-22 13:40:27 +03:00
}
2017-09-03 21:11:01 +03:00
type ContactEmailDefaults int
2017-08-22 13:40:27 +03:00
type ContactEmail struct {
2017-08-22 14:22:27 +03:00
ID string
Email string
2017-09-03 21:11:01 +03:00
Type []string
Defaults ContactEmailDefaults
2017-08-22 14:22:27 +03:00
Order int
2017-08-22 13:40:27 +03:00
ContactID string
2017-08-22 14:22:27 +03:00
LabelIDs []string
2017-09-03 21:11:01 +03:00
// Only when using ListContactsEmails
Name string
2017-08-22 13:40:27 +03:00
}
2017-09-03 21:11:01 +03:00
type ContactCardType int
2017-08-22 13:40:27 +03:00
2017-08-22 14:22:27 +03:00
const (
2017-09-03 21:11:01 +03:00
ContactCardCleartext ContactCardType = iota
ContactCardEncrypted
ContactCardSigned
ContactCardEncryptedAndSigned
2017-08-22 14:22:27 +03:00
)
2017-09-03 21:11:01 +03:00
func (t ContactCardType) Signed() bool {
switch t {
case ContactCardSigned, ContactCardEncryptedAndSigned:
return true
default:
return false
}
}
func (t ContactCardType) Encrypted() bool {
switch t {
case ContactCardEncrypted, ContactCardEncryptedAndSigned:
return true
default:
return false
}
}
type ContactCard struct {
Type ContactCardType
Data string
Signature string
}
type ContactExport struct {
ID string
Cards []*ContactCard
2017-08-22 13:40:27 +03:00
}
2017-08-22 14:22:27 +03:00
func (c *Client) ListContacts() ([]*Contact, error) {
req, err := c.newRequest(http.MethodGet, "/contacts", nil)
if err != nil {
return nil, err
}
var respData struct {
resp
Contacts []*Contact
}
if err := c.doJSON(req, &respData); err != nil {
return nil, err
}
return respData.Contacts, nil
2017-08-22 13:40:27 +03:00
}
2017-09-03 21:11:01 +03:00
func (c *Client) ListContactsEmails(page, pageSize int) (total int, emails []*ContactEmail, err error) {
v := url.Values{}
v.Set("Page", strconv.Itoa(page))
if pageSize > 0 {
v.Set("PageSize", strconv.Itoa(pageSize))
}
req, err := c.newRequest(http.MethodGet, "/contacts/emails?"+v.Encode(), nil)
2017-08-22 13:40:27 +03:00
if err != nil {
2017-09-03 21:11:01 +03:00
return 0, nil, err
2017-08-22 13:40:27 +03:00
}
2017-08-22 14:22:27 +03:00
var respData struct {
resp
2017-09-03 21:11:01 +03:00
ContactEmails []*ContactEmail
Total int
2017-08-22 14:22:27 +03:00
}
2017-08-22 13:40:27 +03:00
if err := c.doJSON(req, &respData); err != nil {
2017-09-03 21:11:01 +03:00
return 0, nil, err
2017-08-22 13:40:27 +03:00
}
2017-09-03 21:11:01 +03:00
return respData.Total, respData.ContactEmails, nil
}
func (c *Client) ListContactsExport(page, pageSize int) (total int, contacts []*ContactExport, err error) {
v := url.Values{}
v.Set("Page", strconv.Itoa(page))
if pageSize > 0 {
v.Set("PageSize", strconv.Itoa(pageSize))
}
req, err := c.newRequest(http.MethodGet, "/contacts/export?"+v.Encode(), nil)
if err != nil {
return 0, nil, err
}
var respData struct {
resp
Contacts []*ContactExport
Total int
}
if err := c.doJSON(req, &respData); err != nil {
return 0, nil, err
}
return respData.Total, respData.Contacts, nil
2017-08-22 13:40:27 +03:00
}
2017-08-22 14:22:27 +03:00
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
}