-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsEnmCliCmd.cls
140 lines (104 loc) · 3.65 KB
/
clsEnmCliCmd.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsEnmCliCmd"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private authEndpoint As String
Private exitEndpoint As String
Private applEndpoint As String
Private blnAsync as Boolean
Private oServerXmlHttp as Object
Private pTimeWait as Integer
' Properties
Property Get TimeWait() As Integer
TimeWait = pTimeWait
End Property
Property Let TimeWait(value As Integer)
pTimeWait = value
End Property
''' PRIVATE FUNCTIONS '''
Private Sub Class_Initialize()
Set oServerXmlHttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
pTimeWait = 10 / 86400
blnAsync = False
End Sub
''' PUBLIC FUNCTIONS '''
Public Sub login(ByVal baseUrl, ByVal login, ByVal password As String)
authEndpoint = baseUrl & "/login?IDToken1=" & login & "&IDToken2=" & WorksheetFunction.EncodeURL(password)
exitEndpoint = baseUrl & "/logout"
applEndpoint = baseUrl & "/script-engine/services/command/"
With oServerXmlHttp
.SetOption(2) = (.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS)
.Open "POST", authEndpoint, blnAsync
.setRequestHeader "Content-Type", "application/json"
.Send
While oServerXmlHttp.readyState <> 4
DoEvents
Wend
End With
End Sub
Public Sub logout()
With oServerXmlHttp
.Open "GET", exitEndpoint, blnAsync
.Send
While oServerXmlHttp.readyState <> 4
DoEvents
Wend
End With
End Sub
Public Function execute(ByVal cmd as String)
With oServerXmlHttp
.Open "POST", applEndpoint, blnAsync
sBoundary = "----WebKitFormBoundary" & RandomString(16)
sPayLoad = sPayLoad & "--" & sBoundary & vbCrLf
sPayLoad = sPayLoad & "Content-Disposition: form-data; name=""command""" & vbCrLf & vbCrLf
sPayLoad = sPayLoad & cmd & vbCrLf
sPayLoad = sPayLoad & "--" & sBoundary & "--" & vbCrLf
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & sBoundary
.setRequestHeader "Content-Length", LenB(sPayLoad)
.Send (sPayLoad)
While oServerXmlHttp.readyState <> 4
DoEvents
Wend
process_id = .getResponseHeader("process_id")
procUrl = applEndpoint & "output/" & process_id & "?max_size=20000"
Application.Wait (Now + timeWait)
.Open "GET", procUrl, blnAsync
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Accept", "text/plain; charset=UTF-8" '"application/json, text/javascript, */*; q=0.01"
.Send ("Command=" & cmd)
While oServerXmlHttp.readyState <> 4
DoEvents
Wend
execute = .responsetext
End With
End Function
''' HELPER FUNCTIONS '''
Private Function RandomString(Length As Integer)
'PURPOSE: Create a Randomized String of Characters
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim CharacterBank As Variant
Dim x As Long
Dim str As String
'Test Length Input
If Length < 1 Then
MsgBox "Length variable must be greater than 0"
Exit Function
End If
CharacterBank = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _
"k", "l", "m", "n", "o", "p", "q", "r","s", "t", "u", "v", "w", "x", _
"y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", _
"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", _
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
'Randomly Select Characters One-by-One
For x = 1 To Length
Randomize
str = str & CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))
Next x
'Output Randomly Generated String
RandomString = str
End Function