Skip to content

Commit 7e7e117

Browse files
committed
All code works, but using sleep fn. Add polling for status?
1 parent d942d1e commit 7e7e117

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

quickstart/AzureSearchClient.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class AzureSearchClient {
2-
3-
42
constructor (searchServiceHelper) {
53
this.searchServiceHelper = searchServiceHelper;
64
}
@@ -11,7 +9,6 @@ class AzureSearchClient {
119
const response = await this.searchServiceHelper.request(endpoint, "GET", null);
1210
// Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range...
1311
const exists = response.status >= 200 && response.status < 300;
14-
this.searchServiceHelper.throwOnHttpError(response);
1512
return exists;
1613
}
1714

@@ -26,7 +23,6 @@ class AzureSearchClient {
2623
async createIndexAsync(definition) {
2724
console.log("\n Creating index...");
2825
const endpoint = this.searchServiceHelper.getCreateIndexUrl();
29-
3026
const response = await this.searchServiceHelper.request(endpoint, "PUT", definition);
3127
this.searchServiceHelper.throwOnHttpError(response);
3228
return this;
@@ -35,14 +31,13 @@ class AzureSearchClient {
3531
async loadDataAsync(hotelsData) {
3632
console.log("\n Adding hotel data...");
3733
const endpoint = this.searchServiceHelper.getPostDataUrl();
38-
39-
console.log(JSON.stringify(hotelsData));
4034
const response = await this.searchServiceHelper.request(endpoint,"POST", hotelsData);
4135
this.searchServiceHelper.throwOnHttpError(response);
4236
return this;
4337
}
4438

4539
async queryAsync(searchTerm) {
40+
console.log("\n Querying...")
4641
const endpoint = this.searchServiceHelper.getSearchUrl(searchTerm);
4742
const response = await this.searchServiceHelper.request(endpoint, "GET");
4843
this.searchServiceHelper.throwOnHttpError(response);

quickstart/SearchServiceHelper.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class SearchServiceHelper {
1919

2020

2121
request(url, method, bodyJson = null) {
22+
console.log(`\n${method} ${url}`);
23+
2224
const headers = {
2325
'content-type' : 'application/json',
2426
'api-key' : this.apiKey
@@ -34,16 +36,15 @@ class SearchServiceHelper {
3436
headers : headers,
3537
body : JSON.stringify(bodyJson)
3638
};
37-
console.log(`\n${method} ${url}`);
3839
return fetch(url, init);
3940
}
4041

4142
throwOnHttpError(response) {
4243
const statusCode = response.status;
4344
console.log(`Response Status: ${statusCode}`);
4445
if (statusCode >= 300){
45-
console.log(`Request returned error code ${JSON.stringify(response)}`);
46-
throw new UserException(`Failure in request. HTTP Status was ${statusCode}`);
46+
console.log(`Request failed: ${JSON.stringify(response, null, 4)}`);
47+
throw new Exception(`Failure in request. HTTP Status was ${statusCode}`);
4748
}
4849
}
4950
}

quickstart/index.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,42 @@ const hotelData = { value : [
2424
require('./data/hotel4.json')
2525
]}
2626

27+
28+
function sleep(ms)
29+
{
30+
return(
31+
new Promise(function(resolve, reject)
32+
{
33+
setTimeout(function() { resolve(); }, ms);
34+
})
35+
);
36+
}
37+
async function doQueries(client) {
38+
return Promise.all(
39+
queries.map(async query => {
40+
const result = await client.queryAsync(query);
41+
const body = await result.json();
42+
const str = JSON.stringify(body, null, 4);
43+
console.log(`Query: ${query} \n ${str}`);
44+
})
45+
);
46+
}
47+
2748
const run = async () => {
2849
try {
2950
const cfg = getAzureConfiguration();
3051
const helper = new SearchServiceHelper(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
3152
const client = new AzureSearchClient(helper);
3253

33-
//const exists = await client.indexExistsAsync();
34-
//await exists ? client.deleteIndexAsync() : Promise.resolve();
35-
//const indexDefinition = require('./hotels_quickstart_index.json');
36-
//await client.createIndexAsync(indexDefinition);
37-
//await client.loadDataAsync(hotelData);
38-
// var indexComplete = false
39-
// do {
40-
// indexComplete = await client.indexCompletedAsync();
41-
// }while(! indexComplete)
42-
queries.forEach(async (query) => {
43-
const result = await client.queryAsync(query);
44-
const body = await result.json();
45-
const str = JSON.stringify(body, null, 4);
46-
console.log(`Query: ${query} \n ${str}`);
47-
});
48-
console.log("Finished.");
54+
const exists = await client.indexExistsAsync();
55+
await exists ? client.deleteIndexAsync() : Promise.resolve();
56+
await sleep(2000);
57+
const indexDefinition = require('./hotels_quickstart_index.json');
58+
await client.createIndexAsync(indexDefinition);
59+
await sleep(2000);
60+
await client.loadDataAsync(hotelData);
61+
await sleep(5000);
62+
await doQueries(client);
4963
} catch (x) {
5064
console.log(x);
5165
}

0 commit comments

Comments
 (0)