-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapi-client.sh
More file actions
executable file
·261 lines (241 loc) · 8.13 KB
/
api-client.sh
File metadata and controls
executable file
·261 lines (241 loc) · 8.13 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
# Flask RESTful API Client Helper Script
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Default server URL
SERVER="http://localhost:5000"
TOKEN=""
print_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
show_usage() {
cat << EOF
${GREEN}Flask RESTful API Client${NC}
Usage: $0 <command> [options]
Commands:
register <username> <password> Register a new user
login <username> <password> Login and get JWT token
get-user Get current user info (requires token)
create-store <name> Create a store (requires token)
get-store <name> Get store details
delete-store <name> Delete store (requires token)
list-stores List all stores
create-item <name> <price> <store_id> Create item (requires token)
get-item <name> Get item details (requires token)
update-item <name> <price> <store_id> Update item (requires token)
delete-item <name> Delete item (requires token)
list-items List all items (requires token)
Options:
--server URL Server URL (default: http://localhost:5000)
--token TOKEN JWT token for authenticated requests
--help Show this help message
Examples:
# Register a new user
$0 register john password123
# Login and save token
TOKEN=\$($0 login john password123 | jq -r '.access_token')
# Create a store (using saved token)
$0 --token "\$TOKEN" create-store "My Store"
# Create an item
$0 --token "\$TOKEN" create-item "Laptop" 999.99 1
# List all items
$0 --token "\$TOKEN" list-items
EOF
exit 0
}
url_encode() {
local string="$1"
echo -n "$string" | jq -sRr @uri
}
make_request() {
local method=$1
local endpoint=$2
local data=$3
local auth=$4
if [ -n "$auth" ]; then
if [ -n "$data" ]; then
curl -s -X "$method" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $auth" \
-d "$data" \
"$SERVER$endpoint" | jq '.' 2>/dev/null || echo "Error: Invalid response"
else
curl -s -X "$method" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $auth" \
"$SERVER$endpoint" | jq '.' 2>/dev/null || echo "Error: Invalid response"
fi
else
if [ -n "$data" ]; then
curl -s -X "$method" \
-H "Content-Type: application/json" \
-d "$data" \
"$SERVER$endpoint" | jq '.' 2>/dev/null || echo "Error: Invalid response"
else
curl -s -X "$method" \
-H "Content-Type: application/json" \
"$SERVER$endpoint" | jq '.' 2>/dev/null || echo "Error: Invalid response"
fi
fi
}
# Parse options
while [[ $# -gt 0 ]]; do
case $1 in
--server)
SERVER="$2"
shift 2
;;
--token)
TOKEN="$2"
shift 2
;;
--help)
show_usage
;;
register)
if [ -z "$2" ] || [ -z "$3" ]; then
print_error "Usage: $0 register <username> <password>"
exit 1
fi
print_info "Registering user: $2"
make_request POST "/register" "{\"username\":\"$2\",\"password\":\"$3\"}"
exit 0
;;
login)
if [ -z "$2" ] || [ -z "$3" ]; then
print_error "Usage: $0 login <username> <password>"
exit 1
fi
print_info "Logging in as: $2"
make_request POST "/user" "{\"username\":\"$2\",\"password\":\"$3\"}"
exit 0
;;
get-user)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token> or login first"
exit 1
fi
print_info "Getting user info"
make_request GET "/user" "" "$TOKEN"
exit 0
;;
create-store)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ]; then
print_error "Usage: $0 create-store <name>"
exit 1
fi
print_info "Creating store: $2"
encoded_name=$(url_encode "$2")
make_request POST "/store/$encoded_name" "" "$TOKEN"
exit 0
;;
get-store)
if [ -z "$2" ]; then
print_error "Usage: $0 get-store <name>"
exit 1
fi
print_info "Getting store: $2"
encoded_name=$(url_encode "$2")
make_request GET "/store/$encoded_name"
exit 0
;;
delete-store)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ]; then
print_error "Usage: $0 delete-store <name>"
exit 1
fi
print_info "Deleting store: $2"
encoded_name=$(url_encode "$2")
make_request DELETE "/store/$encoded_name" "" "$TOKEN"
exit 0
;;
list-stores)
print_info "Listing all stores"
make_request GET "/stores"
exit 0
;;
create-item)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
print_error "Usage: $0 create-item <name> <price> <store_id>"
exit 1
fi
print_info "Creating item: $2"
encoded_name=$(url_encode "$2")
make_request POST "/item/$encoded_name" "{\"price\":$3,\"store_id\":$4}" "$TOKEN"
exit 0
;;
get-item)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ]; then
print_error "Usage: $0 get-item <name>"
exit 1
fi
print_info "Getting item: $2"
encoded_name=$(url_encode "$2")
make_request GET "/item/$encoded_name" "" "$TOKEN"
exit 0
;;
update-item)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
print_error "Usage: $0 update-item <name> <price> <store_id>"
exit 1
fi
print_info "Updating item: $2"
encoded_name=$(url_encode "$2")
make_request PUT "/item/$encoded_name" "{\"price\":$3,\"store_id\":$4}" "$TOKEN"
exit 0
;;
delete-item)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
if [ -z "$2" ]; then
print_error "Usage: $0 delete-item <name>"
exit 1
fi
print_info "Deleting item: $2"
encoded_name=$(url_encode "$2")
make_request DELETE "/item/$encoded_name" "" "$TOKEN"
exit 0
;;
list-items)
if [ -z "$TOKEN" ]; then
print_error "Token required. Use --token <token>"
exit 1
fi
print_info "Listing all items"
make_request GET "/items" "" "$TOKEN"
exit 0
;;
*)
print_error "Unknown command: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
show_usage