-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cli.cr
152 lines (117 loc) · 2.58 KB
/
test_cli.cr
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
141
142
143
144
145
146
147
148
149
150
151
require "http/client"
require "json"
require "readline"
OTAN = "https://otan.aius.u-strasbg.fr"
SALES = "http://localhost:3000"
token = nil
STDIN << "Login: "
STDIN.flush
login = STDIN.read_line
STDIN << "Password: "
STDIN.flush
password = STDIN.noecho &.gets.try &.chomp
def ask_nicely(type, url, body)
headers = HTTP::Headers {
"Content-Type" => "application/json"
}
if type == :post
HTTP::Client.post url, headers: headers, body: body.to_json
elsif type == :delete
HTTP::Client.delete url, headers: headers, body: body.to_json
else type == :get
HTTP::Client.get url, headers: headers
end
end
def post(url, body)
ask_nicely :post, url, body
end
def delete(url, body)
ask_nicely :delete, url, body
end
def get(url)
ask_nicely :get, url, {} of String => String
end
response = post OTAN + "/token", {
"username" => login,
"password" => password,
}
if response.status_code == 200
response = JSON.parse response.body
if response["token"]
puts "Connected."
token = response["token"]
else
puts "No token received. Dying nao."
exit
end
else
puts "HTTP error (#{response.status_code})."
puts response
STDOUT << response.body
puts "Could not authenticate. Dying nao."
exit
end
commands = Hash(String, NamedTuple(help: String, exec: Proc(Array(String), Nil))) {
"sales" => {
help: "List products on sale.",
exec: ->(arg : Array(String)) {
answer = JSON.parse get(SALES + "/products").body
answer.each do |product|
puts "#{product["id"]}: #{product["name"]} - #{product["price"]}"
end
return
}
},
"new-product" => {
help: "Add a product to the sales list.",
exec: ->(arg : Array(String)) {
if ! arg[2]?
puts "usage: sell <name> <price>"
return
end
name = arg[1].to_s
price = arg[2].to_f64
answer = JSON.parse post(SALES + "/products", {
"name" => name,
"price" => price,
"token" => token
}).body
puts answer
return
}
},
}
commands["help"] = {
help: "Lists the available commands.",
exec: ->(arg : Array(String)) {
commands.each do |command, data|
puts "#{command}: #{data[:help]}"
end
return
}
}
while line = Readline.readline "> "
arg = line.split /[ \t]+/
if commands[arg[0]]?
commands[arg[0]][:exec].call arg
elsif arg[0] == "delete-product"
if ! arg[1]?
puts "usage: delete-product <id>"
next
end
id = arg[1].to_i
answer = delete(SALES + "/product/#{id}", {
"token" => token
}).body
puts answer
elsif arg[0] == "sell"
if ! arg[1]?
puts "usage: sell <id>"
next
end
id = arg[1].to_i
puts "id: #{id}"
else
puts "Unknown command: #{arg[0]}"
end
end