This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/
hackru_service.dart
131 lines (116 loc) · 3.87 KB
/
hackru_service.dart
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
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:HackRU/models.dart';
const _lcsUrl = 'https://7c5l6v7ip3.execute-api.us-west-2.amazonaws.com/lcs-test'; // prod
//const _lcsUrl = 'https://7c5l6v7ip3.execute-api.us-west-2.amazonaws.com/lcs-test'; // test
const _miscUrl = 'http://hackru-misc.s3-website-us-west-2.amazonaws.com';
var client = new http.Client();
Future<http.Response> getMisc(String endpoint) {
return client.get(_miscUrl + endpoint);
}
String toParam(LcsCredential credential) {
var param = "";
if (credential != null) {
if (credential.isExpired()) {
throw CredentialExpired();
}
param = "?token="+credential.token;
}
return param;
}
Future<http.Response> getLcs(String endpoint, [LcsCredential credential]) {
return client.get(_lcsUrl + endpoint + toParam(credential));
}
Future<http.Response> postLcs(String endpoint, dynamic body, [LcsCredential credential]) async {
var encodedBody = jsonEncode(body);
var result = await client.post(_lcsUrl + endpoint + toParam(credential),
headers: {"content-Type": "applicationi/json"},
body: encodedBody
);
var decoded = jsonDecode(result.body);
if(decoded["statusCode"] != result.statusCode) {
print(decoded);
print("!!!!!!!!!!!!WARNING");
print("body and container status code dissagree actual ${result.statusCode} body: ${decoded['statusCode']}");
print(endpoint);
}
return result;
}
// misc functions
Future<List<String>> sitemap() async {
var response = await getMisc("/");
return await response.body.split("\n");
}
Future<List<String>> events() async {
var response = await getMisc("/events.txt");
return await response.body.split("\n");
}
Future<String> labelUrl() async {
var response = await getMisc("/label-url.txt");
return response.body;
}
Future<List<HelpResource>> helpResources() async {
var response = await getMisc("/resources.json");
var resources = json.decode(response.body);
return resources.map<HelpResource>(
(resource) => new HelpResource.fromJson(resource)
).toList();
}
// lcs functions
// /authorize can give wrong status codes
Future<LcsCredential> login(String email, String password) async {
var result = await postLcs("/authorize", {
"email": email,
"password": password,
});
var body = jsonDecode(result.body);
// quirk with lcs where it puts the actual result as a string
// inside the normal body
if (body["statusCode"] == 200) {
var auth = jsonDecode(body["body"])["auth"];
return LcsCredential.fromJson(auth);
} else if (body["statusCode"] == 403) {
throw LcsLoginFailed();
} else {
throw LcsError(result);
}
}
Future<User> getUser(LcsCredential credential, [String targetEmail]) async {
if (targetEmail == null) {
targetEmail = credential.email;
}
var result = await postLcs("/read", {
"email": credential.email,
"token": credential.token,
"query": {"email": targetEmail}
}, credential);
if (result.statusCode == 200) {
var users = jsonDecode(result.body)["body"];
if (users.length < 1) {
throw NoSuchUser();
}
return User.fromJson(users[0]);
} else {
throw LcsError(result);
}
}
// /update can give wrong status codes
// check if the user credential belongs to is role.director first. or else it will break :(
void updateUserDayOf(LcsCredential credential, User user, String event) async {
print(event);
var result = await postLcs("/update", {
"updates": {"\$set":{"day_of.$event": true}},
"user_email": user.email,
"auth_email": credential.email,
"auth": credential.token,
}, credential);
var decoded = jsonDecode(result.body);
if (decoded["statusCode"] == 400) {
throw UpdateError(decoded["body"]);
} else if (decoded["statusCode"] == 403) {
// BROKEN BECAUSE LCS
throw PermissionError();
} else if (decoded["statusCode"] != 200){
throw LcsError(result);
}
}