-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
- Loading branch information
1 parent
b211132
commit 0ee7a75
Showing
12 changed files
with
890 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package carddav | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/antchfx/xmlquery" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Client wraps functionality for calling carddav methods | ||
type Client struct { | ||
URL string | ||
User string | ||
Password string | ||
} | ||
|
||
func (c *Client) List() (items []string, err error) { | ||
req, err := http.NewRequest("PROPFIND", c.URL, nil) | ||
if err != nil { | ||
return items, errors.Wrap(err, "failed to make PROPFIND request to list items in carddav endpoint") | ||
} | ||
req.SetBasicAuth(c.User, c.Password) | ||
req.Header.Add("Depth", "1") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return items, errors.Wrap(err, "failed to make request") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode > 399 || resp.StatusCode < 100 { | ||
return items, errors.Wrap(err, "server returned error: ") | ||
} | ||
|
||
doc, err := xmlquery.Parse(resp.Body) | ||
if err != nil { | ||
return items, errors.Wrap(err, "failed parse body as xml") | ||
} | ||
|
||
list, err := xmlquery.QueryAll(doc, "D:multistatus/D:response/D:href") | ||
if err != nil { | ||
return items, errors.Wrap(err, "failed to query body for hrefs") | ||
} | ||
for _, v := range list { | ||
// only take vcf files from the list | ||
if strings.HasSuffix(v.InnerText(), "vcf") { | ||
id, err := extractID(v.InnerText()) | ||
if err != nil { | ||
return items, errors.Wrap(err, "failed to get response item ID") | ||
} | ||
items = append(items, id) | ||
} | ||
} | ||
|
||
return items, nil | ||
} | ||
|
||
func (c *Client) Delete(id string) (err error) { | ||
url := fmt.Sprintf("%s/%s.vcf", strings.TrimSuffix(c.URL, "/"), id) | ||
|
||
req, err := http.NewRequest("DELETE", url, nil) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to make DELETE request") | ||
} | ||
req.SetBasicAuth(c.User, c.Password) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to make request") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode > 399 || resp.StatusCode < 100 { | ||
return errors.Wrap(err, "server returned error: ") | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) Put(id string, vcardData string) (err error) { | ||
url := fmt.Sprintf("%s/%s.vcf", strings.TrimSuffix(c.URL, "/"), id) | ||
|
||
req, err := http.NewRequest("PUT", url, strings.NewReader(vcardData)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to make PUT request") | ||
} | ||
req.SetBasicAuth(c.User, c.Password) | ||
req.Header.Add("Content-Type", "text/vcard") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to make request") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode > 399 || resp.StatusCode < 100 { | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read error body") | ||
} | ||
return fmt.Errorf("server returned error: %d, %s", resp.StatusCode, body) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func extractID(fullPath string) (id string, err error) { | ||
parts := strings.Split(fullPath, "/") | ||
if len(parts) != 7 { | ||
return id, fmt.Errorf("unexpected number of path items: %d", len(parts)) | ||
} | ||
|
||
return strings.TrimSuffix(parts[6], ".vcf"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package carddav | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/maxatome/go-testdeep/td" | ||
) | ||
|
||
func TestDelete(t *testing.T) { | ||
testServerCalled := false | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
td.Cmp(t, r.Method, "DELETE") | ||
td.Cmp(t, r.Header["Authorization"], []string{"Basic YWxpY2U6cGFzc3dvcmQ="}) | ||
td.Cmp(t, r.URL.Path, "/dav/addressbooks/user/charlieegan3@fastmail.com/Default/test-id.vcf") | ||
testServerCalled = true | ||
})) | ||
|
||
cardDavClient := Client{ | ||
URL: testServer.URL + "/dav/addressbooks/user/charlieegan3@fastmail.com/Default/", | ||
User: "alice", | ||
Password: "password", | ||
} | ||
|
||
err := cardDavClient.Delete("test-id") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
td.Cmp(t, testServerCalled, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package carddav | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/maxatome/go-testdeep/td" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
td.Cmp(t, r.Method, "PROPFIND") | ||
td.Cmp(t, r.Header["Authorization"], []string{"Basic YWxpY2U6cGFzc3dvcmQ="}) | ||
td.Cmp(t, r.Header["Depth"], []string{"1"}) | ||
|
||
b, err := ioutil.ReadFile("propfind.xml") | ||
td.Cmp(t, err, nil) | ||
w.Write(b) | ||
})) | ||
|
||
expectedItems := []string{ | ||
"6316e64d-176c-42bc-9d59-30ed53c1a06b", | ||
"a5fadfc4-27fc-459c-847d-c8dabe3f789e", | ||
} | ||
|
||
cardDavClient := Client{ | ||
URL: testServer.URL + "/dav/addressbooks/user/charlieegan3@fastmail.com/Default", | ||
User: "alice", | ||
Password: "password", | ||
} | ||
|
||
gotItems, err := cardDavClient.List() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
td.Cmp(t, gotItems, expectedItems) | ||
} |
Oops, something went wrong.