hmx_client 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,61 +1,76 @@
1
- require 'cli-colorize'
2
-
3
- module HmxClient
4
- module Helpers
5
- include CLIColorize
6
-
7
- def getFromUser(prompt)
8
- content = ''
9
- done = false
10
- puts prompt
11
- while(!done)
12
- line = gets.strip
13
- if (line[line.length-1] == '\\')
14
- done = false
15
- content = content + line[0 .. line.length-2]
16
- else
17
- content = content + line
18
- done = true
19
- end
20
- end
21
- content
22
- end
23
- def error(msg)
24
- puts(colorize(msg, :red))
25
- exit 1
26
- end
27
- def longest(items)
28
- items.map { |i| i.to_s.length }.sort.last
29
- end
30
-
31
- def display(msg="", newline=true)
32
- if newline
33
- puts(msg)
34
- else
35
- print(msg)
36
- STDOUT.flush
37
- end
38
- end
39
-
40
- def display_table(objects, columns, headers)
41
- lengths = []
42
- columns.each_with_index do |column, index|
43
- header = headers[index]
44
- lengths << longest([header].concat(objects.map { |o| o[column].to_s }))
45
- end
46
- display_row headers, lengths
47
- display_row headers.map { |header| "-" * header.length }, lengths
48
- objects.each do |row|
49
- display_row columns.map { |column| row[column] }, lengths
50
- end
51
- end
52
-
53
- def display_row(row, lengths)
54
- row.zip(lengths).each do |column, length|
55
- format = column.is_a?(Fixnum) ? "%#{length}s " : "%-#{length}s "
56
- display format % column, false
57
- end
58
- display
59
- end
60
- end
61
- end
1
+ require 'cli-colorize'
2
+
3
+ module HmxClient
4
+ module Helpers
5
+ include CLIColorize
6
+
7
+ def getFromUser(prompt)
8
+ content = ''
9
+ done = false
10
+ puts prompt
11
+ while(!done)
12
+ line = gets.strip
13
+ if (line[line.length-1] == '\\')
14
+ done = false
15
+ content = content + line[0 .. line.length-2]
16
+ else
17
+ content = content + line
18
+ done = true
19
+ end
20
+ end
21
+ content
22
+ end
23
+ def error(msg)
24
+ puts(colorize(msg, :red))
25
+ exit 1
26
+ end
27
+ def longest(items)
28
+ items.map { |i| i.to_s.length }.sort.last
29
+ end
30
+
31
+ def display(msg="", newline=true)
32
+ if newline
33
+ puts(msg)
34
+ else
35
+ print(msg)
36
+ STDOUT.flush
37
+ end
38
+ end
39
+
40
+ def display_table(objects, columns, headers)
41
+ lengths = []
42
+ columns.each_with_index do |column, index|
43
+ header = headers[index]
44
+ lengths << longest([header].concat(objects.map { |o| o[column].to_s }))
45
+ end
46
+ display_row headers, lengths
47
+ display_row headers.map { |header| "-" * header.length }, lengths
48
+ objects.each do |row|
49
+ display_row columns.map { |column| row[column] }, lengths
50
+ end
51
+ end
52
+
53
+ def display_row(row, lengths)
54
+ row.zip(lengths).each do |column, length|
55
+ format = column.is_a?(Fixnum) ? "%#{length}s " : "%-#{length}s "
56
+ display format % column, false
57
+ end
58
+ display
59
+ end
60
+
61
+ def display_tab(data)
62
+ resp = ''
63
+ data.each { | line |
64
+ line.each { | cell |
65
+ if (cell.is_a? Array)
66
+ cell.each { | inner | resp = resp + "%20.20s\t" % inner }
67
+ else
68
+ resp = resp + "%20.20s\t" % cell
69
+ end
70
+ }
71
+ resp = resp + "\n"
72
+ }
73
+ dout resp
74
+ end
75
+ end
76
+ end
@@ -1,215 +1,148 @@
1
- require 'rubygems'
2
- require 'rest_client'
3
- require 'json'
4
- require 'digest/md5'
5
-
6
- module HmxClient
7
-
8
- include HmxClient::Helpers
9
-
10
- class HmxException < Exception
11
- attr :msg
12
- def initialize(msg)
13
- @msg = msg
14
- end
15
- end
16
-
17
- class Hmx
18
-
19
- def loginApi(url, urlapi)
20
- @urlapi = urlapi
21
- @context = JSON.parse (RestClient.get url).to_str
22
- end
23
- def login(config)
24
- puts config if $debug
25
- @context = nil;
26
- @urlapi = config[:api]
27
- if config.has_key?(:context)
28
- @context = JSON.parse(config[:context])
29
- #Do a null check
30
- begin
31
- performDistRequest(:info, [])
32
- rescue => e
33
- @context = nil;
34
- end
35
- end
36
- if (@context.nil?)
37
- session = requestSession(config[:partition], config[:user])
38
- salty = session['MXSession']['salty']
39
- sendString = config[:password] + ':' + salty
40
- digestToSend = Digest::MD5.hexdigest(sendString)
41
- @context = validateUser(config[:partition], config[:user], digestToSend, session['MXSession']['sessionId'])
42
- config[:context] = JSON.generate(@context)
43
- end
44
- end
45
- def initialize
46
- @distMap = {
47
- :getPartitions => [ 'admin', 'GETPARTITIONS', 'partitions', []],
48
- :clonePartition => [ 'admin', 'CLONEPARTITION', 'partition', [ { :pos => 0, :name => 'destination' }, { :pos => 1, :name => 'copyStyle' } ] ],
49
- :info => [ 'user', 'INFO', 'response', []],
50
- :getData => [ 'user', 'GETDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
51
- :getContent => [ 'user', 'GETCONTENT', 'content', [ { :pos => 0, :name => 'displayName' }]],
52
- :putData => ['user', 'PUTDATA', 'data', [ { :pos => 0, :name => 'data', :file => 1, :json => 1 }]],
53
- :putSimpleData => [ 'user', 'PUTSIMPLEDATA', 'data', [ { :pos => 0, :name => 'displayName'}, { :pos => 1, :name => 'content', :file => 1}]],
54
- :query => [ 'user', 'QUERY', 'result', [ { :pos => 0, :name => 'index' }, { :pos => 1, :name => 'params' }]],
55
- :dynquery => ['user', 'DYNQUERY', 'result', [ { :pos => 0, :name => 'typeName' }, { :pos => 1, :name => 'filterFn' }, { :pos => 2, :name => 'mapFn' } ]],
56
- :deleteData => [ 'user', 'DELETEDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
57
- :getSequenceSize => [ 'user', 'GETSEQUENCESIZE', 'size', [ { :pos => 0, :name => 'typeName' }]],
58
- :execPartSeq => [ 'user', 'EXECPARTIALSEQUENCE', 'response', [ { :pos => 0, :name => 'typeName' },
59
- { :pos => 1, :name => 'fn' },
60
- { :pos => 2, :name => 'start' },
61
- { :pos => 3, :name => 'size' } ] ] ,
62
- :requestSession => [ 'user', 'REQUESTSESSION', 'session', [ { :pos => 0, :name => 'partition' }, { :pos => 1, :name => 'user'}]],
63
- :validateUser => [ 'user', 'VALIDATEUSER', 'context', [ { :pos => 0, :name => 'partition' },
64
- { :pos => 1, :name => 'user' },
65
- { :pos => 2, :name => 'hashPassword' },
66
- { :pos => 3, :name => 'session' }
67
- ]],
68
- :submitTask => [ 'user', 'SUBMITTASK', 'task', [ { :pos => 0, :name => 'task' } ]],
69
- :runView => [ 'user', 'RUNVIEW', 'response', [ { :pos => 0, :name => 'view' }, { :pos => 1, :name => 'params' } ] ],
70
- :getTypes => [ 'admin', 'GETTYPES', 'types', []],
71
- :getType => [ 'admin', 'GETTYPE', 'type', [ { :pos => 0, :name => 'typeName' } ]],
72
- :updateType => [ 'admin', 'UPDATETYPE', 'type', [ { :pos => 0, :name => 'type' }]],
73
- :getFn => [ 'user', 'GETFN', 'fnContent', [ { :pos =>0, :name => 'fnName' } ]],
74
- :putFn => [ 'user', 'PUTFN', 'response', [ { :pos => 0, :name => 'fnName' }, { :pos => 1, :name => 'fnContent', :file => 1 } ]],
75
- :expireSessions => [ 'admin', 'EXPIRESESSIONS', 'sessions', []],
76
- :getAllTasks => [ 'task', 'GETALLTASKS', 'tasks', []],
77
- :getTask => [ 'task', 'GETTASK', 'task', [ { :pos=> 0, :name => 'taskId' } ]],
78
- :purgeTasks => [ 'task', 'PURGETASKS', 'response', []],
79
- :getFountainState => [ 'admin', 'GETFOUNTAINSTATE', 'state', []],
80
- :setFountainId => [ 'admin', 'SETFOUNTAINID', 'dummy', [ { :pos => 0, :name => 'fountain' }, { :pos => 1, :name=> 'value' }]],
81
- :getFountain => [ 'user', 'AUTOID', 'id', [ { :pos => 0, :name => 'fountain' } ]],
82
- :bootstrap => ['special', 'BOOTSTRAP', 'response', []]
83
- }
84
- end
85
- def performDistRequest(cmd, args)
86
- spec = @distMap[cmd]
87
- hash = getSenderHash()
88
- spec[3].each { | param |
89
- # if the args has enough , use that
90
- # otherwise, prompt for the parameter
91
- if (args.length > param[:pos])
92
- hash[param[:name]] = args[param[:pos]]
93
- elsif (param.has_key?(:file) && !HmxClient::Command.fileIn.nil?)
94
- # Content comes from file
95
- realFile = File.expand_path(HmxClient::Command.fileIn)
96
- puts "Reading #{ realFile } to get parameter #{ param[:name] }"
97
- hash[param[:name]] = File.open(realFile) { | f |
98
- c = ''
99
- while(line = f.gets)
100
- c = c + line
101
- end
102
- c
103
- }
104
- else
105
- hash[param[:name]] = getFromUser("Please enter the value of #{ param[:name] }")
106
- end
107
- if (param.has_key?(:json))
108
- hash[param[:name]] = JSON.parse(hash[param[:name]])
109
- end
110
- }
111
- standardRequest(spec[0], spec[1], hash, spec[2])
112
- end
113
- def performRequest(prefix, function, command)
114
- puts "Send data #{ JSON.generate(command) }" if HmxClient::Command.debug?
115
- puts "URL is #{ @urlapi+'/'+prefix } " if HmxClient::Command.debug?
116
- response = RestClient.post @urlapi+'/'+prefix, :function=>function, :params=>JSON.generate(command), :multipart=>true
117
- puts "Raw response is #{ response.to_str }" if HmxClient::Command.debug?
118
- return JSON.parse(response.to_str)
119
- end
120
- def standardRequest(prefix, commandName, params, returnPart)
121
- response = performRequest(prefix, commandName, params)
122
- puts "JSON response is #{ response }" if HmxClient::Command.debug?
123
- if response['inError']
124
- raise HmxException.new(response['exception']), "Error returned from server"
125
- end
126
- return response[returnPart]
127
- end
128
- def getSenderHash()
129
- hash = Hash.new
130
- hash['context'] = @context if @context
131
- return hash
132
- end
133
- def getData(args)
134
- performDistRequest(:getData, args)
135
- end
136
- def getContent(args)
137
- performDistRequest(:getContent, args)
138
- end
139
- def putData(args)
140
- performDistRequest(:putData, args)
141
- end
142
- def putSimpleData(args)
143
- performDistRequest(:putSimpleData, args)
144
- end
145
- def query(*args)
146
- performDistRequest(:query, args)
147
- end
148
- def dynquery(*args)
149
- performDistRequest(:dynquery, args)
150
- end
151
- def deleteData(*args)
152
- performDistRequest(:deleteData, args)
153
- end
154
- def getSequenceSize(*args)
155
- performDistRequest(:getSequenceSize, args)
156
- end
157
- def execPartialSequence(*args)
158
- performDistRequest(:execPartSeq, args)
159
- end
160
- def requestSession(*args)
161
- performDistRequest(:requestSession, args)
162
- end
163
- def validateUser(*args)
164
- performDistRequest(:validateUser, args)
165
- end
166
- def runView(*args)
167
- performDistRequest(:runView, args)
168
- end
169
- def getTypes(*args)
170
- performDistRequest(:getTypes, args)
171
- end
172
- def updateType(*args)
173
- performDistRequest(:updateType, args)
174
- end
175
- def getType(*args)
176
- performDistRequest(:getType, args)
177
- end
178
- def getFn(args)
179
- performDistRequest(:getFn, args)
180
- end
181
- def putFn(args)
182
- performDistRequest(:putFn, args)
183
- end
184
- def expireSessions(args)
185
- performDistRequest(:expireSessions, args)
186
- end
187
- def getAllTasks(args)
188
- performDistRequest(:getAllTasks, args)
189
- end
190
- def getTask(args)
191
- performDistRequest(:getTask, args)
192
- end
193
- def purgeTasks(args)
194
- performDistRequest(:purgeTasks, args)
195
- end
196
- def getFountainState(args)
197
- performDistRequest(:getFountainState, args)
198
- end
199
- def setFountainId(args)
200
- performDistRequest(:setFountainId, args)
201
- end
202
- def getFountain(args)
203
- performDistRequest(:getFountain, args)
204
- end
205
- def bootstrap
206
- performDistRequest(:bootstrap, [])
207
- end
208
- def getPartitions
209
- performDistRequest(:getPartitions, [])
210
- end
211
- def clonePartition(*args)
212
- performDistRequest(:clonePartition, args)
213
- end
214
- end
215
- end
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'json'
4
+ require 'digest/md5'
5
+
6
+ module HmxClient
7
+
8
+ include HmxClient::Helpers
9
+
10
+ class HmxException < Exception
11
+ attr :msg
12
+ def initialize(msg)
13
+ @msg = msg
14
+ end
15
+ end
16
+
17
+ class Hmx
18
+
19
+ def loginApi(url, urlapi)
20
+ @urlapi = urlapi
21
+ @context = JSON.parse (RestClient.get url).to_str
22
+ end
23
+ def login(config)
24
+ puts config if $debug
25
+ @context = nil;
26
+ @urlapi = config[:api]
27
+ if config.has_key?(:context)
28
+ @context = JSON.parse(config[:context])
29
+ #Do a null check
30
+ begin
31
+ doInfo([])
32
+ rescue => e
33
+ @context = nil;
34
+ end
35
+ end
36
+ if (@context.nil?)
37
+ session = doRequestSession([config[:partition], config[:user]])
38
+ salty = session['MXSession']['salty']
39
+ sendString = config[:password] + ':' + salty
40
+ digestToSend = Digest::MD5.hexdigest(sendString)
41
+ @context = doValidateUser([config[:partition], config[:user], digestToSend, session['MXSession']['sessionId']])
42
+ config[:context] = JSON.generate(@context)
43
+ end
44
+ end
45
+ @@distMap = {
46
+ :GetPartitions => [ 'admin', 'GETPARTITIONS', 'partitions', []],
47
+ :ClonePartition => [ 'admin', 'CLONEPARTITION', 'partition', [ { :pos => 0, :name => 'destination' }, { :pos => 1, :name => 'copyStyle' } ] ],
48
+ :Info => [ 'user', 'INFO', 'response', []],
49
+ :GetData => [ 'user', 'GETDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
50
+ :GetContent => [ 'user', 'GETCONTENT', 'content', [ { :pos => 0, :name => 'displayName' }]],
51
+ :PutData => ['user', 'PUTDATA', 'data', [ { :pos => 0, :name => 'data', :file => 1, :json => 1 }]],
52
+ :PutSimpleData => [ 'user', 'PUTSIMPLEDATA', 'data', [ { :pos => 0, :name => 'displayName'}, { :pos => 1, :name => 'content', :file => 1}]],
53
+ :Query => [ 'user', 'QUERY', 'result', [ { :pos => 0, :name => 'index' }, { :pos => 1, :name => 'params' }]],
54
+ :Dynquery => ['user', 'DYNQUERY', 'result', [ { :pos => 0, :name => 'typeName' }, { :pos => 1, :name => 'filterFn' }, { :pos => 2, :name => 'mapFn' } ]],
55
+ :DeleteData => [ 'user', 'DELETEDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
56
+ :GetSequenceSize => [ 'user', 'GETSEQUENCESIZE', 'size', [ { :pos => 0, :name => 'typeName' }]],
57
+ :ExecPartSeq => [ 'user', 'EXECPARTIALSEQUENCE', 'response', [ { :pos => 0, :name => 'typeName' },
58
+ { :pos => 1, :name => 'fn' },
59
+ { :pos => 2, :name => 'start' },
60
+ { :pos => 3, :name => 'size' } ] ] ,
61
+ :RequestSession => [ 'user', 'REQUESTSESSION', 'session', [ { :pos => 0, :name => 'partition' }, { :pos => 1, :name => 'user'}]],
62
+ :ValidateUser => [ 'user', 'VALIDATEUSER', 'context', [ { :pos => 0, :name => 'partition' },
63
+ { :pos => 1, :name => 'user' },
64
+ { :pos => 2, :name => 'hashPassword' },
65
+ { :pos => 3, :name => 'session' }
66
+ ]],
67
+ :SubmitTask => [ 'user', 'SUBMITTASK', 'task', [ { :pos => 0, :name => 'task' } ]],
68
+ :RunView => [ 'user', 'RUNVIEW', 'response', [ { :pos => 0, :name => 'view' }, { :pos => 1, :name => 'params' } ] ],
69
+ :GetTypes => [ 'admin', 'GETTYPES', 'types', []],
70
+ :GetType => [ 'admin', 'GETTYPE', 'type', [ { :pos => 0, :name => 'typeName' } ]],
71
+ :UpdateType => [ 'admin', 'UPDATETYPE', 'type', [ { :pos => 0, :name => 'type' }]],
72
+ :GetFn => [ 'user', 'GETFN', 'fnContent', [ { :pos =>0, :name => 'fnName' } ]],
73
+ :PutFn => [ 'user', 'PUTFN', 'response', [ { :pos => 0, :name => 'fnName' }, { :pos => 1, :name => 'fnContent', :file => 1 }, { :pos => 2, :name => 'fnLanguage'} ]],
74
+ :ExpireSessions => [ 'admin', 'EXPIRESESSIONS', 'sessions', []],
75
+ :GetAllTasks => [ 'task', 'GETALLTASKS', 'tasks', []],
76
+ :GetTask => [ 'task', 'GETTASK', 'task', [ { :pos=> 0, :name => 'taskId' } ]],
77
+ :PurgeTasks => [ 'task', 'PURGETASKS', 'response', []],
78
+ :GetFountainState => [ 'admin', 'GETFOUNTAINSTATE', 'state', []],
79
+ :SetFountainId => [ 'admin', 'SETFOUNTAINID', 'dummy', [ { :pos => 0, :name => 'fountain' }, { :pos => 1, :name=> 'value' }]],
80
+ :GetFountain => [ 'user', 'AUTOID', 'id', [ { :pos => 0, :name => 'fountain' } ]],
81
+ :Bootstrap => ['special', 'BOOTSTRAP', 'response', []]
82
+ }
83
+ # Meta initialize the class methods
84
+ @@distMap.keys.each { | commandName |
85
+ method_name = ( "do" + commandName.to_s).to_sym
86
+ define_method method_name do | arg |
87
+ performDistRequest(commandName.to_sym, arg)
88
+ end
89
+ }
90
+ def initialize
91
+ end
92
+ # Implementation note - the doXXX commands are defined in the section above and basically
93
+ # call performDistRequest for the symbol :XXX with the arguments provided. So by adding to
94
+ # @@distMap we automatically provide hmx.doXXX support for that call
95
+ def performDistRequest(cmd, args)
96
+ spec = @@distMap[cmd]
97
+ hash = getSenderHash()
98
+ spec[3].each { | param |
99
+ # if the args has enough , use that
100
+ # otherwise, prompt for the parameter
101
+ if (args.length > param[:pos])
102
+ hash[param[:name]] = args[param[:pos]]
103
+ elsif (param.has_key?(:file) && !HmxClient::Command.fileIn.nil?)
104
+ # Content comes from file
105
+ realFile = File.expand_path(HmxClient::Command.fileIn)
106
+ puts "Reading #{ realFile } to get parameter #{ param[:name] }"
107
+ hash[param[:name]] = File.open(realFile) { | f |
108
+ c = ''
109
+ while(line = f.gets)
110
+ c = c + line
111
+ end
112
+ c
113
+ }
114
+ else
115
+ hash[param[:name]] = getFromUser("Please enter the value of #{ param[:name] }")
116
+ end
117
+ if (param.has_key?(:json))
118
+ hash[param[:name]] = JSON.parse(hash[param[:name]])
119
+ end
120
+ }
121
+ standardRequest(spec[0], spec[1], hash, spec[2])
122
+ end
123
+ def performRequest(prefix, function, command)
124
+ puts "Send data #{ JSON.generate(command) }" if HmxClient::Command.debug?
125
+ puts "URL is #{ @urlapi+'/'+prefix } " if HmxClient::Command.debug?
126
+ response = RestClient.post @urlapi+'/'+prefix, :function=>function, :params=>JSON.generate(command), :multipart=>true
127
+ puts "Raw response is #{ response.to_str }" if HmxClient::Command.debug?
128
+ if (response.length > 0)
129
+ return JSON.parse(response.to_str)
130
+ else
131
+ return ""
132
+ end
133
+ end
134
+ def standardRequest(prefix, commandName, params, returnPart)
135
+ response = performRequest(prefix, commandName, params)
136
+ puts "JSON response is #{ response }" if HmxClient::Command.debug?
137
+ if response['inError']
138
+ raise HmxException.new(response['exception']), "Error returned from server"
139
+ end
140
+ return response[returnPart]
141
+ end
142
+ def getSenderHash()
143
+ hash = Hash.new
144
+ hash['context'] = @context if @context
145
+ return hash
146
+ end
147
+ end
148
+ end