77package client
88
99import (
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
3537type 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.
4346func 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.
112126func (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.
121145func (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.
0 commit comments