Skip to content

Commit 6bdf2b8

Browse files
asdf
1 parent 40a4443 commit 6bdf2b8

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

apiclient/apiclient.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type APIClient struct {
103103
ua string
104104
logger LG.ILogger
105105
subUser string
106+
roleSeparator string
106107
}
107108

108109
// RequestOptions represents the options for an API request.
@@ -127,6 +128,7 @@ func NewAPIClient() *APIClient {
127128
curlopts: map[string]string{},
128129
ua: "",
129130
logger: nil,
131+
roleSeparator: ":",
130132
}
131133
cl.UseLIVESystem()
132134
cl.SetDefaultLogger()
@@ -297,7 +299,7 @@ func (cl *APIClient) GetVersion() string {
297299
func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
298300
sessionobj["socketcfg"] = map[string]string{
299301
"session": cl.socketConfig.GetSession(),
300-
"login": cl.socketConfig.GetLogin(),
302+
"login": cl.socketConfig.GetLogin(),
301303
}
302304
return cl
303305
}
@@ -306,7 +308,7 @@ func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
306308
// to rebuild and reuse connection settings
307309
func (cl *APIClient) ReuseSession(sessionobj map[string]interface{}) *APIClient {
308310
cfg := sessionobj["socketcfg"].(map[string]string)
309-
cl.SetLogin(cfg["login"])
311+
cl.SetCredentials(cfg["login"])
310312
cl.SetSession(cfg["session"])
311313
return cl
312314
}
@@ -323,31 +325,39 @@ func (cl *APIClient) SetSession(value string) *APIClient {
323325
return cl
324326
}
325327

326-
// Set Login method to set an API user id to be used for API communication
327-
func (cl *APIClient) SetLogin(value string) *APIClient {
328-
cl.socketConfig.SetLogin(value)
329-
return cl
330-
}
331-
332328
// SetPersistent method sets the API connection to use a persistent session
333329
func (cl *APIClient) SetPersistent() *APIClient {
334330
cl.socketConfig.SetPersistent()
335331
return cl
336332
}
337333

338334
// SetCredentials method to set Credentials to be used for API communication
339-
func (cl *APIClient) SetCredentials(uid string, pw string) *APIClient {
340-
cl.socketConfig.SetLogin(uid)
341-
cl.socketConfig.SetPassword(pw)
335+
func (cl *APIClient) SetCredentials(params ...string) *APIClient {
336+
fmt.Printf("params: %v\n", params)
337+
if len(params) > 0 {
338+
cl.socketConfig.SetLogin(params[0])
339+
}
340+
if len(params) > 1 {
341+
cl.socketConfig.SetPassword(params[1])
342+
}
342343
return cl
343344
}
344345

345346
// SetRoleCredentials method to set Role User Credentials to be used for API communication
346-
func (cl *APIClient) SetRoleCredentials(uid string, role string, pw string) *APIClient {
347-
if len(role) > 0 {
348-
return cl.SetCredentials(uid+":"+role, pw)
347+
func (cl *APIClient) SetRoleCredentials(params ...string) *APIClient {
348+
if len(params) > 0 {
349+
uid := params[0]
350+
if len(params) > 1 && len(params[1]) > 0 {
351+
role := params[1]
352+
uid = uid + cl.roleSeparator + role
353+
}
354+
if len(params) > 2 {
355+
pw := params[2]
356+
return cl.SetCredentials(uid, pw)
357+
}
358+
return cl.SetCredentials(uid)
349359
}
350-
return cl.SetCredentials(uid, pw)
360+
return cl
351361
}
352362

353363
// Login method to perform API login to start session-based communication

apiclient/apiclient_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestSetURL(t *testing.T) {
183183
}
184184

185185
func TestSetSession1(t *testing.T) {
186-
cl.SetSession("12345678").SetLogin("myaccountid")
186+
cl.SetSession("12345678").SetCredentials("myaccountid")
187187
tmp := cl.GetPOSTData(map[string]string{
188188
"COMMAND": "StatusAccount",
189189
})
@@ -313,6 +313,27 @@ func TestLogin3(t *testing.T) {
313313
}
314314
}
315315

316+
/**
317+
* Make sure session is Cleaned up if password is provided after session is saved
318+
*/
319+
func TestLogin4(t *testing.T) {
320+
// Initialize the first APIClient instance
321+
cl.SetSession("12345678").SetCredentials("myaccountid", "password").EnableDebugMode()
322+
323+
// Prepare the command map
324+
cmd := map[string]string{
325+
"COMMAND": "StatusAccount",
326+
}
327+
328+
// Get the POST data from the second APIClient instance
329+
tmp := cl.GetPOSTData(cmd)
330+
331+
// Validate the result
332+
if strings.Compare(tmp, "s_login=myaccountid&s_pw=password&s_command=COMMAND%3DStatusAccount") != 0 {
333+
t.Error("TestLogin4: Expected post data string not matching." + tmp)
334+
}
335+
}
336+
316337
// validate against mocked API response [login failed; http timeout] // need mocking
317338
// validate against mocked API response [login succeeded; no session returned] // need mocking
318339

@@ -333,7 +354,7 @@ func TestLogout1(t *testing.T) {
333354
func TestSaveAndReuseSession(t *testing.T) {
334355
// Initialize the first APIClient instance
335356
sessionobj := make(map[string]interface{})
336-
cl.SetSession("12345678").SetLogin("myaccountid").SaveSession(sessionobj).EnableDebugMode()
357+
cl.SetSession("12345678").SetCredentials("myaccountid").SaveSession(sessionobj).EnableDebugMode()
337358

338359
// Initialize the second APIClient instance
339360
cl2 := NewAPIClient()

0 commit comments

Comments
 (0)