Skip to content

Commit

Permalink
Merge pull request #111 from Genymobile/dev/update-example-to-fetch-j…
Browse files Browse the repository at this point in the history
…wt-token-for-specific-instance

Update the example to use the latest apis for fetching recipes and accessing instances.
  • Loading branch information
jparez authored Oct 3, 2024
2 parents 0958bfe + 03e4be1 commit 4d8bc97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
5 changes: 2 additions & 3 deletions example/geny-window.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ <h3>Connect to an existing instance</h3>
<datalist id="instances-history"></datalist>
<div class="note" id="input-note">
<ul>
<li><strong>Instance uuid:</strong> the uuid of a launched device.</li>
<li><strong>Instance uuid:</strong> the uuid of a launched device. <strong>(Genymotion SaaS only)</strong></li>
<li>
<strong>WebSocket address:</strong> the WebSocket address (starting with "wss://") of a
launched device.
<strong>WebSocket address:</strong> the WebSocket address ("<i>wss://public ip address</i>") of the device. <strong>(Genymotion Device Image only)</strong>
</li>
</ul>
</div>
Expand Down
41 changes: 17 additions & 24 deletions example/geny-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ const getApiToken = async () => {
};

// get jwt token
const getJWTToken = async () => {
const getJWTToken = async (instanceUuid) => {
try {
const response = await fetch(baseUrlToFetch + `/v1/instances/access-token`, {
...requestInit,
method: 'POST',
body: JSON.stringify({
instance_uuid: instanceUuid,
}),
});

if (response.status !== 200) {
Expand Down Expand Up @@ -95,7 +98,7 @@ const startInstance = async (recipeUuid) => {

instanceUuid = recipe.uuid;

await getJWTToken();
await getJWTToken(instanceUuid);

return recipe;
};
Expand Down Expand Up @@ -143,14 +146,14 @@ const stopInstance = async () => {
// Fetch recipe and add them to the select
const fetchRecipes = async () => {
try {
const response = await fetch(baseUrlToFetch + `/v1/recipes?limit=1000`, {
const response = await fetch(baseUrlToFetch + `/v3/recipes/?arch=x86_64&arch=x86&arch=arm64&limit=1000`, {
...requestInit,
method: 'GET',
});
const recipes = await response.json();
const recipesResult = await response.json();

if (response.status !== 200) {
alert(recipes.message);
alert(recipesResult.message);
return;
}

Expand All @@ -163,30 +166,20 @@ const fetchRecipes = async () => {
placeholderOption.disabled = true;
placeholderOption.selected = true;
selectElement.add(placeholderOption, selectElement.firstChild);

for (const recipeType in recipes) {
addOptionsToGroup(recipeType, recipes[recipeType]);
const recipes = recipesResult.results;

for (let i = 0; i < recipes.length; i++) {
const recipe = recipes[i];
const optionElement = document.createElement('option');
optionElement.value = recipe.uuid;
optionElement.textContent = recipe.name;
selectElement.appendChild(optionElement);
}
} catch (error) {
alert(error);
}
};

const addOptionsToGroup = (recipeType, recipes) => {
const selectElement = document.querySelector('#listRecipes');

const optgroup = document.createElement('optgroup');
optgroup.label = recipeType;

recipes.forEach((recipe) => {
const optionElement = document.createElement('option');
optionElement.value = recipe.uuid;
optionElement.textContent = recipe.name;
optgroup.appendChild(optionElement);
});

selectElement.appendChild(optgroup);
};

// connect to an existing instance through an instance Uuid or an instance ws address
const connectInstance = async (wsAddress) => {
Expand All @@ -202,7 +195,7 @@ const connectInstance = async (wsAddress) => {
});
const instance = await response.json();
instanceUuid = instance.uuid;
await getJWTToken();
await getJWTToken(instanceUuid);
initPlayer(instance.publicWebrtcUrl);
};

Expand Down

0 comments on commit 4d8bc97

Please sign in to comment.