aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-06-03 14:16:29 +0200
committerTeddy Wing2017-06-03 14:16:29 +0200
commit288da883a3a4a91fec67029b7695ec03099cfc46 (patch)
tree030e74c98970b9772764453d89b5b124acf51681
parent84b921283c4106de55ca7908a58e8f1621b3841f (diff)
downloadtimetasker-288da883a3a4a91fec67029b7695ec03099cfc46.tar.bz2
Initial stab at submitting time entries for real
main.go: * Login as the configured user (haven't yet handled making `PasswordCmd` an actual shell command) * Create a test time entry * Submit that time entry using `SubmitTimeEntry()` http.go: * Create a `baseURL` global that stores the base TimeTask URL to make requests to * Return an `http.Client` from `Login()` that can then be passed to `SubmitTimeEntry()` to reuse the login session. Needed to return a pointer to allow us to return `nil` from the first error handler in the function. Don't like that at all, but we're just trying to get it to work at this point. * Actually make an HTTP POST request in `SubmitTimeEntry()` using the given HTTP Client and existing time entry submission params * Take an `http.Client` argument in `SubmitTimeEntry()` to allow us to use a logged-in session to POST. Otherwise we'd be locked out. * Change `v` variable to `values` in `SubmitTimeEntry()` for better readability
-rw-r--r--main.go23
-rw-r--r--timetask/http.go35
2 files changed, 48 insertions, 10 deletions
diff --git a/main.go b/main.go
index aea2112..1cc56c7 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
// "io/ioutil"
"log"
// "os"
+ "time"
"github.com/teddywing/timetasker/timetask"
@@ -25,6 +26,28 @@ var config Config
func main() {
loadConfig()
+ resp, client, err := timetask.Login(
+ config.Auth.Username,
+ config.Auth.PasswordCmd,
+ )
+ if err != nil {
+ log.Fatalln(err)
+ }
+ log.Printf("%+v\n", resp)
+
+ time_entry := timetask.NewTimeEntry(
+ config.Profile,
+ config.Projects["example"],
+ time.Now(),
+ 7,
+ "timetasker test",
+ )
+ resp, err = timetask.SubmitTimeEntry(*client, time_entry)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ log.Printf("%+v\n", resp)
+
// if len(os.Args) == 1 {
// fmt.Println("Not enough arguments")
// os.Exit(1)
diff --git a/timetask/http.go b/timetask/http.go
index 4f52565..cb91257 100644
--- a/timetask/http.go
+++ b/timetask/http.go
@@ -12,15 +12,21 @@ import (
"golang.org/x/net/publicsuffix"
)
-func Login(username, password string) (resp *http.Response, err error) {
+var baseURL string = "https://af83.timetask.com/index.php"
+
+func Login(username, password string) (
+ resp *http.Response,
+ client *http.Client,
+ err error,
+) {
cookies, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
- return nil, err
+ return nil, nil, err
}
- client := http.Client{Jar: cookies}
+ client = &http.Client{Jar: cookies}
resp, err = client.PostForm(
- "https://af83.timetask.com/index.php",
+ baseURL,
url.Values{
"module": {"people"},
"action": {"loginsubmit"},
@@ -30,21 +36,30 @@ func Login(username, password string) (resp *http.Response, err error) {
},
)
if err != nil {
- return resp, err
+ return resp, client, err
}
- return resp, err
+ return resp, client, err
}
func SubmitTimeEntry(
+ client http.Client,
time_entry TimeEntry,
) (resp *http.Response, err error) {
- v := buildSubmissionParams(time_entry)
+ values := buildSubmissionParams(time_entry)
- v.Set("module", "time")
- v.Set("action", "submitmultipletime")
+ values.Set("module", "time")
+ values.Set("action", "submitmultipletime")
+
+ resp, err = client.PostForm(
+ baseURL,
+ values,
+ )
+ if err != nil {
+ return resp, err
+ }
- return nil, nil
+ return resp, nil
}