Skip to content

Commit a46d0cf

Browse files
added debugMode; possibility to enable debug output
1 parent 689b56e commit a46d0cf

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

client/client.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package client
88

99
import (
10+
"encoding/json"
11+
"fmt"
1012
"io/ioutil"
1113
"net/http"
1214
"net/url"
@@ -33,6 +35,7 @@ import (
3335
//
3436
// Possible commands can be found at https://github.com/hexonet/hexonet-api-documentation/tree/master/API
3537
type Client struct {
38+
debugMode bool
3639
socketTimeout int
3740
apiurl string
3841
socketcfg.Socketcfg
@@ -42,6 +45,7 @@ type Client struct {
4245
// The client is by default set to communicate with the LIVE system. Use method UseOTESystem to switch to the OT&E system instance.
4346
func NewClient() *Client {
4447
cl := &Client{
48+
debugMode: false,
4549
socketTimeout: 300000,
4650
apiurl: "https://coreapi.1api.net/api/call.cgi",
4751
Socketcfg: socketcfg.Socketcfg{},
@@ -108,6 +112,16 @@ func (c *Client) UseOTESystem() {
108112
c.Socketcfg.SetEntity("1234")
109113
}
110114

115+
// EnableDebugMode method to enable debugMode for debug output
116+
func (c *Client) EnableDebugMode() {
117+
c.debugMode = true
118+
}
119+
120+
// DisableDebugMode method to disable debugMode for debug output
121+
func (c *Client) DisableDebugMode() {
122+
c.debugMode = false
123+
}
124+
111125
// Request method requests the given command to the api server and returns the response as ListResponse.
112126
func (c *Client) Request(cmd map[string]string) *listresponse.ListResponse {
113127
if c.Socketcfg == (socketcfg.Socketcfg{}) {
@@ -116,6 +130,16 @@ func (c *Client) Request(cmd map[string]string) *listresponse.ListResponse {
116130
return c.dorequest(cmd, &c.Socketcfg)
117131
}
118132

133+
// debugRequest method used to trigger debug output in case debugMode is activated
134+
func (c *Client) debugRequest(cmd map[string]string, data string, r *listresponse.ListResponse) {
135+
if c.debugMode {
136+
j, _ := json.Marshal(cmd)
137+
fmt.Printf("%s\n", j)
138+
fmt.Println("POST: " + data)
139+
fmt.Println(strconv.Itoa(r.Code()) + " " + r.Description() + "\n")
140+
}
141+
}
142+
119143
// RequestAll method requests ALL entries matching the request criteria by the given command from api server.
120144
// So useful for client-side lists. Finally it returns the response as ListResponse.
121145
func (c *Client) RequestAll(cmd map[string]string) *listresponse.ListResponse {
@@ -141,29 +165,39 @@ func (c *Client) dorequest(cmd map[string]string, cfg *socketcfg.Socketcfg) *lis
141165
if err != nil {
142166
tpl := hashresponse.NewTemplates().Get("commonerror")
143167
tpl = strings.Replace(tpl, "####ERRMSG####", err.Error(), 1)
144-
return listresponse.NewListResponse(tpl)
168+
r := listresponse.NewListResponse(tpl)
169+
c.debugRequest(cmd, data, r)
170+
return r
145171
}
146172
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
147173
req.Header.Add("Expect", "")
148174
resp, err2 := client.Do(req)
149175
if err2 != nil {
150176
tpl := hashresponse.NewTemplates().Get("commonerror")
151177
tpl = strings.Replace(tpl, "####ERRMSG####", err2.Error(), 1)
152-
return listresponse.NewListResponse(tpl)
178+
r := listresponse.NewListResponse(tpl)
179+
c.debugRequest(cmd, data, r)
180+
return r
153181
}
154182
defer resp.Body.Close()
155183
if resp.StatusCode == http.StatusOK {
156184
response, err := ioutil.ReadAll(resp.Body)
157185
if err != nil {
158186
tpl := hashresponse.NewTemplates().Get("commonerror")
159187
tpl = strings.Replace(tpl, "####ERRMSG####", err.Error(), 1)
160-
return listresponse.NewListResponse(tpl)
188+
r := listresponse.NewListResponse(tpl)
189+
c.debugRequest(cmd, data, r)
190+
return r
161191
}
162-
return listresponse.NewListResponse(string(response))
192+
r := listresponse.NewListResponse(string(response))
193+
c.debugRequest(cmd, data, r)
194+
return r
163195
}
164196
tpl := hashresponse.NewTemplates().Get("commonerror")
165197
tpl = strings.Replace(tpl, "####ERRMSG####", string(resp.StatusCode)+resp.Status, 1)
166-
return listresponse.NewListResponse(tpl)
198+
r := listresponse.NewListResponse(tpl)
199+
c.debugRequest(cmd, data, r)
200+
return r
167201
}
168202

169203
// Login method to use as entry point for session based communication.

scripts/test-go.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ echo
66
echo "==> Running automated tests <=="
77
cd test
88
go test
9-
cd ..
10-
9+
cd .. || exit
1110
exit

test/client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func TestReadme1(t *testing.T) {
1212
cl := client.NewClient()
1313
cl.SetCredentials("test.user", "test.passw0rd", "")
1414
cl.UseOTESystem()
15+
cl.EnableDebugMode()
1516
r := cl.Login()
1617
if r.IsSuccess() {
1718
cmd := map[string]string{

test/hashresponse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ func TestGetColumnKeys(t *testing.T) {
251251
if len(colKeys) != 2 {
252252
t.Error("TestGetColumnKeys: amount of columns differs")
253253
}
254-
if colKeys[0] != "CREATEDDATE" {
254+
if colKeys[0] != "CREATEDDATE" && colKeys[0] != "FINALIZATIONDATE" {
255255
t.Error("TestGetColumnKeys: column name for index 0 differs")
256256
}
257-
if colKeys[1] != "FINALIZATIONDATE" {
257+
if colKeys[1] != "FINALIZATIONDATE" && colKeys[1] != "CREATEDDATE" {
258258
t.Error("TestGetColumnKeys: column name for index 1 differs")
259259
}
260260
}

0 commit comments

Comments
 (0)