diff options
author | Teddy Wing | 2017-06-03 15:13:28 +0200 |
---|---|---|
committer | Teddy Wing | 2017-06-03 15:13:28 +0200 |
commit | 61c447fda6fe833bebb953600167975685e89102 (patch) | |
tree | ce96e85b9307df20903d3257a7a23df9cc1bbde3 /timetask/http.go | |
parent | c0576d6d96656f4a83e8bdff7cb0a09f455a0b10 (diff) | |
parent | 24a3a2d9dbe6b2cd48d126f3f36a6d79497b4cc4 (diff) | |
download | timetasker-61c447fda6fe833bebb953600167975685e89102.tar.bz2 |
Merge branch 'submit-single-time-entry' into timetasker-daily
Diffstat (limited to 'timetask/http.go')
-rw-r--r-- | timetask/http.go | 206 |
1 files changed, 97 insertions, 109 deletions
diff --git a/timetask/http.go b/timetask/http.go index 94a1597..736fcfb 100644 --- a/timetask/http.go +++ b/timetask/http.go @@ -6,21 +6,27 @@ import ( "net/http" "net/http/cookiejar" "net/url" - // "strconv" + "strconv" // "strings" "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,112 +36,94 @@ func Login(username, password string) (resp *http.Response, err error) { }, ) if err != nil { + return resp, client, err + } + + return resp, client, err +} + +func SubmitTimeEntry( + client http.Client, + time_entry TimeEntry, +) (resp *http.Response, err error) { + values := buildSubmissionParams(time_entry) + + values.Set("module", "time") + values.Set("action", "submitmultipletime") + + resp, err = client.PostForm( + baseURL, + values, + ) + if err != nil { return resp, err } - return resp, err + return resp, nil } -// func SubmitTimeEntries(fields Fields, time_entries []TimeEntry) (resp *http.Response, err error) { -// v := buildSubmissionParams(fields, time_entries) -// -// v.Set("module", "time") -// v.Set("action", "submitmultipletime") -// -// return nil, nil -// } -// -// func buildSubmissionParams(fields Fields, time_entries []TimeEntry) url.Values { -// v := url.Values{} -// entry_indexes := []string{} -// -// for i, entry := range time_entries { -// entry_indexes = append(entry_indexes, strconv.Itoa(i)) -// -// client, err := fields.ClientByName(entry.Client) -// if err != nil { -// log.Panic(err) -// } -// -// project, err := client.ProjectByName(entry.Project) -// if err != nil { -// log.Panic(err) -// } -// -// module, err := project.ModuleByName(entry.Module) -// if err != nil { -// log.Panic(err) -// } -// -// task, err := project.TaskByName(entry.Task) -// if err != nil { -// log.Panic(err) -// } -// -// work_type, err := project.WorkTypeByName(entry.WorkType) -// if err != nil { -// log.Panic(err) -// } -// -// var billable string -// if entry.Billable { -// billable = "t" -// } else { -// billable = "f" -// } -// -// v.Set( -// fmt.Sprintf("f_personID%d", i), -// strconv.Itoa(fields.PersonID), -// ) -// -// v.Set( -// fmt.Sprintf("f_clientID%d", i), -// strconv.Itoa(client.ID), -// ) -// -// v.Set( -// fmt.Sprintf("f_projectID%d", i), -// strconv.Itoa(project.ID), -// ) -// -// v.Set( -// fmt.Sprintf("f_moduleID%d", i), -// strconv.Itoa(module.ID), -// ) -// -// v.Set( -// fmt.Sprintf("f_taskID%d", i), -// strconv.Itoa(task.ID), -// ) -// -// v.Set( -// fmt.Sprintf("f_worktypeID%d", i), -// strconv.Itoa(work_type.ID), -// ) -// -// v.Set( -// fmt.Sprintf("f_date%d", i), -// entry.Date.Format("02/01/06"), // day/month/year -// ) -// -// v.Set( -// fmt.Sprintf("f_time%d", i), -// strconv.Itoa(entry.Time), -// ) -// -// v.Set( -// fmt.Sprintf("f_billable%d", i), -// billable, -// ) -// -// v.Set( -// fmt.Sprintf("f_description%d", i), -// entry.Description, -// ) -// } -// -// v.Set("f_entryIndexes", strings.Join(entry_indexes, ",")) -// -// return v -// } + +func buildSubmissionParams(time_entry TimeEntry) url.Values { + v := url.Values{} + + v.Set( + "f_personID0", + strconv.Itoa(time_entry.PersonID), + ) + + v.Set( + "f_clientID0", + strconv.Itoa(time_entry.Client), + ) + + v.Set( + "f_projectID0", + strconv.Itoa(time_entry.Project), + ) + + v.Set( + "f_moduleID0", + strconv.Itoa(time_entry.Module), + ) + + v.Set( + "f_taskID0", + strconv.Itoa(time_entry.Task), + ) + + v.Set( + "f_worktypeID0", + strconv.Itoa(time_entry.WorkType), + ) + + v.Set( + "f_date0", + time_entry.Date.Format("02/01/06"), // day/month/year + ) + + v.Set( + "f_time0", + strconv.Itoa(time_entry.Time), + ) + + var billable string + if time_entry.Billable { + billable = "t" + } else { + billable = "f" + } + + v.Set( + "f_billable0", + billable, + ) + + v.Set( + "f_description0", + time_entry.Description, + ) + + v.Set("f_entryIndexes", "0") + + return v +} |