-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotiPhysioClient.java
More file actions
370 lines (314 loc) · 16.8 KB
/
MotiPhysioClient.java
File metadata and controls
370 lines (314 loc) · 16.8 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* MotiPhysioClient.java
* =====================
* Public Moti-Physio API — Java Sample Client
*
* Demonstrates all 8 API endpoints using the built-in java.net.http.HttpClient
* (Java 11+) and the org.json library for JSON handling.
*
* Requirements:
* Java 11 or later
* org.json jar (https://mvnrepository.com/artifact/org.json/json)
* OR add to pom.xml / build.gradle — see README.md for details.
*
* Compile & run (with org.json on the classpath):
* javac -cp .:json-20231013.jar MotiPhysioClient.java
* java -cp .:json-20231013.jar MotiPhysioClient
*/
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Optional;
import org.json.JSONArray;
import org.json.JSONObject;
public class MotiPhysioClient {
// -----------------------------------------------------------------------
// Configuration — replace these values with your own credentials
// -----------------------------------------------------------------------
private static final String BASE_URL = "https://api.motiphysio.com";
private static final String PROGRAM_ID = "YOUR_PROGRAM_ID";
private static final String SECURITY_KEY = "YOUR_SECURITY_KEY";
// Shared HttpClient instance (thread-safe, reuse across requests)
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
// -----------------------------------------------------------------------
// Helper: POST request → returns raw JSON string
//
// Sends a POST request with the given JSON body.
// Throws RuntimeException when:
// - The HTTP status is not 200
// - The response body contains an "error" key (business-logic error)
// -----------------------------------------------------------------------
private static String post(String endpoint, JSONObject payload) throws Exception {
String url = BASE_URL + endpoint;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.timeout(Duration.ofSeconds(30))
.POST(HttpRequest.BodyPublishers.ofString(payload.toString()))
.build();
HttpResponse<String> response =
HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new RuntimeException(
"HTTP error " + response.statusCode() + ": " + response.body());
}
String body = response.body();
// Business-logic errors use HTTP 200 with {"error": "..."} in the body.
// We detect them by checking whether the body starts with '{' and contains
// an "error" key.
if (body.startsWith("{")) {
JSONObject obj = new JSONObject(body);
if (obj.has("error")) {
throw new RuntimeException("API error: " + obj.getString("error"));
}
}
return body;
}
// -----------------------------------------------------------------------
// Helper: sleep without checked exception (for readability in main)
// -----------------------------------------------------------------------
private static void sleep(long ms) {
try { Thread.sleep(ms); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
// -----------------------------------------------------------------------
// 1. get_user_list
// Returns all members registered under your program.
// Pass 0 for startPeriod / endPeriod to omit the filter.
// -----------------------------------------------------------------------
public static JSONArray getUserList(long startPeriod, long endPeriod) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
// Only add optional date-filter fields when a non-zero value is given
if (startPeriod > 0) payload.put("start_period", startPeriod);
if (endPeriod > 0) payload.put("end_period", endPeriod);
String body = post("/v1/get_user_list", payload);
return new JSONArray(body);
}
// -----------------------------------------------------------------------
// 2. get_user_info
// Returns detailed profile information for a single member.
// -----------------------------------------------------------------------
public static JSONObject getUserInfo(String userId) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
String body = post("/v1/get_user_info", payload);
return new JSONObject(body);
}
// -----------------------------------------------------------------------
// 3. get_user_static_analysis_list
// Returns Static (posture) analysis records for a member.
// The "index" in each record → pass as analysis_index to report endpoint.
// -----------------------------------------------------------------------
public static JSONArray getUserStaticAnalysisList(
String userId, long startPeriod, long endPeriod) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
if (startPeriod > 0) payload.put("start_period", startPeriod);
if (endPeriod > 0) payload.put("end_period", endPeriod);
return new JSONArray(post("/v1/get_user_static_analysis_list", payload));
}
// -----------------------------------------------------------------------
// 4. get_user_ohs_analysis_list
// Returns OHS (Overhead Squat / core function) analysis records.
// -----------------------------------------------------------------------
public static JSONArray getUserOhsAnalysisList(
String userId, long startPeriod, long endPeriod) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
if (startPeriod > 0) payload.put("start_period", startPeriod);
if (endPeriod > 0) payload.put("end_period", endPeriod);
return new JSONArray(post("/v1/get_user_ohs_analysis_list", payload));
}
// -----------------------------------------------------------------------
// 5. get_user_ols_analysis_list
// Returns OLS (One-Leg Stand / balance) analysis records.
// -----------------------------------------------------------------------
public static JSONArray getUserOlsAnalysisList(
String userId, long startPeriod, long endPeriod) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
if (startPeriod > 0) payload.put("start_period", startPeriod);
if (endPeriod > 0) payload.put("end_period", endPeriod);
return new JSONArray(post("/v1/get_user_ols_analysis_list", payload));
}
// -----------------------------------------------------------------------
// 6. get_user_static_analysis_report
// Returns Presigned S3 URLs for all Static analysis report images.
// analysisIndex: 0-based index from getUserStaticAnalysisList().
// URLs are valid for 24 hours (86400 seconds).
// -----------------------------------------------------------------------
public static JSONObject getUserStaticAnalysisReport(
String userId, int analysisIndex) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
payload.put("analysis_index", analysisIndex);
return new JSONObject(post("/v1/get_user_static_analysis_report", payload));
}
// -----------------------------------------------------------------------
// 7. get_user_ohs_analysis_report
// Returns Presigned S3 URLs for OHS report images.
// analysisIndex: 0-based index from getUserOhsAnalysisList().
// -----------------------------------------------------------------------
public static JSONObject getUserOhsAnalysisReport(
String userId, int analysisIndex) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
payload.put("analysis_index", analysisIndex);
return new JSONObject(post("/v1/get_user_ohs_analysis_report", payload));
}
// -----------------------------------------------------------------------
// 8. get_user_ols_analysis_report
// Returns Presigned S3 URLs for OLS report images (Left & Right).
// analysisIndex: 0-based index from getUserOlsAnalysisList().
// -----------------------------------------------------------------------
public static JSONObject getUserOlsAnalysisReport(
String userId, int analysisIndex) throws Exception {
JSONObject payload = new JSONObject();
payload.put("program_id", PROGRAM_ID);
payload.put("security_key", SECURITY_KEY);
payload.put("user_id", userId);
payload.put("analysis_index", analysisIndex);
return new JSONObject(post("/v1/get_user_ols_analysis_report", payload));
}
// -----------------------------------------------------------------------
// main — demonstrates all 8 endpoints in sequence
// -----------------------------------------------------------------------
public static void main(String[] args) {
System.out.println("=".repeat(60));
System.out.println(" Public Moti-Physio API — Java Sample");
System.out.println("=".repeat(60));
try {
// ------------------------------------------------------------
// 1. Member list
// ------------------------------------------------------------
System.out.println("\n[1] Fetching member list ...");
JSONArray users = getUserList(0, 0); // 0 = no filter
System.out.printf(" → %d member(s) found%n", users.length());
for (int i = 0; i < Math.min(3, users.length()); i++) {
JSONObject u = users.getJSONObject(i);
System.out.printf(" %s | %s | %s | %s%n",
u.getString("user_id"),
u.getString("name"),
u.getString("gender"),
u.getString("birth"));
}
// Use the first user or fall back to the sample ID
String userId = users.length() > 0
? users.getJSONObject(0).getString("user_id")
: "1489-00001";
sleep(200);
// ------------------------------------------------------------
// 2. Member detail
// ------------------------------------------------------------
System.out.printf("%n[2] Fetching info for user '%s' ...%n", userId);
JSONObject info = getUserInfo(userId);
System.out.println(" → " + info.toString(2));
sleep(200);
// ------------------------------------------------------------
// 3. Static analysis list
// ------------------------------------------------------------
System.out.printf("%n[3] Fetching Static analysis list for '%s' ...%n", userId);
JSONArray staticList = getUserStaticAnalysisList(userId, 0, 0);
System.out.printf(" → %d record(s)%n", staticList.length());
for (int i = 0; i < Math.min(3, staticList.length()); i++) {
JSONObject r = staticList.getJSONObject(i);
System.out.printf(" index=%d time=%d ver=%s%n",
r.getInt("index"),
r.getLong("measurement_time"),
r.getString("version"));
}
sleep(200);
// ------------------------------------------------------------
// 4. OHS analysis list
// ------------------------------------------------------------
System.out.printf("%n[4] Fetching OHS analysis list for '%s' ...%n", userId);
JSONArray ohsList = getUserOhsAnalysisList(userId, 0, 0);
System.out.printf(" → %d record(s)%n", ohsList.length());
sleep(200);
// ------------------------------------------------------------
// 5. OLS analysis list
// ------------------------------------------------------------
System.out.printf("%n[5] Fetching OLS analysis list for '%s' ...%n", userId);
JSONArray olsList = getUserOlsAnalysisList(userId, 0, 0);
System.out.printf(" → %d record(s)%n", olsList.length());
sleep(200);
// ------------------------------------------------------------
// 6. Static analysis report
// ------------------------------------------------------------
if (staticList.length() > 0) {
int firstStaticIndex = staticList.getJSONObject(0).getInt("index");
System.out.printf("%n[6] Fetching Static report (analysis_index=%d) ...%n",
firstStaticIndex);
JSONObject report = getUserStaticAnalysisReport(userId, firstStaticIndex);
int expiryHours = report.optInt("url_expiration_seconds", 86400) / 3600;
System.out.printf(" → URLs valid for %d hours%n", expiryHours);
// Print the first URL from the first available report type
JSONObject reports = report.getJSONObject("reports");
for (String key : reports.keySet()) {
JSONArray pages = reports.getJSONArray(key);
if (pages.length() > 0) {
String url = pages.getJSONObject(0).getString("presigned_url");
System.out.printf(" [%s] page 0 → %s...%n",
key, url.substring(0, Math.min(80, url.length())));
break;
}
}
} else {
System.out.println("\n[6] No Static analysis records — skipping report request");
}
sleep(200);
// ------------------------------------------------------------
// 7. OHS analysis report
// ------------------------------------------------------------
if (ohsList.length() > 0) {
int firstOhsIndex = ohsList.getJSONObject(0).getInt("index");
System.out.printf("%n[7] Fetching OHS report (analysis_index=%d) ...%n",
firstOhsIndex);
JSONObject report = getUserOhsAnalysisReport(userId, firstOhsIndex);
JSONArray pages = report.getJSONObject("reports")
.optJSONArray("ohs_result_sheet");
System.out.printf(" → %d page(s) in ohs_result_sheet%n",
pages != null ? pages.length() : 0);
} else {
System.out.println("\n[7] No OHS analysis records — skipping report request");
}
sleep(200);
// ------------------------------------------------------------
// 8. OLS analysis report
// ------------------------------------------------------------
if (olsList.length() > 0) {
int firstOlsIndex = olsList.getJSONObject(0).getInt("index");
System.out.printf("%n[8] Fetching OLS report (analysis_index=%d) ...%n",
firstOlsIndex);
JSONObject report = getUserOlsAnalysisReport(userId, firstOlsIndex);
JSONArray pages = report.getJSONObject("reports")
.optJSONArray("ols_result_sheet");
System.out.printf(" → %d page(s) in ols_result_sheet%n",
pages != null ? pages.length() : 0);
} else {
System.out.println("\n[8] No OLS analysis records — skipping report request");
}
System.out.println("\nDone.");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
}
}