Skip to content

Commit 6f5f845

Browse files
committed
Moved helper functionality into Client
Simplifies project structure. Not enough functionality to justify explaining extra file. (?)
1 parent 9cb043d commit 6f5f845

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

quickstart/AzureSearchClient.js

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,93 @@
1+
const fetch = require('node-fetch');
2+
13
class AzureSearchClient {
2-
constructor (searchServiceHelper) {
3-
this.searchServiceHelper = searchServiceHelper;
4+
constructor(searchServiceName, apiKey, indexName) {
5+
this.searchServiceName = searchServiceName;
6+
this.apiKey = apiKey;
7+
this.indexName = indexName;
8+
this.apiVersion = '2019-05-06';
9+
}
10+
11+
getIndexUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}?api-version=${this.apiVersion}`; }
12+
13+
getPostDataUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs/index?api-version=${this.apiVersion}`; }
14+
15+
getSearchUrl(searchTerm) { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs?api-version=${this.apiVersion}&search=${searchTerm}&searchMode=all`; }
16+
17+
request(url, method, bodyJson = null) {
18+
// Uncomment the following for request details:
19+
/*
20+
console.log(`\n${method} ${url}`);
21+
if (bodyJson !== null) {
22+
console.log(`\ncontent: ${JSON.stringify(bodyJson, null, 4)}`);
23+
}
24+
*/
25+
26+
const headers = {
27+
'content-type' : 'application/json',
28+
'api-key' : this.apiKey
29+
};
30+
const init = bodyJson === null ?
31+
{
32+
method : method,
33+
headers : headers
34+
}
35+
:
36+
{
37+
method : method,
38+
headers : headers,
39+
body : JSON.stringify(bodyJson)
40+
};
41+
return fetch(url, init);
42+
}
43+
44+
throwOnHttpError(response) {
45+
const statusCode = response.status;
46+
console.log(`Response Status: ${statusCode}`);
47+
if (statusCode >= 300){
48+
console.log(`Request failed: ${JSON.stringify(response, null, 4)}`);
49+
throw new Exception(`Failure in request. HTTP Status was ${statusCode}`);
50+
}
451
}
552

653
async indexExistsAsync() {
754
console.log("\n Checking if index exists...");
8-
const endpoint = this.searchServiceHelper.getIndexUrl();
9-
const response = await this.searchServiceHelper.request(endpoint, "GET", null);
55+
const endpoint = this.getIndexUrl();
56+
const response = await this.request(endpoint, "GET", null);
1057
// Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range...
1158
const exists = response.status >= 200 && response.status < 300;
1259
return exists;
1360
}
1461

1562
async deleteIndexAsync() {
1663
console.log("\n Deleting existing index...");
17-
const endpoint = this.searchServiceHelper.getIndexUrl();
18-
const response = await this.searchServiceHelper.request(endpoint, "DELETE");
19-
this.searchServiceHelper.throwOnHttpError(response);
64+
const endpoint = this.getIndexUrl();
65+
const response = await this.request(endpoint, "DELETE");
66+
this.throwOnHttpError(response);
2067
return this;
2168
}
2269

2370
async createIndexAsync(definition) {
2471
console.log("\n Creating index...");
25-
const endpoint = this.searchServiceHelper.getIndexUrl();
26-
const response = await this.searchServiceHelper.request(endpoint, "PUT", definition);
27-
this.searchServiceHelper.throwOnHttpError(response);
72+
const endpoint = this.getIndexUrl();
73+
const response = await this.request(endpoint, "PUT", definition);
74+
this.throwOnHttpError(response);
2875
return this;
2976
}
3077

3178
async postDataAsync(hotelsData) {
3279
console.log("\n Adding hotel data...");
33-
const endpoint = this.searchServiceHelper.getPostDataUrl();
34-
const response = await this.searchServiceHelper.request(endpoint,"POST", hotelsData);
35-
this.searchServiceHelper.throwOnHttpError(response);
80+
const endpoint = this.getPostDataUrl();
81+
const response = await this.request(endpoint,"POST", hotelsData);
82+
this.throwOnHttpError(response);
3683
return this;
3784
}
3885

3986
async queryAsync(searchTerm) {
4087
console.log("\n Querying...")
41-
const endpoint = this.searchServiceHelper.getSearchUrl(searchTerm);
42-
const response = await this.searchServiceHelper.request(endpoint, "GET");
43-
this.searchServiceHelper.throwOnHttpError(response);
88+
const endpoint = this.getSearchUrl(searchTerm);
89+
const response = await this.request(endpoint, "GET");
90+
this.throwOnHttpError(response);
4491
return response;
4592
}
4693
}

quickstart/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env/node
22

33
const nconf = require('nconf');
4-
const SearchServiceHelper = require('./SearchServiceHelper.js');
54
const AzureSearchClient = require('./AzureSearchClient.js');
65

76
function getAzureConfiguration() {
@@ -48,16 +47,18 @@ async function doQueries(client) {
4847
const run = async () => {
4948
try {
5049
const cfg = getAzureConfiguration();
51-
const helper = new SearchServiceHelper(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
52-
const client = new AzureSearchClient(helper);
50+
const client = new AzureSearchClient(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
5351

5452
const exists = await client.indexExistsAsync();
5553
await exists ? client.deleteIndexAsync() : Promise.resolve();
54+
// Deleting index can take a few seconds
5655
await sleep(2000);
5756
const indexDefinition = require('./hotels_quickstart_index.json');
5857
await client.createIndexAsync(indexDefinition);
58+
// Index availability can take a few seconds
5959
await sleep(2000);
6060
await client.postDataAsync(hotelData);
61+
// Data availability can take a few seconds
6162
await sleep(5000);
6263
await doQueries(client);
6364
} catch (x) {

quickstart/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"url": "."
1212
},
1313
"dependencies": {
14+
"eslint-config-prettier": "^6.0.0",
1415
"nconf": "^0.9.0",
1516
"node-fetch": "^2.6.0"
1617
},
@@ -19,6 +20,7 @@
1920
"devDependencies": {
2021
"eslint": "^5.3.0",
2122
"eslint-config-airbnb-base": "^13.2.0",
22-
"eslint-plugin-import": "^2.17.2"
23+
"eslint-plugin-import": "^2.17.2",
24+
"prettier": "1.18.2"
2325
}
2426
}

0 commit comments

Comments
 (0)