forked from Comcast/rally-rest-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrallyclient.go
157 lines (128 loc) · 4.41 KB
/
rallyclient.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright 2014 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rallyresttoolkit
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
//RallyClient - struct
type RallyClient struct {
apikey string
apiurl string
client ClientDoer
}
//ClientDoer - interface
type ClientDoer interface {
Do(*http.Request) (*http.Response, error)
}
// New - creates a new RallyClient
func New(apikey string, apiurl string, client ClientDoer) *RallyClient {
return &RallyClient{
apikey: apikey,
apiurl: apiurl,
client: client,
}
}
//HTTPClient - returns the internal client object
func (s *RallyClient) HTTPClient() ClientDoer {
return s.client
}
// QueryRequest - function to search for an object.
func (s *RallyClient) QueryRequest(query map[string]string, queryType string, output interface{}) (err error) {
baseURL, _ := url.Parse(strings.Join([]string{s.apiurl, queryType}, "/"))
params := url.Values{}
params.Add("fetch", "true")
for idx, val := range query {
params.Add("query", fmt.Sprintf("( %s = %s )", idx, val))
}
baseURL.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", baseURL)
req, _ := http.NewRequest("GET", urlStr, nil)
req.Header.Add("ZSESSIONID", s.apikey)
if rallyResponse, err := s.HTTPClient().Do(req); err == nil {
content, _ := ioutil.ReadAll(rallyResponse.Body)
if err = json.Unmarshal(content, output); err != nil {
return err
}
}
return nil
}
// GetRequest - Function to perform GET requests when objectID is known.
func (s *RallyClient) GetRequest(objectID string, queryType string, output interface{}) (err error) {
baseURL, _ := url.Parse(strings.Join([]string{s.apiurl, queryType, objectID}, "/"))
params := url.Values{}
params.Add("fetch", "true")
baseURL.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", baseURL)
req, _ := http.NewRequest("GET", urlStr, nil)
req.Header.Add("ZSESSIONID", s.apikey)
if rallyResponse, err := s.HTTPClient().Do(req); err == nil {
content, _ := ioutil.ReadAll(rallyResponse.Body)
if err = json.Unmarshal(content, output); err != nil {
return err
}
}
return nil
}
func (s *RallyClient) CreateRequest(queryType string, input interface{}, output interface{}) (err error) {
baseURL, _ := url.Parse(strings.Join([]string{s.apiurl, queryType, "create"}, "/"))
urlStr := fmt.Sprintf("%v", baseURL)
inputByteArray, err := json.Marshal(input)
req, _ := http.NewRequest("POST", urlStr, bytes.NewReader(inputByteArray))
req.Header.Add("ZSESSIONID", s.apikey)
if rallyResponse, err := s.HTTPClient().Do(req); err == nil {
content, _ := ioutil.ReadAll(rallyResponse.Body)
if err = json.Unmarshal(content, output); err != nil {
return err
}
}
return nil
}
func (s *RallyClient) UpdateRequest(objectID string, queryType string, input interface{}, output interface{}) (err error) {
baseURL, _ := url.Parse(strings.Join([]string{s.apiurl, queryType, objectID}, "/"))
urlStr := fmt.Sprintf("%v", baseURL)
inputByteArray, err := json.Marshal(input)
req, _ := http.NewRequest("POST", urlStr, bytes.NewReader(inputByteArray))
req.Header.Add("ZSESSIONID", s.apikey)
if rallyResponse, err := s.HTTPClient().Do(req); err == nil {
content, _ := ioutil.ReadAll(rallyResponse.Body)
if err = json.Unmarshal(content, output); err != nil {
return err
}
}
return nil
}
func (s *RallyClient) DeleteRequest(objectID string, queryType string, output interface{}) (err error) {
baseURL, _ := url.Parse(strings.Join([]string{s.apiurl, queryType, objectID}, "/"))
params := url.Values{}
params.Add("fetch", "true")
baseURL.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", baseURL)
req, _ := http.NewRequest("DELETE", urlStr, nil)
req.Header.Add("ZSESSIONID", s.apikey)
if rallyResponse, err := s.HTTPClient().Do(req); err == nil {
content, _ := ioutil.ReadAll(rallyResponse.Body)
if err = json.Unmarshal(content, output); err != nil {
return err
}
}
return nil
}