Skip to content

Commit ae820b8

Browse files
feat(centralnic-reseller-go-sdk): introducing CNR Go-SDK API Connector
BREAKING CHANGE: We have deprecated the Hexonet Go SDK and introduced the CentralNic Reseller Go SDK.
1 parent f6b96a0 commit ae820b8

File tree

15 files changed

+352
-305
lines changed

15 files changed

+352
-305
lines changed

apiclient/apiclient.go

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ import (
2020
"strings"
2121
"time"
2222

23-
IDN "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/idntranslator"
24-
LG "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/logger"
25-
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/response"
26-
RTM "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/responsetemplatemanager"
27-
SC "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/socketconfig"
23+
IDN "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/idntranslator"
24+
LG "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/logger"
25+
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/response"
26+
RTM "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/responsetemplatemanager"
27+
SC "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/socketconfig"
2828
)
2929

30-
// ISPAPI_CONNECTION_URL_PROXY represents the url used for the high performance connection setup
31-
const ISPAPI_CONNECTION_URL_PROXY = "http://127.0.0.1/api/call.cgi" //nolint
30+
// CNR_CONNECTION_URL_PROXY represents the url used for the high performance connection setup
31+
const CNR_CONNECTION_URL_PROXY = "http://127.0.0.1/api/call.cgi" //nolint
3232

33-
// ISPAPI_CONNECTION_URL_LIVE represents the url used for the default connection setup
34-
const ISPAPI_CONNECTION_URL_LIVE = "https://api.ispapi.net/api/call.cgi" //nolint
33+
// CNR_CONNECTION_URL_LIVE represents the url used for the default connection setup
34+
const CNR_CONNECTION_URL_LIVE = "https://api.rrpproxy.net/api/call.cgi" //nolint
3535

36-
// ISPAPI_CONNECTION_URL_OTE represents the url used for the OT&E (demo system) connection setup
37-
const ISPAPI_CONNECTION_URL_OTE = "https://api-ote.ispapi.net/api/call.cgi" //nolint
36+
// CNR_CONNECTION_URL_OTE represents the url used for the OT&E (demo system) connection setup
37+
const CNR_CONNECTION_URL_OTE = "https://api-ote.rrpproxy.net/api/call.cgi" //nolint
3838

3939
var rtm = RTM.GetInstance()
4040

@@ -59,14 +59,27 @@ type APIClient struct {
5959
curlopts map[string]string
6060
ua string
6161
logger LG.ILogger
62+
subUser string
63+
}
64+
65+
// RequestOptions represents the options for an API request.
66+
type RequestOptions struct {
67+
SetUserView bool // SetUserView indicates whether to set a data view to a given subuser.
68+
}
69+
70+
// NewRequestOptions creates a new instance of RequestOptions with default values.
71+
func NewRequestOptions() *RequestOptions {
72+
return &RequestOptions{
73+
SetUserView: true,
74+
}
6275
}
6376

6477
// NewAPIClient represents the constructor for struct APIClient.
6578
func NewAPIClient() *APIClient {
6679
cl := &APIClient{
6780
debugMode: false,
6881
socketTimeout: 300 * time.Second,
69-
socketURL: ISPAPI_CONNECTION_URL_LIVE,
82+
socketURL: CNR_CONNECTION_URL_LIVE,
7083
socketConfig: SC.NewSocketConfig(),
7184
curlopts: map[string]string{},
7285
ua: "",
@@ -105,7 +118,7 @@ func (cl *APIClient) GetProxy() (string, error) {
105118
if exists {
106119
return val, nil
107120
}
108-
return "", errors.New("No proxy configuration available")
121+
return "", errors.New("no proxy configuration available")
109122
}
110123

111124
// SetReferer method to set a value for HTTP Header `Referer` to use for API communication
@@ -124,7 +137,7 @@ func (cl *APIClient) GetReferer() (string, error) {
124137
if exists {
125138
return val, nil
126139
}
127-
return "", errors.New("No configuration available for HTTP Header `Referer`")
140+
return "", errors.New("no configuration available for HTTP Header `Referer`")
128141
}
129142

130143
// EnableDebugMode method to enable Debug Output to logger
@@ -139,6 +152,24 @@ func (cl *APIClient) DisableDebugMode() *APIClient {
139152
return cl
140153
}
141154

155+
// SetUserView method to set a data view to a given subuser
156+
func (cl *APIClient) SetUserView(uid string) *APIClient {
157+
cl.subUser = uid
158+
return cl
159+
}
160+
161+
// ResetUserView method to reset data view back from subuser to user
162+
func (cl *APIClient) ResetUserView() *APIClient {
163+
cl.subUser = ""
164+
return cl
165+
}
166+
167+
// UseHighPerformanceConnectionSetup to activate high performance conneciton setup
168+
func (cl *APIClient) UseHighPerformanceConnectionSetup() *APIClient {
169+
cl.SetURL(CNR_CONNECTION_URL_PROXY)
170+
return cl
171+
}
172+
142173
// GetPOSTData method to Serialize given command for POST request
143174
// including connection configuration data
144175
func (cl *APIClient) GetPOSTData(cmd map[string]string, secured ...bool) string {
@@ -180,7 +211,7 @@ func (cl *APIClient) GetPOSTData(cmd map[string]string, secured ...bool) string
180211
func (cl *APIClient) GetSession() (string, error) {
181212
sessid := cl.socketConfig.GetSession()
182213
if len(sessid) == 0 {
183-
return "", errors.New("Could not find an active session")
214+
return "", errors.New("could not find an active session")
184215
}
185216
return sessid, nil
186217
}
@@ -219,7 +250,6 @@ func (cl *APIClient) GetVersion() string {
219250
// Please save/update that map into user session
220251
func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
221252
sessionobj["socketcfg"] = map[string]string{
222-
"entity": cl.socketConfig.GetSystemEntity(),
223253
"session": cl.socketConfig.GetSession(),
224254
}
225255
return cl
@@ -229,7 +259,6 @@ func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
229259
// to rebuild and reuse connection settings
230260
func (cl *APIClient) ReuseSession(sessionobj map[string]interface{}) *APIClient {
231261
cfg := sessionobj["socketcfg"].(map[string]string)
232-
cl.socketConfig.SetSystemEntity(cfg["entity"])
233262
cl.SetSession(cfg["session"])
234263
return cl
235264
}
@@ -252,12 +281,6 @@ func (cl *APIClient) SetSession(value string) *APIClient {
252281
return cl
253282
}
254283

255-
// SetRemoteIPAddress method to set an Remote IP Address to be used for API communication
256-
func (cl *APIClient) SetRemoteIPAddress(value string) *APIClient {
257-
cl.socketConfig.SetRemoteAddress(value)
258-
return cl
259-
}
260-
261284
// SetCredentials method to set Credentials to be used for API communication
262285
func (cl *APIClient) SetCredentials(uid string, pw string) *APIClient {
263286
cl.socketConfig.SetLogin(uid)
@@ -268,7 +291,7 @@ func (cl *APIClient) SetCredentials(uid string, pw string) *APIClient {
268291
// SetRoleCredentials method to set Role User Credentials to be used for API communication
269292
func (cl *APIClient) SetRoleCredentials(uid string, role string, pw string) *APIClient {
270293
if len(role) > 0 {
271-
return cl.SetCredentials(uid+"!"+role, pw)
294+
return cl.SetCredentials(uid+":"+role, pw)
272295
}
273296
return cl.SetCredentials(uid, pw)
274297
}
@@ -281,9 +304,9 @@ func (cl *APIClient) Login(params ...string) *R.Response {
281304
otp = params[0]
282305
}
283306
cl.SetOTP(otp)
284-
rr := cl.Request(map[string]interface{}{"COMMAND": "StartSession"})
307+
rr := cl.Request(map[string]interface{}{"COMMAND": "StartSession"}, &RequestOptions{SetUserView: false})
285308
if rr.IsSuccess() {
286-
col := rr.GetColumn("SESSION")
309+
col := rr.GetColumn("SESSIONID")
287310
if col != nil {
288311
cl.SetSession(col.GetData()[0])
289312
} else {
@@ -312,9 +335,9 @@ func (cl *APIClient) LoginExtended(params ...interface{}) *R.Response {
312335
for k, v := range parameters {
313336
cmd[k] = v
314337
}
315-
rr := cl.Request(cmd)
338+
rr := cl.Request(cmd, &RequestOptions{SetUserView: false})
316339
if rr.IsSuccess() {
317-
col := rr.GetColumn("SESSION")
340+
col := rr.GetColumn("SESSIONID")
318341
if col != nil {
319342
cl.SetSession(col.GetData()[0])
320343
} else {
@@ -328,15 +351,26 @@ func (cl *APIClient) LoginExtended(params ...interface{}) *R.Response {
328351
func (cl *APIClient) Logout() *R.Response {
329352
rr := cl.Request(map[string]interface{}{
330353
"COMMAND": "EndSession",
331-
})
354+
}, &RequestOptions{SetUserView: false})
332355
if rr.IsSuccess() {
333356
cl.SetSession("")
334357
}
335358
return rr
336359
}
337360

338361
// Request method to perform API request using the given command
339-
func (cl *APIClient) Request(cmd map[string]interface{}) *R.Response {
362+
func (cl *APIClient) Request(cmd map[string]interface{}, opts ...*RequestOptions) *R.Response {
363+
// Use default RequestOptions if opts is not available
364+
options := NewRequestOptions()
365+
if len(opts) > 0 {
366+
options = opts[0]
367+
}
368+
369+
// Check if SetUserView option is enabled and subUser is set
370+
if (options.SetUserView) && (len(cl.subUser) > 0) {
371+
cmd["SUBUSER"] = cl.subUser
372+
}
373+
340374
// flatten nested api command bulk parameters
341375
newcmd := cl.flattenCommand(cmd)
342376
// auto convert umlaut names to punycode
@@ -460,42 +494,22 @@ func (cl *APIClient) RequestAllResponsePages(cmd map[string]string) []R.Response
460494
return responses
461495
}
462496

463-
// SetUserView method to set a data view to a given subuser
464-
func (cl *APIClient) SetUserView(uid string) *APIClient {
465-
cl.socketConfig.SetUser(uid)
466-
return cl
467-
}
468-
469-
// ResetUserView method to reset data view back from subuser to user
470-
func (cl *APIClient) ResetUserView() *APIClient {
471-
cl.socketConfig.SetUser("")
472-
return cl
473-
}
474-
475-
// UseHighPerformanceConnectionSetup to activate high performance conneciton setup
476-
func (cl *APIClient) UseHighPerformanceConnectionSetup() *APIClient {
477-
cl.SetURL(ISPAPI_CONNECTION_URL_PROXY)
478-
return cl
479-
}
480-
481497
// UseDefaultConnectionSetup to activate default conneciton setup (the default anyways)
482498
func (cl *APIClient) UseDefaultConnectionSetup() *APIClient {
483-
cl.SetURL(ISPAPI_CONNECTION_URL_LIVE)
499+
cl.SetURL(CNR_CONNECTION_URL_LIVE)
484500
return cl
485501
}
486502

487503
// UseOTESystem method to set OT&E System for API communication
488504
func (cl *APIClient) UseOTESystem() *APIClient {
489-
cl.SetURL(ISPAPI_CONNECTION_URL_OTE)
490-
cl.socketConfig.SetSystemEntity("1234")
505+
cl.SetURL(CNR_CONNECTION_URL_OTE)
491506
return cl
492507
}
493508

494509
// UseLIVESystem method to set LIVE System for API communication
495510
// Usage of LIVE System is active by default.
496511
func (cl *APIClient) UseLIVESystem() *APIClient {
497-
cl.SetURL(ISPAPI_CONNECTION_URL_LIVE)
498-
cl.socketConfig.SetSystemEntity("54cd")
512+
cl.SetURL(CNR_CONNECTION_URL_LIVE)
499513
return cl
500514
}
501515

0 commit comments

Comments
 (0)