Skip to content

Commit d97b669

Browse files
committed
Added use of queryKey, switched to static fns
1 parent 7449306 commit d97b669

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

quickstart/AzureSearchClient.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const fetch = require('node-fetch');
22

33
class AzureSearchClient {
4-
constructor(searchServiceName, apiKey, indexName) {
4+
constructor(searchServiceName, adminKey, queryKey, indexName) {
55
this.searchServiceName = searchServiceName;
6-
this.apiKey = apiKey;
6+
this.adminKey = adminKey;
7+
// The query key is used for read-only requests and so can be distributed with less risk of abuse.
8+
this.queryKey = queryKey;
79
this.indexName = indexName;
810
this.apiVersion = '2019-05-06';
911
}
@@ -43,7 +45,6 @@ class AzureSearchClient {
4345

4446
static throwOnHttpError(response) {
4547
const statusCode = response.status;
46-
console.log(`Response Status: ${statusCode}`);
4748
if (statusCode >= 300){
4849
console.log(`Request failed: ${JSON.stringify(response, null, 4)}`);
4950
throw new Error(`Failure in request. HTTP Status was ${statusCode}`);
@@ -53,7 +54,7 @@ class AzureSearchClient {
5354
async indexExistsAsync() {
5455
console.log("\n Checking if index exists...");
5556
const endpoint = this.getIndexUrl();
56-
const response = await this.request(endpoint, "GET", this.apiKey);
57+
const response = await AzureSearchClient.request(endpoint, "GET", this.queryKey);
5758
// Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range...
5859
const exists = response.status >= 200 && response.status < 300;
5960
return exists;
@@ -62,32 +63,32 @@ class AzureSearchClient {
6263
async deleteIndexAsync() {
6364
console.log("\n Deleting existing index...");
6465
const endpoint = this.getIndexUrl();
65-
const response = await this.request(endpoint, "DELETE", this.apiKey);
66-
this.throwOnHttpError(response);
66+
const response = await AzureSearchClient.request(endpoint, "DELETE", this.adminKey);
67+
AzureSearchClient.throwOnHttpError(response);
6768
return this;
6869
}
6970

7071
async createIndexAsync(definition) {
7172
console.log("\n Creating index...");
7273
const endpoint = this.getIndexUrl();
73-
const response = await this.request(endpoint, "PUT", this.apiKey, definition);
74-
this.throwOnHttpError(response);
74+
const response = await AzureSearchClient.request(endpoint, "PUT", this.adminKey, definition);
75+
AzureSearchClient.throwOnHttpError(response);
7576
return this;
7677
}
7778

7879
async postDataAsync(hotelsData) {
7980
console.log("\n Adding hotel data...");
8081
const endpoint = this.getPostDataUrl();
81-
const response = await this.request(endpoint,"POST", this.apiKey, hotelsData);
82-
this.throwOnHttpError(response);
82+
const response = await AzureSearchClient.request(endpoint,"POST", this.adminKey, hotelsData);
83+
AzureSearchClient.throwOnHttpError(response);
8384
return this;
8485
}
8586

8687
async queryAsync(searchTerm) {
8788
console.log("\n Querying...")
8889
const endpoint = this.getSearchUrl(searchTerm);
89-
const response = await this.request(endpoint, "GET", this.apiKey);
90-
this.throwOnHttpError(response);
90+
const response = await AzureSearchClient.request(endpoint, "GET", this.queryKey);
91+
AzureSearchClient.throwOnHttpError(response);
9192
return response;
9293
}
9394
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"serviceName" : "[SEARCH_SERVICE_NAME]",
3-
"apiKey" : "[SEARCH_SERVICE_API_KEY]"
3+
"adminKey" : "[SEARCH_SERVICE_ADMIN_KEY]",
4+
"queryKey" : "[SEARCH_SERVICE_QUERY_KEY"]""
45
}

quickstart/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const AzureSearchClient = require('./AzureSearchClient.js');
55

66
function getAzureConfiguration() {
77
const config = nconf.file({ file: 'azure_search_config.json' });
8-
if (config.get('serviceName') == '[SEARCH_SERVICE_NAME' || config.get('apiKey') == '[SEARCH_SERVICE_API_KEY]') {
8+
if (config.get('serviceName') == '[SEARCH_SERVICE_NAME' || config.get('adminKey') == '[SEARCH_SERVICE_API_KEY]') {
99
throw new Error("You have not set the values in your azure_search_config.json file. Please change them to match your search service's values.");
1010
}
1111
return config;
@@ -47,7 +47,7 @@ async function doQueries(client) {
4747
const run = async () => {
4848
try {
4949
const cfg = getAzureConfiguration();
50-
const client = new AzureSearchClient(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
50+
const client = new AzureSearchClient(cfg.get("serviceName"), cfg.get("adminKey"), cfg.get("queryKey"), "hotels");
5151

5252
const exists = await client.indexExistsAsync();
5353
await exists ? client.deleteIndexAsync() : Promise.resolve();

0 commit comments

Comments
 (0)