@@ -13,10 +13,10 @@ import (
1313 "strconv"
1414 "strings"
1515
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"
2020)
2121
2222// Response is a struct used to cover basic functionality to work with
@@ -31,7 +31,17 @@ type Response struct {
3131 records []record.Record
3232}
3333
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.
3545func NewResponse (raw string , cmd map [string ]string , phs ... map [string ]string ) * Response {
3646 ph := map [string ]string {}
3747 if len (phs ) > 0 {
@@ -88,20 +98,53 @@ func NewResponse(raw string, cmd map[string]string, phs ...map[string]string) *R
8898 return r
8999}
90100
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.
92114func (r * Response ) GetCode () int {
93115 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
97130 }
98- return 421
131+ return c
99132}
100133
101134// GetDescription method to return the API response description
102135func (r * Response ) GetDescription () string {
103136 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
105148}
106149
107150// GetPlain method to return raw API response
@@ -158,12 +201,23 @@ func (r *Response) IsTmpError() bool {
158201
159202// IsPending method to check if current operation is returned as pending
160203func (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
165217 }
218+ return strings .EqualFold (statusData , "REQUESTED" )
166219 }
220+
167221 return false
168222}
169223
@@ -199,7 +253,7 @@ func (r *Response) GetColumnIndex(key string, index int) (string, error) {
199253 return d , nil
200254 }
201255 }
202- return "" , errors .New ("Column Data Index does not exist" )
256+ return "" , errors .New ("column Data Index does not exist" )
203257}
204258
205259// GetColumnKeys method to get the list of column names
@@ -242,7 +296,7 @@ func (r *Response) GetCurrentPageNumber() (int, error) {
242296 if ferr == nil && limit > 0 {
243297 return int (math .Floor (float64 (first )/ float64 (limit ))) + 1 , nil
244298 }
245- return 0 , errors .New ("Could not find current page number" )
299+ return 0 , errors .New ("could not find current page number" )
246300}
247301
248302// GetCurrentRecord method to get record of current record index
@@ -263,14 +317,14 @@ func (r *Response) GetFirstRecordIndex() (int, error) {
263317 if err2 == nil {
264318 return idx , nil
265319 }
266- return 0 , errors .New ("Could not find first record index" )
320+ return 0 , errors .New ("could not find first record index" )
267321 }
268322 }
269323 tlen := len (r .records )
270324 if tlen > 1 {
271325 return 0 , nil
272326 }
273- return 0 , errors .New ("Could not find first record index" )
327+ return 0 , errors .New ("could not find first record index" )
274328}
275329
276330// GetLastRecordIndex method to get last record index of the current list query
@@ -283,14 +337,14 @@ func (r *Response) GetLastRecordIndex() (int, error) {
283337 if err2 == nil {
284338 return idx , nil
285339 }
286- return 0 , errors .New ("Could not find last record index" )
340+ return 0 , errors .New ("could not find last record index" )
287341 }
288342 }
289343 tlen := r .GetRecordsCount ()
290344 if tlen > 0 {
291345 return (tlen - 1 ), nil
292346 }
293- return 0 , errors .New ("Could not find last record index" )
347+ return 0 , errors .New ("could not find last record index" )
294348}
295349
296350// GetListHash method to get Response as List Hash including useful meta data for tables
@@ -322,7 +376,7 @@ func (r *Response) GetNextRecord() *record.Record {
322376func (r * Response ) GetNextPageNumber () (int , error ) {
323377 cp , err := r .GetCurrentPageNumber ()
324378 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" )
326380 }
327381 page := cp + 1
328382 pages := r .GetNumberOfPages ()
@@ -385,7 +439,7 @@ func (r *Response) GetPreviousPageNumber() (int, error) {
385439 }
386440 pp := cp - 1
387441 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" )
389443 }
390444 return pp , nil
391445}
@@ -472,14 +526,15 @@ func (r *Response) RewindRecordList() *Response {
472526 return r
473527}
474528
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)
476530func (r * Response ) hasColumn (key string ) (int , bool ) {
531+ lowerKey := strings .ToLower (key )
477532 for i , k := range r .columnkeys {
478- if k == key {
533+ if strings . ToLower ( k ) == lowerKey {
479534 return i , true
480535 }
481536 }
482- return 0 , false
537+ return - 1 , false
483538}
484539
485540// hasCurrentRecord method to check if the record on current record index exists
0 commit comments