1+ const fetch = require ( 'node-fetch' ) ;
2+
3+ class 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+ console . log ( `${ searchServiceName } | ${ this . searchServiceName } ` ) ;
10+ }
11+
12+ _indexUrl ( ) { return `https://${ this . searchServiceName } .search.windows.net/indexes/${ this . indexName } ?api-version=${ this . apiVersion } ` ; }
13+
14+ getIndexExistsUrl ( ) { return this . _indexUrl ( ) ; }
15+ getCreateIndexUrl ( ) { return this . _indexUrl ( ) ; }
16+
17+ getSearchURL ( searchTerm ) { return `https://${ this . searchServiceName } .search.windows.net/indexes/${ this . indexName } /docs?api-version=${ this . apiVersion } &search=${ searchTerm } &searchMode=all` ; }
18+
19+
20+ request ( url , method , bodyContent = null ) {
21+ const headers = {
22+ 'content-type' : 'application/json' ,
23+ 'api-key' : this . apiKey ,
24+ 'proxy' : 'http://127.0.0.1:8888'
25+ } ;
26+ const init = bodyContent === null ?
27+ {
28+ method : method ,
29+ headers : headers
30+ }
31+ :
32+ {
33+ method : method ,
34+ headers : headers ,
35+ body : JSON . stringify ( bodyContent )
36+ } ;
37+ console . log ( url ) ;
38+ console . log ( `init = ${ init . body } ` )
39+ return fetch ( url , init ) ;
40+ }
41+
42+ throwOnHttpError ( statusCode ) {
43+ if ( statusCode >= 500 ) {
44+ console . log ( `Request returned error code ${ statusCode } ` ) ;
45+ throw new UserException ( `Failure in request. HTTP Status was ${ statusCode } ` ) ;
46+ }
47+ }
48+ }
49+
50+ module . exports = SearchServiceHelper ;
0 commit comments