1
0

fix: importing trello attachments

Since jan 2021, trello requires authentication to access attachments. This commit passes the required auth headers to make downloading card attachments work.

resolves https://github.com/go-vikunja/api/issues/6
This commit is contained in:
kolaente
2021-11-14 21:47:51 +01:00
parent 57e5d10eee
commit c3e0e6405a
3 changed files with 21 additions and 4 deletions

View File

@ -26,10 +26,24 @@ import (
// DownloadFile downloads a file and returns its contents
func DownloadFile(url string) (buf *bytes.Buffer, err error) {
return DownloadFileWithHeaders(url, nil)
}
// DownloadFileWithHeaders downloads a file and allows you to pass in headers
func DownloadFileWithHeaders(url string, headers http.Header) (buf *bytes.Buffer, err error) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
return nil, err
}
if headers != nil {
for key, h := range headers {
for _, hh := range h {
req.Header.Add(key, hh)
}
}
}
hc := http.Client{}
resp, err := hc.Do(req)
if err != nil {
@ -38,6 +52,7 @@ func DownloadFile(url string) (buf *bytes.Buffer, err error) {
defer resp.Body.Close()
buf = &bytes.Buffer{}
_, err = buf.ReadFrom(resp.Body)
return
}