Skip to content

Commit 4e55e29

Browse files
committed
Asyncified
1 parent fd5b6a3 commit 4e55e29

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

quickstart/AzureSearchClient.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@ class AzureSearchClient {
55
this.searchServiceHelper = searchServiceHelper;
66
}
77

8-
indexExists() {
8+
async indexExistsAsync() {
99
console.log("\n Checking if index exists...");
1010
const endpoint = this.searchServiceHelper.getIndexExistsUrl();
11-
this.searchServiceHelper.request(endpoint, "GET", null)
12-
.then((response) => {
13-
return response.status == 200;
14-
});
11+
const response = await this.searchServiceHelper.request(endpoint, "GET", null);
12+
console.log(`The response was ${response}`);
13+
// Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range...
14+
const exists = response.status >= 200 && response.status < 300;
15+
console.log(`Index exists? ${exists}`);
16+
return exists;
1517
}
1618

17-
createIndex(definition) {
19+
async deleteIndexAsync() {
20+
console.log("\n Deleting existing index...");
21+
const endpoint = this.searchServiceHelper.getIndexExistsUrl();
22+
const response = await this.searchServiceHelper.request(endpoint, "DELETE", null);
23+
this.searchServiceHelper.throwOnHttpError(response.status);
24+
return this;
25+
}
26+
27+
async createIndexAsync(definition) {
1828
console.log("\n Creating index...");
1929
const endpoint = this.searchServiceHelper.getCreateIndexUrl();
2030

21-
this.searchServiceHelper.request(endpoint, "PUT", definition)
22-
.then((response) => {
23-
console.log(response);
24-
this.searchServiceHelper.throwOnHttpError(response.status);
25-
});
26-
31+
const response = await this.searchServiceHelper.request(endpoint, "PUT", definition);
32+
console.log(response);
33+
this.searchServiceHelper.throwOnHttpError(response.status);
34+
return this;
2735
}
2836
}
2937

quickstart/SearchServiceHelper.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class SearchServiceHelper {
66
this.apiKey = apiKey;
77
this.indexName = indexName;
88
this.apiVersion = '2019-05-06';
9-
console.log(`${searchServiceName} | ${this.searchServiceName}`);
109
}
1110

1211
_indexUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}?api-version=${this.apiVersion}`; }
@@ -17,13 +16,12 @@ class SearchServiceHelper {
1716
getSearchURL(searchTerm) { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs?api-version=${this.apiVersion}&search=${searchTerm}&searchMode=all`; }
1817

1918

20-
request(url, method, bodyContent = null) {
19+
request(url, method, bodyJson = null) {
2120
const headers = {
2221
'content-type' : 'application/json',
23-
'api-key' : this.apiKey,
24-
'proxy' : 'http://127.0.0.1:8888'
22+
'api-key' : this.apiKey
2523
};
26-
const init = bodyContent === null ?
24+
const init = bodyJson === null ?
2725
{
2826
method : method,
2927
headers : headers
@@ -32,10 +30,9 @@ class SearchServiceHelper {
3230
{
3331
method : method,
3432
headers : headers,
35-
body : JSON.stringify(bodyContent)
33+
body : JSON.stringify(bodyJson)
3634
};
37-
console.log(url);
38-
console.log(`init = ${init.body}`)
35+
console.log(`\n${method} ${url}`);
3936
return fetch(url, init);
4037
}
4138

quickstart/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ function getAzureConfiguration() {
1212
return config;
1313
}
1414

15-
const run = () => {
15+
const run = async () => {
1616
try {
1717
const cfg = getAzureConfiguration();
1818
const helper = new SearchServiceHelper(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
1919
const client = new AzureSearchClient(helper);
20-
if (client.indexExists()) {
21-
client.deleteIndex();
22-
}
23-
20+
21+
const exists = await client.indexExistsAsync();
22+
await exists ? client.deleteIndexAsync() : Promise.resolve();
2423
const indexDefinition = require('./hotels_quickstart_index.json');
25-
client.createIndex(indexDefinition);
24+
await client.createIndexAsync(indexDefinition);
25+
queries.forEach(async (query) => {
26+
const result = await client.queryAsync(q);
27+
console.log(`Query: ${q} \n ${result}`);
28+
})
2629
} catch (x) {
2730
console.log(x);
2831
}

0 commit comments

Comments
 (0)