v / examples / veb_fullstack / product_view_api.v
35 lines · 25 sloc · 724 bytes · b474aa0faf8d055aaced80ed0687ac354d0864a0
Raw
1module main
2
3import json
4import net.http
5
6pub fn get_products(token string) ![]Product {
7 mut header := http.new_header()
8 header.add_custom('token', token)!
9 url := 'http://localhost:8082/controller/products'
10
11 mut config := http.FetchConfig{
12 header: header
13 }
14
15 resp := http.fetch(http.FetchConfig{ ...config, url: url })!
16 products := json.decode([]Product, resp.body)!
17
18 return products
19}
20
21pub fn get_user(token string) !User {
22 mut header := http.new_header()
23 header.add_custom('token', token)!
24
25 url := 'http://localhost:8082/controller/user'
26
27 mut config := http.FetchConfig{
28 header: header
29 }
30
31 resp := http.fetch(http.FetchConfig{ ...config, url: url })!
32 user := json.decode(User, resp.body)!
33
34 return user
35}
36