summaryrefslogtreecommitdiffstats
path: root/web.vala
blob: e782623965646cfec9c01a1f5f39a3b75a8f3193 (plain)
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
/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

public class Web {
	private Soup.SessionAsync session;
	private static string server = "https://shop.kreativitaet-trifft-technik.de";
	uint64 user = 0;

	public Web() {
		session = new Soup.SessionAsync();
		var cookies = new Soup.CookieJar();
		session.add_feature(cookies);
	}

	public void login(uint64 id) {
		//stdout.printf("login: %llu\n", id);

		var message = new Soup.Message("POST", server+"/login");
		var post_data = "userid=%llu".printf(id);
		message.set_request("application/x-www-form-urlencoded", Soup.MemoryUse.COPY, post_data.data);
		session.send_message(message);

		stdout.write(message.response_body.data);
		stdout.printf("\n");

		/* on success */
		this.user = id;
	}

	public void logout() {
		if(this.is_logged_in()) {
			//stdout.printf("logout\n", id);

			var message = new Soup.Message("GET", server+"/logout");
			session.send_message(message);

			stdout.write(message.response_body.data);
			stdout.printf("\n");

			this.user = 0;
		}
	}

	public void buy(uint64 article) {
		if(this.is_logged_in()) {
			//stdout.printf(" buy: %llu\n", article);

			var message = new Soup.Message("POST", server+"/buy");
			var post_data = "article=%llu".printf(article);
			message.set_request("application/x-www-form-urlencoded", Soup.MemoryUse.COPY, post_data.data);
			session.send_message(message);

			stdout.write(message.response_body.data);
			stdout.printf("\n");
		} else {
			/* not logged into the system */
		}
	}

	public bool is_logged_in() {
		return (user != 0);
	}
}