-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01ec51f
commit 0c6feb4
Showing
8 changed files
with
567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/*! | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/*! | ||
* @module search/document | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @type {module:common/util} | ||
* @private | ||
*/ | ||
var util = require('../common/util.js'); | ||
|
||
function Document(index, id) { | ||
this.search_ = index.search_; | ||
this.index_ = index; | ||
this.id = id; | ||
} | ||
|
||
Document.prototype.delete = function(callback) { | ||
callback = callback || util.noop; | ||
this.makeReq_('DELETE', '', null, null, callback); | ||
}; | ||
|
||
Document.prototype.getMetadata = function(callback) { | ||
var self = this; | ||
|
||
callback = callback || util.noop; | ||
|
||
this.makeReq_('GET', '/', null, null, function(err, resp) { | ||
if (err) { | ||
callback(err, null, resp); | ||
return; | ||
} | ||
|
||
if (util.is(resp.fields, 'object')) { | ||
self.fields = resp.fields; | ||
} | ||
|
||
if (util.is(resp.rank, 'number')) { | ||
self.rank = resp.rank; | ||
} | ||
|
||
callback(null, self, resp); | ||
}); | ||
}; | ||
|
||
Document.prototype.makeReq_ = function(method, path, query, body, callback) { | ||
path = '/documents/' + this.id + path; | ||
|
||
this.index_.makeReq_(method, path, query, body, callback); | ||
}; | ||
|
||
module.exports = Document; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/*! | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/*! | ||
* @module search/index | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var extend = require('extend'); | ||
|
||
/** | ||
* @type {module:search/document} | ||
* @private | ||
*/ | ||
var Document = require('./document.js'); | ||
|
||
/** | ||
* @type {module:common/util} | ||
* @private | ||
*/ | ||
var util = require('../common/util.js'); | ||
|
||
function Index(search, id) { | ||
this.search_ = search; | ||
this.id = id; | ||
} | ||
|
||
Index.prototype.createDocument = function(document, callback) { | ||
var self = this; | ||
|
||
this.makeReq_('POST', '/documents', null, document, function(err, resp) { | ||
if (err) { | ||
callback(err, null, resp); | ||
return; | ||
} | ||
|
||
var document = self.documentFromApiResp_(resp); | ||
|
||
callback(null, document, resp); | ||
}); | ||
}; | ||
|
||
Index.prototype.document = function(id) { | ||
return new Document(this, id); | ||
}; | ||
|
||
Index.prototype.getDocuments = function(query, callback) { | ||
var self = this; | ||
|
||
if (util.is(query, 'function')) { | ||
callback = query; | ||
query = {}; | ||
} | ||
|
||
this.makeReq_('GET', '/documents', query, null, function(err, resp) { | ||
if (err) { | ||
callback(err, null, null, resp); | ||
return; | ||
} | ||
|
||
var nextQuery = null; | ||
|
||
if (resp.nextPageToken) { | ||
nextQuery = extend({}, query, { | ||
pageToken: resp.nextPageToken | ||
}); | ||
} | ||
|
||
var documents = (resp.documents || []) | ||
.map(self.documentFromApiResp_.bind(self)); | ||
|
||
callback(null, documents, nextQuery, resp); | ||
}); | ||
}; | ||
|
||
Index.prototype.search = function(query, callback) { | ||
var self = this; | ||
|
||
if (util.is(query, 'string')) { | ||
query = { | ||
query: query | ||
}; | ||
} | ||
|
||
this.makeReq_('GET', '/search', query, null, function(err, resp) { | ||
if (err) { | ||
callback(err, null, null, resp); | ||
return; | ||
} | ||
|
||
var nextQuery = null; | ||
|
||
if (resp.nextPageToken) { | ||
nextQuery = extend({}, query, { | ||
pageToken: resp.nextPageToken | ||
}); | ||
} | ||
|
||
var documents = (resp.results || []) | ||
.map(self.documentFromApiResp_.bind(self)); | ||
|
||
callback(null, documents, nextQuery, resp); | ||
}); | ||
}; | ||
|
||
Index.prototype.documentFromApiResp_ = function(apiResp) { | ||
var document = this.document(apiResp.docId); | ||
|
||
if (util.is(apiResp.fields, 'object')) { | ||
document.fields = apiResp.fields; | ||
} | ||
|
||
if (util.is(apiResp.rank, 'number')) { | ||
document.rank = apiResp.rank; | ||
} | ||
|
||
return document; | ||
}; | ||
|
||
Index.prototype.makeReq_ = function(method, path, query, body, callback) { | ||
path = '/indexes/' + this.id + path; | ||
|
||
this.search_.makeReq_(method, path, query, body, callback); | ||
}; | ||
|
||
module.exports = Index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/*! | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/*! | ||
* @module search | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var extend = require('extend'); | ||
|
||
/** | ||
* @type {module:search/index} | ||
* @private | ||
*/ | ||
var Index = require('./index-class.js'); | ||
|
||
/** | ||
* @type {module:common/util} | ||
* @private | ||
*/ | ||
var util = require('../common/util.js'); | ||
|
||
/** | ||
* @const {string} Base URL for the Search API. | ||
* @private | ||
*/ | ||
var SEARCH_BASE_URL = 'https://cloudsearch.googleapis.com/v1/'; | ||
|
||
/** | ||
* @const {array} Required scopes for the Search API. | ||
* @private | ||
*/ | ||
var SCOPES = [ | ||
'https://www.googleapis.com/auth/cloud-platform', | ||
'https://www.googleapis.com/auth/cloudsearch', | ||
'https://www.googleapis.com/auth/userinfo.email' | ||
]; | ||
|
||
function Search(options) { | ||
options = options || {}; | ||
|
||
if (!options.projectId) { | ||
throw util.missingProjectIdError; | ||
} | ||
|
||
this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({ | ||
credentials: options.credentials, | ||
keyFile: options.keyFilename, | ||
scopes: SCOPES, | ||
email: options.email | ||
}); | ||
|
||
this.projectId = options.projectId; | ||
this.projectName = 'projects/' + this.projectId; | ||
} | ||
|
||
Search.prototype.getIndexes = function(query, callback) { | ||
var self = this; | ||
|
||
if (util.is(query, 'function')) { | ||
callback = query; | ||
query = {}; | ||
} | ||
|
||
this.makeReq_('GET', '/indexes', query, null, function(err, resp) { | ||
if (err) { | ||
callback(err, null, null, resp); | ||
return; | ||
} | ||
|
||
var nextQuery = null; | ||
|
||
if (resp.nextPageToken) { | ||
nextQuery = extend({}, query, { | ||
pageToken: resp.nextPageToken | ||
}); | ||
} | ||
|
||
var indexes = (resp.indexes || []).map(function(indexObject) { | ||
var index = self.index(indexObject.indexId); | ||
|
||
if (util.is(resp.indexedField, 'object')) { | ||
index.fields = resp.indexedField; | ||
} | ||
|
||
return index; | ||
}); | ||
|
||
callback(null, indexes, nextQuery, resp); | ||
}); | ||
}; | ||
|
||
Search.prototype.index = function(id) { | ||
return new Index(this, id); | ||
}; | ||
|
||
Search.prototype.makeReq_ = function(method, path, query, body, callback) { | ||
var reqOpts = { | ||
method: method, | ||
qs: query, | ||
uri: util.format('{base}projects/{projectId}{path}', { | ||
base: SEARCH_BASE_URL, | ||
projectId: this.projectId, | ||
path: path | ||
}) | ||
}; | ||
|
||
if (body) { | ||
reqOpts.json = body; | ||
} | ||
|
||
this.makeAuthorizedRequest_(reqOpts, callback); | ||
}; | ||
|
||
module.exports = Search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.