aboutsummaryrefslogtreecommitdiffstats
path: root/timetask/http.go
blob: 8b902309ef8d6b6e68e914bf98f49f626de1fab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package timetask

import (
	// "fmt"
	// "log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strconv"
	// "strings"

	"golang.org/x/net/publicsuffix"
)

func Login(username, password string) (resp *http.Response, err error) {
	cookies, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
	if err != nil {
		return nil, err
	}

	client := http.Client{Jar: cookies}
	resp, err = client.PostForm(
		"https://af83.timetask.com/index.php",
		url.Values{
			"module":     {"people"},
			"action":     {"loginsubmit"},
			"f_url":      {"/"},
			"f_username": {username},
			"f_password": {password},
		},
	)
	if err != nil {
		return resp, err
	}

	return resp, err
}

func SubmitTimeEntry(
	project Project,
	time_entry TimeEntry,
) (resp *http.Response, err error) {
}

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(profile Profile, time_entry TimeEntry) url.Values {
	v := url.Values{}

	v.Set(
		"f_personID0",
		strconv.Itoa(profile.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,
	)

	return v
}