Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(response class): merge with responsetemplate, add responsetranslator #136

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM ghcr.io/scop/bash-completion/test:centos7 AS bash_completion
FROM mcr.microsoft.com/devcontainers/go:latest

RUN apt-get update && apt-get install -y \
bash-completion \
&& rm -rf /var/lib/apt/lists/*

# Set a default value for ZSH_CUSTOM if it's not already set
ENV ZSH_CUSTOM=/home/vscode/.oh-my-zsh/custom

# Clone the powerlevel10k theme for zsh and change owner
RUN git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k \
&& chown -R vscode:vscode $ZSH_CUSTOM/themes/powerlevel10k

# Clone the zsh-autosuggestions repository and change owner
RUN git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions \
&& chown -R vscode:vscode $ZSH_CUSTOM/plugins/zsh-autosuggestions

COPY --chown=vscode:vscode configurations/.p10k.zsh /home/vscode/.p10k.zsh

COPY --chown=vscode:vscode configurations/.zshrc /home/vscode/.zshrc

COPY --chown=vscode:vscode configurations/p10k-instant-prompt-vscode.zsh /home/vscode/.cache/p10k-instant-prompt-vscode.zsh

# Set the default shell to Zsh
SHELL ["/bin/zsh", "-c"]

# Start a simple loop to keep the container running
CMD ["zsh", "-c", "while true; do sleep 3600; done"]
1,735 changes: 1,735 additions & 0 deletions .devcontainer/configurations/.p10k.zsh

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions .devcontainer/configurations/.zshrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh

ZSH_THEME="powerlevel10k/powerlevel10k"

plugins=(git zsh-autosuggestions)

source $ZSH/oh-my-zsh.sh

DISABLE_AUTO_UPDATE=true
DISABLE_UPDATE_PROMPT=true

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
323 changes: 323 additions & 0 deletions .devcontainer/configurations/p10k-instant-prompt-vscode.zsh

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "rtldev-middleware-go-sdk",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"go.gopath": "/go",
"go.goroot": "/usr/local/go",
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/bin/zsh"
}
}
},
"extensions": [
"golang.go"
]
}
},
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/git:1": {}
},
"forwardPorts": [
8080
],
"mounts": [
"source=${localEnv:HOME}${localEnv:USERPROFILE},target=/WSL_USER,type=bind,consistency=cached"
],
"postCreateCommand": "zsh ./.devcontainer/post-create.sh",
"runArgs": ["--name", "rtldev-middleware-go-sdk"],
"remoteUser": "vscode"
}
13 changes: 13 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

# Link WSL bash_history to bash_history
ln -sf /WSL_USER/.zsh_history ~/.zsh_history

# Install commitizen globally
npm install -g commitizen

# Run go mod tidy
go mod tidy

# Add configuration to .czrc for commitizen
echo '{"path": "cz-conventional-changelog"}' >> ~/.czrc
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ coverage.html
node_modules

# go binary
go-sdk
rtldev-middleware-go-sdk
34 changes: 34 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
#########################
#########################
## Golang Linter rules ##
#########################
#########################

# https://raw.githubusercontent.com/super-linter/super-linter/main/.github/linters/.golangci.yml

issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- gosec
- goconst
linters:
enable:
- gosec
- unconvert
- gocyclo
- goconst
- goimports
- gocritic
- revive
- shadow # Added shadow linter
linters-settings:
errcheck:
# report about assignment of errors to blank identifier
# default is false: such cases aren't reported by default.
check-blank: true
gocyclo:
# minimal code complexity to report, 30 by default
min-complexity: 15
34 changes: 11 additions & 23 deletions apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package apiclient
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand All @@ -20,6 +20,7 @@ import (
"strings"
"time"

IDN "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3/idntranslator"
LG "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3/logger"
R "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3/response"
RTM "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3/responsetemplatemanager"
Expand Down Expand Up @@ -364,7 +365,7 @@ func (cl *APIClient) Request(cmd map[string]interface{}) *R.Response {
}
req, err := http.NewRequest("POST", cfg["CONNECTION_URL"], strings.NewReader(data))
if err != nil {
tpl := rtm.GetTemplate("httperror").GetPlain()
tpl := rtm.GetTemplate("httperror")
r := R.NewResponse(tpl, newcmd, cfg)
if cl.debugMode {
cl.logger.Log(secured, r, err.Error())
Expand All @@ -380,7 +381,7 @@ func (cl *APIClient) Request(cmd map[string]interface{}) *R.Response {
}
resp, err2 := client.Do(req)
if err2 != nil {
tpl := rtm.GetTemplate("httperror").GetPlain()
tpl := rtm.GetTemplate("httperror")
r := R.NewResponse(tpl, newcmd, cfg)
if cl.debugMode {
cl.logger.Log(secured, r, err2.Error())
Expand All @@ -389,9 +390,9 @@ func (cl *APIClient) Request(cmd map[string]interface{}) *R.Response {
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
response, err := ioutil.ReadAll(resp.Body)
response, err := io.ReadAll(resp.Body)
if err != nil {
tpl := rtm.GetTemplate("httperror").GetPlain()
tpl := rtm.GetTemplate("httperror")
r := R.NewResponse(tpl, newcmd, cfg)
if cl.debugMode {
cl.logger.Log(secured, r, err.Error())
Expand All @@ -404,7 +405,7 @@ func (cl *APIClient) Request(cmd map[string]interface{}) *R.Response {
}
return r
}
tpl := rtm.GetTemplate("httperror").GetPlain()
tpl := rtm.GetTemplate("httperror")
r := R.NewResponse(tpl, newcmd, cfg)
if cl.debugMode {
cl.logger.Log(secured, r)
Expand Down Expand Up @@ -522,9 +523,6 @@ func (cl *APIClient) flattenCommand(cmd map[string]interface{}) map[string]strin

// autoIDNConvert method to translate all whitelisted parameter values to punycode, if necessary
func (cl *APIClient) autoIDNConvert(cmd map[string]string) map[string]string {
newcmd := map[string]string{
"COMMAND": "ConvertIDN",
}
// don't convert for convertidn command to avoid endless loop
pattern := regexp.MustCompile(`(?i)^CONVERTIDN$`)
mm := pattern.MatchString(cmd["COMMAND"])
Expand Down Expand Up @@ -553,25 +551,15 @@ func (cl *APIClient) autoIDNConvert(cmd map[string]string) map[string]string {
if mm {
toconvert = append(toconvert, val)
idxs = append(idxs, key)
} else {
newcmd[key] = val
}
}
if len(toconvert) == 0 {
return cmd
}
r := cl.Request(map[string]interface{}{
"COMMAND": "ConvertIDN",
"DOMAIN": toconvert,
})
if !r.IsSuccess() {
return cmd
}
col := r.GetColumn("ACE")
if col != nil {
for idx, pc := range col.GetData() {
cmd[idxs[idx]] = pc
}
r := IDN.Convert(toconvert)

for idx, pc := range r {
cmd[idxs[idx]] = pc.PUNYCODE
}
return cmd
}
20 changes: 11 additions & 9 deletions apiclient/apiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func TestGetPOSTDataSecured(t *testing.T) {
cl.SetCredentials("", "")
}

func TestEnableDebugMode(t *testing.T) {
func TestEnableDebugMode(_ *testing.T) {
cl.EnableDebugMode()
}

func TestDisableDebugMode(t *testing.T) {
func TestDisableDebugMode(_ *testing.T) {
cl.DisableDebugMode()
}

Expand Down Expand Up @@ -91,12 +91,13 @@ func TestAutoIDNConvertCommand(t *testing.T) {
cl.UseOTESystem()
r := cl.Request(map[string]interface{}{
"COMMAND": "CheckDomains",
"DOMAiN": []string{"example.com", "dömäin.example", "example.net"},
"DOMAIN": []string{"example.com", "dömäin.example", "example.net"},
})
if !r.IsSuccess() || r.GetCode() != 200 || r.GetDescription() != "Command completed successfully" {
t.Error("TestRequestFlattenCommand: Expected response to succeed." + strconv.Itoa(r.GetCode()) + r.GetDescription())
}
cmd := r.GetCommand()

val1, exists1 := cmd["DOMAIN0"]
val2, exists2 := cmd["DOMAIN1"]
val3, exists3 := cmd["DOMAIN2"]
Expand Down Expand Up @@ -417,10 +418,11 @@ func TestLogout1(t *testing.T) {

func TestLogout2(t *testing.T) {
tpl := R.NewResponse(
rtm.GetTemplate("login200").GetPlain(),
rtm.GetTemplate("login200"),
map[string]string{
"COMMAND": "StartSession",
},
nil,
)
rec := tpl.GetRecord(0)
sessid, err := rec.GetDataByKey("SESSION")
Expand All @@ -438,9 +440,9 @@ func TestLogout2(t *testing.T) {
// validate against mocked API response [200 < r.statusCode > 299] // need mocking
// validate against mocked API response [200 < r.statusCode > 299, no debug] // need mocking

func TestRequestNextResponsePage1(t *testing.T) {
func TestRequestNextResponsePage1(t *testing.T) { // nolint: gocyclo
r := R.NewResponse(
rtm.GetTemplate("listP0").GetPlain(),
rtm.GetTemplate("listP0"),
map[string]string{
"COMMAND": "QueryDomainList",
"LIMIT": "2",
Expand Down Expand Up @@ -497,7 +499,7 @@ func TestRequestNextResponsePage1(t *testing.T) {

func TestRequestNextResponsePage2(t *testing.T) {
r := R.NewResponse(
rtm.GetTemplate("listP0").GetPlain(),
rtm.GetTemplate("listP0"),
map[string]string{
"COMMAND": "QueryDomainList",
"LIMIT": "2",
Expand All @@ -511,10 +513,10 @@ func TestRequestNextResponsePage2(t *testing.T) {
}
}

func TestRequestNextResponsePage3(t *testing.T) {
func TestRequestNextResponsePage3(t *testing.T) { // nolint: gocyclo
cl.DisableDebugMode()
r := R.NewResponse(
rtm.GetTemplate("listP0").GetPlain(),
rtm.GetTemplate("listP0"),
map[string]string{
"COMMAND": "QueryDomainList",
"LIMIT": "2",
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
module github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3

go 1.20
go 1.22.3

require (
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.25.0
golang.org/x/text v0.15.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading