-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpManager.m
172 lines (138 loc) · 5.67 KB
/
httpManager.m
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
classdef httpManager
properties
frontendUrl
baseUrl
token
end
methods (Static)
function success = register(baseUrl, name, password, description)
request = struct( ...
'project_name', name, ...
'password', password, ...
'description', description);
url = strcat(baseUrl, "/project/register");
[status, ~] = httpManager.postData(url, request);
success = status == matlab.net.http.StatusCode.Created;
end
function [obj, success] = fromLogin(baseUrl, frontendUrl, name, password)
request = struct( ...
'project_name', name, ...
'password', password);
url = strcat(baseUrl, "/project/login");
[status, respData] = httpManager.postData(url, request);
if status == matlab.net.http.StatusCode.OK
success = true;
obj = httpManager(baseUrl, frontendUrl, respData.token);
else
success = false;
obj = respData;
end
end
function [status, respData] = postData(url, body)
import matlab.net.http.*
packaged = MessageBody(body);
r = RequestMessage(RequestMethod.POST, ...
[HeaderField("Accept", "*/*"), ...
HeaderField("Content-Type", "application/json"), ...
HeaderField("Connection", "keep-alive")], ...
packaged);
resp = send(r, url);
status = resp.StatusCode;
respData = resp.Body.Data;
end
function [status, respData] = getData(url)
import matlab.net.http.*
r = RequestMessage(RequestMethod.GET, ...
[HeaderField("Accept", "*/*"), ...
HeaderField("Content-Type", "application/json"), ...
HeaderField("Connection", "keep-alive")]);
resp = send(r, url);
status = resp.StatusCode;
respData = resp.Body.Data;
end
end
methods
function [success, shortCode] = getDemonstrationShortCode(obj, gid)
reqUrl = strcat(obj.baseUrl, ...
"/demonstration", ...
"/get-shortcode", ...
"/", string(gid), ...
"?token=", obj.token);
[status, shortCode] = httpManager.getData(reqUrl);
success = status == matlab.net.http.StatusCode.OK;
end
function exists = demonstrationExists(obj, projectName, gid)
reqUrl = strcat(obj.baseUrl, ...
"/demonstration", ...
"/demonstration-exists", ...
"/", projectName, ...
"/", string(gid));
[status, ~] = httpManager.getData(reqUrl);
exists = status == 200;
end
function [success, urlCode] = getUrl(obj, pid)
reqUrl = strcat(obj.baseUrl, ...
"/participant", ...
"/", string(pid), ...
"/get-url", ...
"?token=", obj.token);
[status, urlCode] = httpManager.getData(reqUrl);
success = status == matlab.net.http.StatusCode.OK;
end
function success = removeTrial(obj, pid, tiid)
url = strcat(obj.baseUrl, ...
"/participant/remove-trial");
data = struct( ...
'participant_id', string(pid), ...
'trial_id', string(tiid), ...
'token', obj.token);
status = httpManager.postData(url, data);
success = status == matlab.net.http.StatusCode.OK;
end
function [success, outfileLocation] = downloadGesture(obj, tiid, pid, gesture_index, fileLocation)
try
tiid = string(tiid);
gesture_index = string(gesture_index);
pid = string(pid);
url = strcat(obj.baseUrl, ...
"/gesture-data/get-gesture/", pid, ...
"/", tiid, ...
"/", gesture_index);
saveLocation = fullfile(fileLocation, getFileName());
outfileLocation = websave(saveLocation, url, "token", obj.token);
disp(strcat("Downloaded file at ", outfileLocation));
success = true;
catch
warning("problem downloading gesture from " + tiid + ", index: " + gesture_index);
outfileLocation = "";
success = false;
end
function filename = getFileName()
filename = strcat(tiid, "-", gesture_index, ".csv");
end
end
function [success, trials] = getCompletedTrials(obj)
url = strcat(obj.baseUrl, ...
"/participant/get-completed-trials?token=", ...
obj.token);
[status, trials] = httpManager.getData(url);
success = status == matlab.net.http.StatusCode.OK;
end
function success = pushTrial(obj, pid, trialStruct)
data = struct( ...
'participant_id', string(pid), ...
'trial', trialStruct, ...
'token', obj.token);
url = strcat(obj.baseUrl, "/participant/push-trial");
status = httpManager.postData(url, data);
success = status == matlab.net.http.StatusCode.Created;
end
end
methods
function obj = httpManager(baseUrl, frontendUrl, token)
obj.frontendUrl = frontendUrl;
obj.baseUrl = baseUrl;
obj.token = token;
end
end
end