diff options
| author | Teddy Wing | 2017-06-04 00:26:03 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-06-04 00:26:03 +0200 | 
| commit | 34fb8b7b98076b65b05efc2bbaff013fbbbf5116 (patch) | |
| tree | eae64bb174ff4b708f2ba701168acac373079020 /timetask | |
| parent | f8821fa88888491bf49d8cd626f7a011665c6066 (diff) | |
| parent | 6a62cc9aef3684ef0bd72e6ab79157a699b01c72 (diff) | |
| download | timetasker-34fb8b7b98076b65b05efc2bbaff013fbbbf5116.tar.bz2 | |
Merge branch 'command-to-get-module-ids-of-project' into timetasker-daily
Diffstat (limited to 'timetask')
| -rw-r--r-- | timetask/http.go | 51 | ||||
| -rw-r--r-- | timetask/http_test.go | 2 | ||||
| -rw-r--r-- | timetask/module.go | 24 | ||||
| -rw-r--r-- | timetask/module_test.go | 50 | 
4 files changed, 126 insertions, 1 deletions
| diff --git a/timetask/http.go b/timetask/http.go index 8c41b4f..575ceae 100644 --- a/timetask/http.go +++ b/timetask/http.go @@ -1,6 +1,9 @@  package timetask  import ( +	"bytes" +	"fmt" +	"io/ioutil"  	"net/http"  	"net/http/cookiejar"  	"net/url" @@ -126,3 +129,51 @@ func buildSubmissionParams(time_entry TimeEntry) url.Values {  	return v  } + +func RequestModules( +	client http.Client, +	time_entry TimeEntry, +) (string, error) { +	params := url.Values{ +		"module":        {"projects"}, +		"action":        {"listmodulesxref"}, +		"f_ID":          {strconv.Itoa(time_entry.Project)}, +		"f_active":      {"t"}, +		"f_clientID":    {strconv.Itoa(time_entry.Client)}, +		"f_personID":    {strconv.Itoa(time_entry.PersonID)}, +		"f_milestoneID": {""}, +	} +	modules_url, err := url.Parse(baseURL) +	if err != nil { +		return "", err +	} + +	modules_url.RawQuery = params.Encode() + +	resp, err := client.Get(modules_url.String()) +	if err != nil { +		return "", err +	} + +	defer resp.Body.Close() +	body, err := ioutil.ReadAll(resp.Body) +	if err != nil { +		return "", err +	} +	response_body := string(body) + +	modules, err := ModuleParseXML(response_body) +	if err != nil { +		return "", err +	} + +	var module_buf bytes.Buffer +	module_buf.WriteString("ID\tModule\n") +	for _, module := range modules { +		module_buf.WriteString( +			fmt.Sprintf("%d\t%s\n", module.ID, module.Name), +		) +	} + +	return module_buf.String(), nil +} diff --git a/timetask/http_test.go b/timetask/http_test.go index 604c3c3..36f9e4a 100644 --- a/timetask/http_test.go +++ b/timetask/http_test.go @@ -19,7 +19,7 @@ func init() {  func TestLogin(t *testing.T) {  	t.Skip("No requests") -	response, err := Login(username, password) +	response, _, err := Login(username, password)  	if err != nil {  		t.Fatal(err)  	} diff --git a/timetask/module.go b/timetask/module.go new file mode 100644 index 0000000..4dde57a --- /dev/null +++ b/timetask/module.go @@ -0,0 +1,24 @@ +package timetask + +import ( +	"encoding/xml" +) + +type Module struct { +	ID   int    `xml:"moduleid"` +	Name string `xml:"modulename"` +} + +type moduleXML struct { +	Modules []Module `xml:"response>item"` +} + +func ModuleParseXML(xml_str string) ([]Module, error) { +	modules := moduleXML{} +	err := xml.Unmarshal([]byte(xml_str), &modules) +	if err != nil { +		return nil, err +	} + +	return modules.Modules, nil +} diff --git a/timetask/module_test.go b/timetask/module_test.go new file mode 100644 index 0000000..cee87c5 --- /dev/null +++ b/timetask/module_test.go @@ -0,0 +1,50 @@ +package timetask + +import "testing" + +const modules_xml = `<?xml version="1.0" encoding="UTF-8" ?> +<ajax-response> +	<response type="object" id="ModuleList"> +					<item> +						<moduleid><![CDATA[55555]]></moduleid> +						<modulename><![CDATA[R&D]]></modulename> +						</item> +					<item> +						<moduleid><![CDATA[77777]]></moduleid> +						<modulename><![CDATA[Sprint 1]]></modulename> +						</item> +					<item> +						<moduleid><![CDATA[222222]]></moduleid> +						<modulename><![CDATA[Sprint 2]]></modulename> +						</item> +			</response> +</ajax-response>` + +func TestModuleParseXML(t *testing.T) { +	modules, err := ModuleParseXML(modules_xml) +	if err != nil { +		t.Error(err) +	} + +	_ = []Module{ // wanted +		Module{ +			ID:   55555, +			Name: "R&D", +		}, +		Module{ +			ID:   77777, +			Name: "Sprint 1", +		}, +		Module{ +			ID:   222222, +			Name: "Sprint 2", +		}, +	} + +	// Need a way to compare slices +	// if modules != wanted { +	// 	t.Errorf("Module parsing failed. Wanted %+v got %+v", wanted, modules) +	// } + +	t.Logf("%+v\n", modules) +} | 
