Skip to content

Commit

Permalink
feat(centralnic-reseller-go-sdk): introducing CentralNic Reseller Go …
Browse files Browse the repository at this point in the history
…SDK API Connector

BREAKING CHANGE: We have deprecated the Hexonet Go SDK and introduced the CentralNic Reseller Go SDK.
  • Loading branch information
AsifNawaz-cnic committed Sep 20, 2024
1 parent f6b96a0 commit 21c445c
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 431 deletions.
253 changes: 150 additions & 103 deletions apiclient/apiclient.go

Large diffs are not rendered by default.

286 changes: 119 additions & 167 deletions apiclient/apiclient_test.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (c *Column) GetKey() string {

// GetData method to return the column data
func (c *Column) GetData() []string {
if c == nil {
return nil
}
return c.data
}

Expand All @@ -40,7 +43,7 @@ func (c *Column) GetDataByIndex(idx int) (string, error) {
if c.hasDataIndex(idx) {
return c.data[idx], nil
}
return "", errors.New("Index not found")
return "", errors.New("index not found")
}

// hasDataIndex method to check if the given data index exists
Expand Down
4 changes: 2 additions & 2 deletions customlogger/customlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ package customlogger
import (
"fmt"

L "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/logger"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/response"
L "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/logger"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/response"
)

// Logger is a struct representing logger for API communication.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4
module github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5

go 1.22.1 // using this version to make it compatible with dnscontrol

Expand Down
2 changes: 1 addition & 1 deletion idntranslator/idntranslator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package idntranslator_test
import (
"testing"

"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/idntranslator"
"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/idntranslator"

"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package logger
import (
"fmt"

R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/response"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/response"
)

// ilogger reflect basic interface for loggers
Expand Down
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "go-sdk",
"description": "GO/GOLANG SDK for the insanely fast HEXONET API",
"description": "GO/GOLANG SDK for the insanely fast CentralNic Reseller (fka RRPProxy) API",
"version": "0.0.0-development",
"private": true,
"author": {
"name": "Kai Schwarz",
"email": "[email protected]"
"email": "[email protected]"
},
"license": "MIT",
"engines": {
Expand All @@ -17,16 +17,20 @@
"maintainers": [
{
"name": "Kai Schwarz",
"email": "[email protected]"
"email": "[email protected]"
},
{
"name": "Asif Nawaz",
"email": "[email protected]"
}
],
"keywords": [
"hexonet",
"cnr",
"domain",
"api",
"connector",
"isp",
"ispapi",
"cnic",
"centralnic reseller",
"ssl",
"cert",
"dns",
Expand Down
3 changes: 3 additions & 0 deletions record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (c *Record) GetData() map[string]string {

// GetDataByKey method to return the column data at the provided index
func (c *Record) GetDataByKey(key string) (string, error) {
if c == nil {
return "", errors.New("record is nil")
}
if c.hasData(key) {
return c.data[key], nil
}
Expand Down
107 changes: 81 additions & 26 deletions response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"strconv"
"strings"

"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/column"
"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/record"
rp "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/responseparser"
rt "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v4/responsetranslator"
"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/column"
"github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/record"
rp "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/responseparser"
rt "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5/responsetranslator"
)

// Response is a struct used to cover basic functionality to work with
Expand All @@ -31,7 +31,17 @@ type Response struct {
records []record.Record
}

// NewResponse represents the constructor for struct Response.
const defaultCode = 421

// NewResponse creates a new Response object.
// It takes a raw string, a command map, and optional placeholder maps as parameters.
// The function replaces the "PASSWORD" value in the command map with "***" if it exists.
// It then translates the raw string using the command and placeholder maps.
// The function initializes a new Response object with the translated raw string, a hash value,
// the command map, empty column keys and columns, a record index of 0, and an empty records slice.
// If the hash value contains a "PROPERTY" key, the function adds columns and records to the Response object
// based on the values in the "PROPERTY" map.
// The function returns the newly created Response object.
func NewResponse(raw string, cmd map[string]string, phs ...map[string]string) *Response {
ph := map[string]string{}
if len(phs) > 0 {
Expand Down Expand Up @@ -88,20 +98,53 @@ func NewResponse(raw string, cmd map[string]string, phs ...map[string]string) *R
return r
}

// GetCode method to return the API response code
// GetCode returns the code associated with the response.
// If the response does not have a code or if the code is not a valid integer,
// it returns the default code.
//
// The code is retrieved from the response's hash, which is a map[string]interface{}.
// The code value is expected to be stored under the key "CODE" as a string.
// If the code value is not found or is not a string, the default code is returned.
//
// If an error occurs while converting the code string to an integer,
// the default code is returned and the error is logged or handled appropriately.
//
// Returns:
// - int: The code associated with the response.
func (r *Response) GetCode() int {
h := r.GetHash()
c, err := strconv.Atoi(h["CODE"].(string))
if err == nil {
return c
if h == nil {
return defaultCode
}

codeStr, ok := h["CODE"].(string)
if !ok {
// Log or handle the error appropriately
return defaultCode
}

c, err := strconv.Atoi(codeStr)
if err != nil {
// Log or handle the error appropriately
return defaultCode
}
return 421
return c
}

// GetDescription method to return the API response description
func (r *Response) GetDescription() string {
h := r.GetHash()
return h["DESCRIPTION"].(string)
if h == nil {
return ""
}

// Assuming there is a "DESCRIPTION" key in the hash
desc, ok := h["DESCRIPTION"].(string)
if !ok {
// Log or handle the error appropriately
return ""
}
return desc
}

// GetPlain method to return raw API response
Expand Down Expand Up @@ -158,12 +201,23 @@ func (r *Response) IsTmpError() bool {

// IsPending method to check if current operation is returned as pending
func (r *Response) IsPending() bool {
h := r.GetHash()
if val, ok := h["PENDING"]; ok {
if val.(string) == "1" {
return true
cmd := r.GetCommand()

// Check if the COMMAND is AddDomain (case-insensitive)
if command, ok := cmd["COMMAND"]; !ok || !strings.EqualFold(command, "AddDomain") {
return false
}

// Retrieve the STATUS column and check if its data equals REQUESTED (case-insensitive)
status := r.GetColumn("STATUS")
if status != nil {
statusData, err := status.GetDataByIndex(0)
if err != nil {
return false
}
return strings.EqualFold(statusData, "REQUESTED")
}

return false
}

Expand Down Expand Up @@ -199,7 +253,7 @@ func (r *Response) GetColumnIndex(key string, index int) (string, error) {
return d, nil
}
}
return "", errors.New("Column Data Index does not exist")
return "", errors.New("column Data Index does not exist")
}

// GetColumnKeys method to get the list of column names
Expand Down Expand Up @@ -242,7 +296,7 @@ func (r *Response) GetCurrentPageNumber() (int, error) {
if ferr == nil && limit > 0 {
return int(math.Floor(float64(first)/float64(limit))) + 1, nil
}
return 0, errors.New("Could not find current page number")
return 0, errors.New("could not find current page number")
}

// GetCurrentRecord method to get record of current record index
Expand All @@ -263,14 +317,14 @@ func (r *Response) GetFirstRecordIndex() (int, error) {
if err2 == nil {
return idx, nil
}
return 0, errors.New("Could not find first record index")
return 0, errors.New("could not find first record index")
}
}
tlen := len(r.records)
if tlen > 1 {
return 0, nil
}
return 0, errors.New("Could not find first record index")
return 0, errors.New("could not find first record index")
}

// GetLastRecordIndex method to get last record index of the current list query
Expand All @@ -283,14 +337,14 @@ func (r *Response) GetLastRecordIndex() (int, error) {
if err2 == nil {
return idx, nil
}
return 0, errors.New("Could not find last record index")
return 0, errors.New("could not find last record index")
}
}
tlen := r.GetRecordsCount()
if tlen > 0 {
return (tlen - 1), nil
}
return 0, errors.New("Could not find last record index")
return 0, errors.New("could not find last record index")
}

// GetListHash method to get Response as List Hash including useful meta data for tables
Expand Down Expand Up @@ -322,7 +376,7 @@ func (r *Response) GetNextRecord() *record.Record {
func (r *Response) GetNextPageNumber() (int, error) {
cp, err := r.GetCurrentPageNumber()
if err != nil {
return 0, errors.New("Could not find next page number")
return 0, errors.New("could not find next page number")
}
page := cp + 1
pages := r.GetNumberOfPages()
Expand Down Expand Up @@ -385,7 +439,7 @@ func (r *Response) GetPreviousPageNumber() (int, error) {
}
pp := cp - 1
if pp < 1 {
return 0, errors.New("Could not find previous page number")
return 0, errors.New("could not find previous page number")
}
return pp, nil
}
Expand Down Expand Up @@ -472,14 +526,15 @@ func (r *Response) RewindRecordList() *Response {
return r
}

// hasColumn method to check if the given column exists in column list
// hasColumn method to check if the given column exists in column list (case-insensitive)
func (r *Response) hasColumn(key string) (int, bool) {
lowerKey := strings.ToLower(key)
for i, k := range r.columnkeys {
if k == key {
if strings.ToLower(k) == lowerKey {
return i, true
}
}
return 0, false
return -1, false
}

// hasCurrentRecord method to check if the record on current record index exists
Expand Down
Loading

0 comments on commit 21c445c

Please sign in to comment.