Add more contacts API endpoints

This commit is contained in:
emersion 2017-08-22 13:22:27 +02:00
parent 67d7c932fe
commit 1978c7ced0
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 93 additions and 28 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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