@@ -13,10 +13,10 @@ import (
13
13
"strconv"
14
14
"strings"
15
15
16
- "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4 /column"
17
- "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4 /record"
18
- rp "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4 /responseparser"
19
- rt "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4 /responsetranslator"
16
+ "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5 /column"
17
+ "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5 /record"
18
+ rp "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5 /responseparser"
19
+ rt "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5 /responsetranslator"
20
20
)
21
21
22
22
// Response is a struct used to cover basic functionality to work with
@@ -31,7 +31,17 @@ type Response struct {
31
31
records []record.Record
32
32
}
33
33
34
- // NewResponse represents the constructor for struct Response.
34
+ const defaultCode = 421
35
+
36
+ // NewResponse creates a new Response object.
37
+ // It takes a raw string, a command map, and optional placeholder maps as parameters.
38
+ // The function replaces the "PASSWORD" value in the command map with "***" if it exists.
39
+ // It then translates the raw string using the command and placeholder maps.
40
+ // The function initializes a new Response object with the translated raw string, a hash value,
41
+ // the command map, empty column keys and columns, a record index of 0, and an empty records slice.
42
+ // If the hash value contains a "PROPERTY" key, the function adds columns and records to the Response object
43
+ // based on the values in the "PROPERTY" map.
44
+ // The function returns the newly created Response object.
35
45
func NewResponse (raw string , cmd map [string ]string , phs ... map [string ]string ) * Response {
36
46
ph := map [string ]string {}
37
47
if len (phs ) > 0 {
@@ -88,20 +98,53 @@ func NewResponse(raw string, cmd map[string]string, phs ...map[string]string) *R
88
98
return r
89
99
}
90
100
91
- // GetCode method to return the API response code
101
+ // GetCode returns the code associated with the response.
102
+ // If the response does not have a code or if the code is not a valid integer,
103
+ // it returns the default code.
104
+ //
105
+ // The code is retrieved from the response's hash, which is a map[string]interface{}.
106
+ // The code value is expected to be stored under the key "CODE" as a string.
107
+ // If the code value is not found or is not a string, the default code is returned.
108
+ //
109
+ // If an error occurs while converting the code string to an integer,
110
+ // the default code is returned and the error is logged or handled appropriately.
111
+ //
112
+ // Returns:
113
+ // - int: The code associated with the response.
92
114
func (r * Response ) GetCode () int {
93
115
h := r .GetHash ()
94
- c , err := strconv .Atoi (h ["CODE" ].(string ))
95
- if err == nil {
96
- return c
116
+ if h == nil {
117
+ return defaultCode
118
+ }
119
+
120
+ codeStr , ok := h ["CODE" ].(string )
121
+ if ! ok {
122
+ // Log or handle the error appropriately
123
+ return defaultCode
124
+ }
125
+
126
+ c , err := strconv .Atoi (codeStr )
127
+ if err != nil {
128
+ // Log or handle the error appropriately
129
+ return defaultCode
97
130
}
98
- return 421
131
+ return c
99
132
}
100
133
101
134
// GetDescription method to return the API response description
102
135
func (r * Response ) GetDescription () string {
103
136
h := r .GetHash ()
104
- return h ["DESCRIPTION" ].(string )
137
+ if h == nil {
138
+ return ""
139
+ }
140
+
141
+ // Assuming there is a "DESCRIPTION" key in the hash
142
+ desc , ok := h ["DESCRIPTION" ].(string )
143
+ if ! ok {
144
+ // Log or handle the error appropriately
145
+ return ""
146
+ }
147
+ return desc
105
148
}
106
149
107
150
// GetPlain method to return raw API response
@@ -158,12 +201,23 @@ func (r *Response) IsTmpError() bool {
158
201
159
202
// IsPending method to check if current operation is returned as pending
160
203
func (r * Response ) IsPending () bool {
161
- h := r .GetHash ()
162
- if val , ok := h ["PENDING" ]; ok {
163
- if val .(string ) == "1" {
164
- return true
204
+ cmd := r .GetCommand ()
205
+
206
+ // Check if the COMMAND is AddDomain (case-insensitive)
207
+ if command , ok := cmd ["COMMAND" ]; ! ok || ! strings .EqualFold (command , "AddDomain" ) {
208
+ return false
209
+ }
210
+
211
+ // Retrieve the STATUS column and check if its data equals REQUESTED (case-insensitive)
212
+ status := r .GetColumn ("STATUS" )
213
+ if status != nil {
214
+ statusData , err := status .GetDataByIndex (0 )
215
+ if err != nil {
216
+ return false
165
217
}
218
+ return strings .EqualFold (statusData , "REQUESTED" )
166
219
}
220
+
167
221
return false
168
222
}
169
223
@@ -199,7 +253,7 @@ func (r *Response) GetColumnIndex(key string, index int) (string, error) {
199
253
return d , nil
200
254
}
201
255
}
202
- return "" , errors .New ("Column Data Index does not exist" )
256
+ return "" , errors .New ("column Data Index does not exist" )
203
257
}
204
258
205
259
// GetColumnKeys method to get the list of column names
@@ -242,7 +296,7 @@ func (r *Response) GetCurrentPageNumber() (int, error) {
242
296
if ferr == nil && limit > 0 {
243
297
return int (math .Floor (float64 (first )/ float64 (limit ))) + 1 , nil
244
298
}
245
- return 0 , errors .New ("Could not find current page number" )
299
+ return 0 , errors .New ("could not find current page number" )
246
300
}
247
301
248
302
// GetCurrentRecord method to get record of current record index
@@ -263,14 +317,14 @@ func (r *Response) GetFirstRecordIndex() (int, error) {
263
317
if err2 == nil {
264
318
return idx , nil
265
319
}
266
- return 0 , errors .New ("Could not find first record index" )
320
+ return 0 , errors .New ("could not find first record index" )
267
321
}
268
322
}
269
323
tlen := len (r .records )
270
324
if tlen > 1 {
271
325
return 0 , nil
272
326
}
273
- return 0 , errors .New ("Could not find first record index" )
327
+ return 0 , errors .New ("could not find first record index" )
274
328
}
275
329
276
330
// GetLastRecordIndex method to get last record index of the current list query
@@ -283,14 +337,14 @@ func (r *Response) GetLastRecordIndex() (int, error) {
283
337
if err2 == nil {
284
338
return idx , nil
285
339
}
286
- return 0 , errors .New ("Could not find last record index" )
340
+ return 0 , errors .New ("could not find last record index" )
287
341
}
288
342
}
289
343
tlen := r .GetRecordsCount ()
290
344
if tlen > 0 {
291
345
return (tlen - 1 ), nil
292
346
}
293
- return 0 , errors .New ("Could not find last record index" )
347
+ return 0 , errors .New ("could not find last record index" )
294
348
}
295
349
296
350
// GetListHash method to get Response as List Hash including useful meta data for tables
@@ -322,7 +376,7 @@ func (r *Response) GetNextRecord() *record.Record {
322
376
func (r * Response ) GetNextPageNumber () (int , error ) {
323
377
cp , err := r .GetCurrentPageNumber ()
324
378
if err != nil {
325
- return 0 , errors .New ("Could not find next page number" )
379
+ return 0 , errors .New ("could not find next page number" )
326
380
}
327
381
page := cp + 1
328
382
pages := r .GetNumberOfPages ()
@@ -385,7 +439,7 @@ func (r *Response) GetPreviousPageNumber() (int, error) {
385
439
}
386
440
pp := cp - 1
387
441
if pp < 1 {
388
- return 0 , errors .New ("Could not find previous page number" )
442
+ return 0 , errors .New ("could not find previous page number" )
389
443
}
390
444
return pp , nil
391
445
}
@@ -472,14 +526,15 @@ func (r *Response) RewindRecordList() *Response {
472
526
return r
473
527
}
474
528
475
- // hasColumn method to check if the given column exists in column list
529
+ // hasColumn method to check if the given column exists in column list (case-insensitive)
476
530
func (r * Response ) hasColumn (key string ) (int , bool ) {
531
+ lowerKey := strings .ToLower (key )
477
532
for i , k := range r .columnkeys {
478
- if k == key {
533
+ if strings . ToLower ( k ) == lowerKey {
479
534
return i , true
480
535
}
481
536
}
482
- return 0 , false
537
+ return - 1 , false
483
538
}
484
539
485
540
// hasCurrentRecord method to check if the record on current record index exists
0 commit comments