Skip to content

Commit 6bb6e61

Browse files
authored
Merge pull request #15 from AddSearch/set-api-hostname
Set api hostname
2 parents f2f489b + 93f7ff8 commit 6bb6e61

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ client.setJWT(token);
306306
client.setThrottleTime(500);
307307
```
308308

309+
#### Set API hostname
310+
```js
311+
// Set API hostname (e.g. for dedicated environments)
312+
client.setApiHostname('api.addsearch.com');
313+
```
314+
309315
## Indexing API
310316
With the Indexing API, you can fetch, create, update, and delete single documents or
311317
batches of documents.

src/apifetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require('isomorphic-fetch');
66
/**
77
* Fetch search results of search suggestions from the Addsearch API
88
*/
9-
var executeApiFetch = function(sitekey, type, settings, cb, fuzzyRetry) {
9+
var executeApiFetch = function(apiHostname, sitekey, type, settings, cb, fuzzyRetry) {
1010

1111
const RESPONSE_BAD_REQUEST = 400;
1212
const RESPONSE_SERVER_ERROR = 500;
@@ -143,7 +143,7 @@ var executeApiFetch = function(sitekey, type, settings, cb, fuzzyRetry) {
143143

144144

145145
// Execute API call
146-
fetch('https://api.addsearch.com/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
146+
fetch('https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
147147
.then(function(response) {
148148
return response.json();
149149
}).then(function(json) {

src/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ var Settings = require('./settings');
77
var util = require('./util');
88
var throttle = require('./throttle');
99

10+
var API_HOSTNAME = 'api.addsearch.com';
1011
var API_THROTTLE_TIME_MS = 200;
1112

1213
var client = function(sitekey, privatekey) {
1314
this.sitekey = sitekey;
1415
this.privatekey = privatekey;
16+
this.apiHostname = API_HOSTNAME;
1517
this.settings = new Settings();
1618
this.sessionId = ('a-' + (Math.random() * 100000000)).substring(0, 10);
1719

@@ -53,7 +55,7 @@ var client = function(sitekey, privatekey) {
5355
if (!this.throttledSearchFetch) {
5456
this.throttledSearchFetch = throttle(API_THROTTLE_TIME_MS, executeApiFetch);
5557
}
56-
this.throttledSearchFetch(this.sitekey, 'search', this.settings.getSettings(), callback);
58+
this.throttledSearchFetch(this.apiHostname, this.sitekey, 'search', this.settings.getSettings(), callback);
5759
}
5860

5961

@@ -71,7 +73,7 @@ var client = function(sitekey, privatekey) {
7173
if (!this.throttledSuggestionsFetch) {
7274
this.throttledSuggestionsFetch = throttle(API_THROTTLE_TIME_MS, executeApiFetch);
7375
}
74-
this.throttledSuggestionsFetch(this.sitekey, 'suggest', this.settings.getSettings(), callback);
76+
this.throttledSuggestionsFetch(this.apiHostname, this.sitekey, 'suggest', this.settings.getSettings(), callback);
7577
}
7678

7779

@@ -89,37 +91,37 @@ var client = function(sitekey, privatekey) {
8991
if (!this.throttledAutocompleteFetch) {
9092
this.throttledAutocompleteFetch = throttle(API_THROTTLE_TIME_MS, executeApiFetch);
9193
}
92-
this.throttledAutocompleteFetch(this.sitekey, 'autocomplete', this.settings.getSettings(), callback);
94+
this.throttledAutocompleteFetch(this.apiHostname, this.sitekey, 'autocomplete', this.settings.getSettings(), callback);
9395
}
9496

9597

9698
/**
9799
* Indexing API functions
98100
*/
99101
this.getDocument = function(id) {
100-
return indexingapi.getDocument(this.sitekey, this.privatekey, id);
102+
return indexingapi.getDocument(this.apiHostname, this.sitekey, this.privatekey, id);
101103
}
102104

103105
this.saveDocument = function(document) {
104-
return indexingapi.saveDocument(this.sitekey, this.privatekey, document);
106+
return indexingapi.saveDocument(this.apiHostname, this.sitekey, this.privatekey, document);
105107
}
106108

107109
this.saveDocumentsBatch = function(batch) {
108110
if (!batch || !batch.documents || !Array.isArray(batch.documents)) {
109111
throw "Please provide an array of documents: {documents: []}";
110112
}
111-
return indexingapi.saveDocumentsBatch(this.sitekey, this.privatekey, batch);
113+
return indexingapi.saveDocumentsBatch(this.apiHostname, this.sitekey, this.privatekey, batch);
112114
}
113115

114116
this.deleteDocument = function(id) {
115-
return indexingapi.deleteDocument(this.sitekey, this.privatekey, id);
117+
return indexingapi.deleteDocument(this.apiHostname, this.sitekey, this.privatekey, id);
116118
}
117119

118120
this.deleteDocumentsBatch = function(batch) {
119121
if (!batch || !batch.documents || !Array.isArray(batch.documents)) {
120122
throw "Please provide an array of document ids: {documents: []}";
121123
}
122-
return indexingapi.deleteDocumentsBatch(this.sitekey, this.privatekey, batch);
124+
return indexingapi.deleteDocumentsBatch(this.apiHostname, this.sitekey, this.privatekey, batch);
123125
}
124126

125127

@@ -128,6 +130,7 @@ var client = function(sitekey, privatekey) {
128130
/**
129131
* Public functions
130132
*/
133+
this.setApiHostname = function(hostname) { this.apiHostname = hostname; }
131134
this.getSettings = function() { return this.settings.getSettings(); }
132135
this.setLanguage = function(lang) { this.settings.setLanguage(lang); }
133136
this.setCategoryFilters = function(categories) { this.settings.setCategoryFilters(categories); }
@@ -165,7 +168,7 @@ var client = function(sitekey, privatekey) {
165168
keyword: keyword,
166169
numberOfResults: data.numberOfResults
167170
};
168-
sendStats(this.sitekey, data);
171+
sendStats(this.apiHostname, this.sitekey, data);
169172
}
170173

171174
else if (type === 'click') {
@@ -176,7 +179,7 @@ var client = function(sitekey, privatekey) {
176179
docid: data.documentId,
177180
position: data.position
178181
};
179-
sendStats(this.sitekey, data);
182+
sendStats(this.apiHostname, this.sitekey, data);
180183
}
181184

182185
else {

src/indexingapi.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ require('isomorphic-fetch');
44
const util = require('./util');
55
const Promise = require('es6-promise').Promise;
66

7-
const BASE_URL = 'https://api.addsearch.com/v2/indices/';
8-
97
const getHeaders = function(sitekey, privatekey) {
108
return {
119
'Authorization': 'Basic ' + util.base64(sitekey + ':' + privatekey),
@@ -17,9 +15,10 @@ const getHeaders = function(sitekey, privatekey) {
1715
/**
1816
* Fetch document
1917
*/
20-
var getDocument = function(sitekey, privatekey, id) {
18+
var getDocument = function(apiHostname, sitekey, privatekey, id) {
2119
const promise = new Promise((resolve, reject) => {
22-
fetch(BASE_URL + sitekey + '/documents/' + id,
20+
21+
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
2322
{
2423
method: 'GET',
2524
headers: getHeaders(sitekey, privatekey)
@@ -42,13 +41,13 @@ var getDocument = function(sitekey, privatekey, id) {
4241
/**
4342
* Add document
4443
*/
45-
var saveDocument = function(sitekey, privatekey, document) {
44+
var saveDocument = function(apiHostname, sitekey, privatekey, document) {
4645

4746
// If the doc has id or url field, PUT instead of POST
4847
const isPut = document.id || document.url;
4948

5049
const promise = new Promise((resolve, reject) => {
51-
fetch(BASE_URL + sitekey + '/documents/',
50+
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/',
5251
{
5352
method: isPut ? 'PUT' : 'POST',
5453
headers: getHeaders(sitekey, privatekey),
@@ -73,10 +72,10 @@ var saveDocument = function(sitekey, privatekey, document) {
7372
/**
7473
* Batch add documents
7574
*/
76-
var saveDocumentsBatch = function(sitekey, privatekey, documents) {
75+
var saveDocumentsBatch = function(apiHostname, sitekey, privatekey, documents) {
7776

7877
const promise = new Promise((resolve, reject) => {
79-
fetch(BASE_URL + sitekey + '/documents:batch',
78+
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
8079
{
8180
method: 'PUT',
8281
headers: getHeaders(sitekey, privatekey),
@@ -101,9 +100,9 @@ var saveDocumentsBatch = function(sitekey, privatekey, documents) {
101100
/**
102101
* Delete documents
103102
*/
104-
var deleteDocument = function(sitekey, privatekey, id) {
103+
var deleteDocument = function(apiHostname, sitekey, privatekey, id) {
105104
const promise = new Promise((resolve, reject) => {
106-
fetch(BASE_URL + sitekey + '/documents/' + id,
105+
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
107106
{
108107
method: 'DELETE',
109108
headers: getHeaders(sitekey, privatekey)
@@ -127,9 +126,9 @@ var deleteDocument = function(sitekey, privatekey, id) {
127126
/**
128127
* Batch delete documents
129128
*/
130-
var deleteDocumentsBatch = function(sitekey, privatekey, batch) {
129+
var deleteDocumentsBatch = function(apiHostname, sitekey, privatekey, batch) {
131130
const promise = new Promise((resolve, reject) => {
132-
fetch(BASE_URL + sitekey + '/documents:batch',
131+
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
133132
{
134133
method: 'DELETE',
135134
headers: getHeaders(sitekey, privatekey),

src/stats.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
require('es6-promise').polyfill();
44
require('isomorphic-fetch');
55

6-
var sendStats = function(sitekey, data) {
6+
var sendStats = function(apiHostname, sitekey, data) {
77

88
// Beacon in browsers
99
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon) {
10-
navigator.sendBeacon('https://api.addsearch.com/v1/stats/' + sitekey + '/', JSON.stringify(data));
10+
navigator.sendBeacon('https://' + apiHostname + '/v1/stats/' + sitekey + '/', JSON.stringify(data));
1111
}
1212

1313
// POST in node
1414
else {
15-
fetch('https://api.addsearch.com/v1/stats/' + sitekey + '/', {
15+
fetch('https://' + apiHostname + '/v1/stats/' + sitekey + '/', {
1616
method: 'POST',
1717
headers: {
1818
'Content-Type': 'text/plain',

test/apifetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('apifetch', function() {
1010
fetchMock.get('*', { response: 200 });
1111
const expect = {response: 200};
1212

13-
apifetch('sitekey', 'search', {paging:{}, keyword: 'foo'}, (res) => {
13+
apifetch('api.addsearch.com', 'sitekey', 'search', {paging:{}, keyword: 'foo'}, (res) => {
1414
assert.deepEqual(res, expect);
1515
});
1616
});
@@ -20,7 +20,7 @@ describe('apifetch', function() {
2020
fetchMock.get('*', 'foo');
2121
const expect = 400;
2222

23-
apifetch('sitekey', 'ping', {paging:{}, keyword: 'foo'}, (res) => {
23+
apifetch('api.addsearch.com', 'sitekey', 'ping', {paging:{}, keyword: 'foo'}, (res) => {
2424
assert.equal(res.error.response, expect);
2525
});
2626
});

0 commit comments

Comments
 (0)