35 lines
902 B
Go
35 lines
902 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"local/oauth2"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func Upload(config Config, transaction *Transaction) error {
|
|
params := url.Values{
|
|
"list": {config.TodoList},
|
|
"title": {fmt.Sprintf("%v: %s @ %s @ %s", transaction.Bank, transaction.Amount, transaction.Account, transaction.Date)},
|
|
"tag": {config.TodoTag},
|
|
}
|
|
req, err := http.NewRequest("POST", config.TodoAddr+"/ajax.php?newTask", strings.NewReader(params.Encode()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Cookie", oauth2.COOKIE+"="+config.TodoToken)
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
|
return fmt.Errorf("bad status from todo: %v: %s", resp.StatusCode, b)
|
|
}
|
|
return nil
|
|
}
|