aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.go26
-rw-r--r--timetask/http.go51
2 files changed, 37 insertions, 40 deletions
diff --git a/main.go b/main.go
index 1521821..2f37632 100644
--- a/main.go
+++ b/main.go
@@ -2,9 +2,7 @@ package main
import (
"fmt"
- "io/ioutil"
"os"
- "strings"
"time"
"github.com/teddywing/timetasker/timetask"
@@ -93,22 +91,12 @@ func main() {
password, err := passwordCmd(config.Auth.PasswordCmd)
kingpin.FatalIfError(err, "password command failed")
- resp, client, err := timetask.Login(
+ client, err := timetask.Login(
config.Auth.Username,
password,
)
kingpin.FatalIfError(err, "login request failed")
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if strings.Contains(
- string(body),
- "The username and password don't appear to be valid.",
- ) {
- kingpin.Errorf("TimeTask authentication failed")
- os.Exit(1)
- }
-
// List modules
if *list_modules {
modules, err := timetask.RequestModules(*client, time_entry)
@@ -118,16 +106,6 @@ func main() {
os.Exit(0)
}
- resp, err = timetask.SubmitTimeEntry(*client, time_entry)
+ err = timetask.SubmitTimeEntry(*client, time_entry)
kingpin.FatalIfError(err, "time entry submission request failed")
-
- defer resp.Body.Close()
- body, err = ioutil.ReadAll(resp.Body)
- if strings.Contains(
- string(body),
- "No time entries were created.",
- ) {
- kingpin.Errorf("time entry creation failed")
- os.Exit(1)
- }
}
diff --git a/timetask/http.go b/timetask/http.go
index 575ceae..f0a6548 100644
--- a/timetask/http.go
+++ b/timetask/http.go
@@ -15,18 +15,14 @@ import (
var baseURL string = "https://af83.timetask.com/index.php"
-func Login(username, password string) (
- resp *http.Response,
- client *http.Client,
- err error,
-) {
+func Login(username, password string) (client *http.Client, err error) {
cookies, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
- return nil, nil, err
+ return nil, err
}
client = &http.Client{Jar: cookies}
- resp, err = client.PostForm(
+ resp, err := client.PostForm(
baseURL,
url.Values{
"module": {"people"},
@@ -37,30 +33,53 @@ func Login(username, password string) (
},
)
if err != nil {
- return resp, client, err
+ return client, err
}
- return resp, client, err
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return client, err
+ }
+
+ if strings.Contains(
+ string(body),
+ "The username and password don't appear to be valid.",
+ ) {
+ return client, fmt.Errorf("TimeTask authentication failed")
+ }
+
+ return client, err
}
-func SubmitTimeEntry(
- client http.Client,
- time_entry TimeEntry,
-) (resp *http.Response, err error) {
+func SubmitTimeEntry(client http.Client, time_entry TimeEntry) error {
values := buildSubmissionParams(time_entry)
values.Set("module", "time")
values.Set("action", "submitmultipletime")
- resp, err = client.PostForm(
+ resp, err := client.PostForm(
baseURL,
values,
)
if err != nil {
- return resp, err
+ return err
+ }
+
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+
+ if strings.Contains(
+ string(body),
+ "No time entries were created.",
+ ) {
+ return fmt.Errorf("time entry creation failed")
}
- return resp, nil
+ return nil
}
func buildSubmissionParams(time_entry TimeEntry) url.Values {