-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
226 lines (201 loc) · 7.16 KB
/
index.html
File metadata and controls
226 lines (201 loc) · 7.16 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Exam Sessions Demo</title>
<link rel="stylesheet" href="assets/styles.css" />
</head>
<body>
<main>
<h1>Examplary Embed Sessions Demo</h1>
<p>
This is a simple example of how to use
<a href="https://developers.examplary.ai/embed-sessions"
>Examplary Embed Sessions</a
>.
</p>
<p>
Start by creating an API key on
<a href="https://app.examplary.ai/account/developer" target="_blank"
>this page</a
>, then paste it below.
</p>
<div class="input-group">
<label for="api-key-input">Enter your API Key:</label>
<input
type="text"
id="api-key-input"
placeholder="examp_..."
pattern="examp_[a-z0-9A-Z]{32}"
size="40"
/>
</div>
<details id="step-1" open>
<summary>Step 1: create a user</summary>
<p>
We need a user to associate the session with. As an admin in your
workspace, you can create a new user:
</p>
<button id="create-user-button" onclick="createUser()">
Create user
</button>
<span id="create-user-result"></span>
<script>
// Note: In a real application, you do this server-side to keep your API key secret.
async function createUser() {
const response = await fetch(`${baseUrl}/users`, {
method: "POST",
body: JSON.stringify({
email: `user+${Date.now()}@example.com`,
name: "Demo User",
}),
headers: {
Authorization: apiKey,
"Content-Type": "application/json",
},
});
const data = await response.json();
userId = data.id;
document.querySelector("#create-user-result").textContent =
`Created user with ID ${data.id}`;
document.querySelector("#step-2").open = true;
}
</script>
</details>
<details id="step-2">
<summary>Step 2: create an embed session</summary>
<p>Now we can create an embed session for the user we just created:</p>
<button id="create-session-button" onclick="createEmbedSession()">
Create embed session
</button>
<span id="create-session-result"></span>
<script>
// Note: In a real application, you do this server-side to keep your API key secret.
async function createEmbedSession() {
if (!userId) {
return alert("Please create a user first.");
}
const response = await fetch(`${baseUrl}/embed-sessions`, {
method: "POST",
body: JSON.stringify({
flow: "generate-exam",
actor: userId,
presets: {
subject: "Mathematics",
},
theme: {
primaryColor: "#4f46e5",
locale: "en",
},
metadata: {
myUniqueIdentifier: "abc1234",
},
allowedOrigin: window.location.origin,
}),
headers: {
Authorization: apiKey,
"Content-Type": "application/json",
},
});
const data = await response.json();
document.querySelector("#create-session-result").innerHTML =
`Got embed session URL: <a href="${data.embedUrl}" target="_blank" class="url">${data.embedUrl}</a>`;
embedUrl = data.embedUrl;
document.querySelector("#step-3").open = true;
}
</script>
</details>
<details id="step-3">
<summary>Step 3: embed the session</summary>
<p>
Finally, we can embed the session in an iframe using the URL we just
received:
</p>
<button id="embed-session-button" onclick="embedSession()">
Embed session
</button>
<iframe width="100%" height="600"></iframe>
<p>
<em>Tip:</em> The Examplary UI looks even better in a modal window!
</p>
<script>
function embedSession() {
if (!embedUrl) {
return alert("Please create an embed session first.");
}
document.querySelector("iframe").src = embedUrl;
document.querySelector("#step-4").open = true;
}
</script>
</details>
<details id="step-4">
<summary>Step 4: listen for events from the embed session</summary>
<p>
You can listen for events sent from the embed session using the
postMessage API. Events are shown in the developer console on the
right.
</p>
<p>
If in your use case <code>postMessage</code> is not suitable, you can
also set a <code>returnUrl</code> that the embed session will redirect
to after completion.
</p>
<script>
window.addEventListener("message", (event) => {
// Make sure the message is coming from a trusted origin
if (event.origin !== new URL(embedUrl).origin) return;
console.log(event);
logToConsole(
`\n\n❥ Received message from embed session:\n${JSON.stringify(event.data, null, 2)}`,
);
if (event.data?.status === "completed") {
document.querySelectorAll("details").forEach((detail) => {
detail.open = false;
});
document.querySelector("#step-5").open = true;
examId = event.data.outputs.examId;
}
});
</script>
</details>
<details id="step-5">
<summary>Step 5: download QTI 3 package</summary>
<p>
After the user has completed the exam generation flow, you can fetch
the generated QTI 3 package using the API. You will need the
<code>examId</code> from the event data sent when the session is
completed.
</p>
<button id="download-qti-button" onclick="downloadQtiPackage()">
Download QTI 3 package
</button>
<script>
async function downloadQtiPackage() {
if (!examId) {
return alert("Please complete an exam generation first.");
}
const response = await fetch(
`${baseUrl}/exams/${examId}/export/qti3-zip`,
{
method: "POST",
headers: {
Authorization: apiKey,
},
},
);
const json = await response.json();
location.href = json.url;
}
</script>
</details>
</main>
<aside>
<h4>Developer console</h4>
<pre id="console-output">
This panel will show the request and response details for API calls.</pre
>
</aside>
<script type="module" src="assets/demo-helpers.js"></script>
</body>
</html>