Skip to content

Commit

Permalink
feat(centralnic-reseller-go-sdk): introducing CNR Go-SDK API Connector
Browse files Browse the repository at this point in the history
BREAKING CHANGE: We have deprecated the Hexonet Go SDK and introduced the CentralNic Reseller Go SDK.
  • Loading branch information
AsifNawaz-cnic committed Sep 3, 2024
1 parent f6b96a0 commit 99ed838
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 310 deletions.
76 changes: 27 additions & 49 deletions apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ import (
"strings"
"time"

IDN "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/idntranslator"
LG "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/logger"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/response"
RTM "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/responsetemplatemanager"
SC "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/socketconfig"
IDN "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/idntranslator"
LG "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/logger"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/response"
RTM "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/responsetemplatemanager"
SC "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/socketconfig"
)

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

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

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

var rtm = RTM.GetInstance()

Expand Down Expand Up @@ -66,7 +66,7 @@ func NewAPIClient() *APIClient {
cl := &APIClient{
debugMode: false,
socketTimeout: 300 * time.Second,
socketURL: ISPAPI_CONNECTION_URL_LIVE,
socketURL: CNR_CONNECTION_URL_LIVE,
socketConfig: SC.NewSocketConfig(),
curlopts: map[string]string{},
ua: "",
Expand Down Expand Up @@ -105,7 +105,7 @@ func (cl *APIClient) GetProxy() (string, error) {
if exists {
return val, nil
}
return "", errors.New("No proxy configuration available")
return "", errors.New("no proxy configuration available")
}

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

// EnableDebugMode method to enable Debug Output to logger
Expand All @@ -139,6 +139,12 @@ func (cl *APIClient) DisableDebugMode() *APIClient {
return cl
}

// UseHighPerformanceConnectionSetup to activate high performance conneciton setup
func (cl *APIClient) UseHighPerformanceConnectionSetup() *APIClient {
cl.SetURL(CNR_CONNECTION_URL_PROXY)
return cl
}

// GetPOSTData method to Serialize given command for POST request
// including connection configuration data
func (cl *APIClient) GetPOSTData(cmd map[string]string, secured ...bool) string {
Expand Down Expand Up @@ -180,7 +186,7 @@ func (cl *APIClient) GetPOSTData(cmd map[string]string, secured ...bool) string
func (cl *APIClient) GetSession() (string, error) {
sessid := cl.socketConfig.GetSession()
if len(sessid) == 0 {
return "", errors.New("Could not find an active session")
return "", errors.New("could not find an active session")
}
return sessid, nil
}
Expand Down Expand Up @@ -219,7 +225,6 @@ func (cl *APIClient) GetVersion() string {
// Please save/update that map into user session
func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
sessionobj["socketcfg"] = map[string]string{
"entity": cl.socketConfig.GetSystemEntity(),
"session": cl.socketConfig.GetSession(),
}
return cl
Expand All @@ -229,7 +234,6 @@ func (cl *APIClient) SaveSession(sessionobj map[string]interface{}) *APIClient {
// to rebuild and reuse connection settings
func (cl *APIClient) ReuseSession(sessionobj map[string]interface{}) *APIClient {
cfg := sessionobj["socketcfg"].(map[string]string)
cl.socketConfig.SetSystemEntity(cfg["entity"])
cl.SetSession(cfg["session"])
return cl
}
Expand All @@ -252,12 +256,6 @@ func (cl *APIClient) SetSession(value string) *APIClient {
return cl
}

// SetRemoteIPAddress method to set an Remote IP Address to be used for API communication
func (cl *APIClient) SetRemoteIPAddress(value string) *APIClient {
cl.socketConfig.SetRemoteAddress(value)
return cl
}

// SetCredentials method to set Credentials to be used for API communication
func (cl *APIClient) SetCredentials(uid string, pw string) *APIClient {
cl.socketConfig.SetLogin(uid)
Expand All @@ -268,7 +266,7 @@ func (cl *APIClient) SetCredentials(uid string, pw string) *APIClient {
// SetRoleCredentials method to set Role User Credentials to be used for API communication
func (cl *APIClient) SetRoleCredentials(uid string, role string, pw string) *APIClient {
if len(role) > 0 {
return cl.SetCredentials(uid+"!"+role, pw)
return cl.SetCredentials(uid+":"+role, pw)
}
return cl.SetCredentials(uid, pw)
}
Expand All @@ -283,7 +281,7 @@ func (cl *APIClient) Login(params ...string) *R.Response {
cl.SetOTP(otp)
rr := cl.Request(map[string]interface{}{"COMMAND": "StartSession"})
if rr.IsSuccess() {
col := rr.GetColumn("SESSION")
col := rr.GetColumn("SESSIONID")
if col != nil {
cl.SetSession(col.GetData()[0])
} else {
Expand Down Expand Up @@ -314,7 +312,7 @@ func (cl *APIClient) LoginExtended(params ...interface{}) *R.Response {
}
rr := cl.Request(cmd)
if rr.IsSuccess() {
col := rr.GetColumn("SESSION")
col := rr.GetColumn("SESSIONID")
if col != nil {
cl.SetSession(col.GetData()[0])
} else {
Expand Down Expand Up @@ -460,42 +458,22 @@ func (cl *APIClient) RequestAllResponsePages(cmd map[string]string) []R.Response
return responses
}

// SetUserView method to set a data view to a given subuser
func (cl *APIClient) SetUserView(uid string) *APIClient {
cl.socketConfig.SetUser(uid)
return cl
}

// ResetUserView method to reset data view back from subuser to user
func (cl *APIClient) ResetUserView() *APIClient {
cl.socketConfig.SetUser("")
return cl
}

// UseHighPerformanceConnectionSetup to activate high performance conneciton setup
func (cl *APIClient) UseHighPerformanceConnectionSetup() *APIClient {
cl.SetURL(ISPAPI_CONNECTION_URL_PROXY)
return cl
}

// UseDefaultConnectionSetup to activate default conneciton setup (the default anyways)
func (cl *APIClient) UseDefaultConnectionSetup() *APIClient {
cl.SetURL(ISPAPI_CONNECTION_URL_LIVE)
cl.SetURL(CNR_CONNECTION_URL_LIVE)
return cl
}

// UseOTESystem method to set OT&E System for API communication
func (cl *APIClient) UseOTESystem() *APIClient {
cl.SetURL(ISPAPI_CONNECTION_URL_OTE)
cl.socketConfig.SetSystemEntity("1234")
cl.SetURL(CNR_CONNECTION_URL_OTE)
return cl
}

// UseLIVESystem method to set LIVE System for API communication
// Usage of LIVE System is active by default.
func (cl *APIClient) UseLIVESystem() *APIClient {
cl.SetURL(ISPAPI_CONNECTION_URL_LIVE)
cl.socketConfig.SetSystemEntity("54cd")
cl.SetURL(CNR_CONNECTION_URL_LIVE)
return cl
}

Expand Down
Loading

0 comments on commit 99ed838

Please sign in to comment.