|
| 1 | +const fetch = require('node-fetch'); |
| 2 | + |
1 | 3 | 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 | + } |
4 | 51 | } |
5 | 52 |
|
6 | 53 | async indexExistsAsync() { |
7 | 54 | 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); |
10 | 57 | // Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range... |
11 | 58 | const exists = response.status >= 200 && response.status < 300; |
12 | 59 | return exists; |
13 | 60 | } |
14 | 61 |
|
15 | 62 | async deleteIndexAsync() { |
16 | 63 | 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); |
20 | 67 | return this; |
21 | 68 | } |
22 | 69 |
|
23 | 70 | async createIndexAsync(definition) { |
24 | 71 | 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); |
28 | 75 | return this; |
29 | 76 | } |
30 | 77 |
|
31 | 78 | async postDataAsync(hotelsData) { |
32 | 79 | 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); |
36 | 83 | return this; |
37 | 84 | } |
38 | 85 |
|
39 | 86 | async queryAsync(searchTerm) { |
40 | 87 | 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); |
44 | 91 | return response; |
45 | 92 | } |
46 | 93 | } |
|
0 commit comments