11const fetch = require ( 'node-fetch' ) ;
22
33class 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}
0 commit comments