Skip to content

Commit

Permalink
Commonize codes to setup documents API and search API
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Dec 17, 2012
1 parent 990e9db commit 50e986a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bin/gcs-post-sdf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ client.assertDomainExists(function() {
else
batches = JSON.parse(batches);

client.setupDocumentsAPI(function(documentsAPI) {
client.setupAPI('doc', function(documentsAPI) {
documentsAPI.DocumentsBatch(
{
Docs: batches
Expand Down
49 changes: 33 additions & 16 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Client(options) {
this.host = options.host;
this.port = options.port;
this.docEndpoint = options.docEndpoint;
this.searchEndpoint = options.searchEndpoint;
this.isACS = options.isACS;

this.accessKeyId = options.accessKeyId;
Expand All @@ -40,42 +41,61 @@ Client.prototype = {
return this._configurationAPI;
},

setupDocumentsAPI: function(callback) {
if (this.documentsAPI)
return callback(this.documentsAPI);
get APIDefinitions() {
return {
'doc': {
endPoint: this.docEndPoint,
elementName: 'DocService',
klass: DocumentService
},
'search': {
endPoint: this.searchEndPoint,
elementName: 'SearchService',
klass: SearchService
}
};
},
setupAPI: function(type, callback) {
this.cachedAPI = this.cachedAPI || {};
if (type in this.cachedAPI)
return this.cacnedAPI[type];

var self = this;
if (this.docEndpoint) {
var parsedEndpoint = this.docEndpoint.match(/^(doc-([^-]+)-([^\.]+)[^:]+)(?::(\d+))?$/);
this._documentsAPI = this.createDocumentService({
var definition = this.APIDefinitions[type];
if (definition.endPoint) {
var parsedEndpoint = definition.endPoint.match(/^([^-]+-([^-]+)-([^\.]+)[^:]+)(?::(\d+))?$/);
var api = this.createService({
domainName: parsedEndpoint[2],
domainId: parsedEndpoint[3],
host: parsedEndpoint[1],
port: parsedEndpoint[4] || 80
port: parsedEndpoint[4] || 80,
klass: definition.klass
});
return callback(this.documentsAPI);
this.cacnedAPI[type] = api;
return callback(api);
}

this.getDomainStatus(function(error, domain) {
if (error)
self.raiseFatalError(error);

try {
var endpoint = self.arnToEndpoint(domain.DocService.Arn).split(':');
self._documentsAPI = self.createDocumentService({
var endpoint = self.arnToEndpoint(domain[definition.elementName].Arn).split(':');
var api = self.createService({
domainName: domain.DomainName,
domainId: domain.DomainId.replace(new RegExp('/' + domain.DomainName + '$'), ''),
host: endpoint[0],
port: endpoint[1] || 80
});
callback(self.documentsAPI);
self.cacnedAPI[type] = api;
callback(api);
} catch(error) {
self.raiseFatalError(error);
}
});
},
createDocumentService: function(options) {
var service = new DocumentService({
createService: function(options) {
var service = new options.klass({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey,
domainName: options.domainName,
Expand Down Expand Up @@ -110,9 +130,6 @@ Client.prototype = {
var notEscapedParts = string.replace(escapedPartsMatcher, '');
return notEscapedParts.length + (escapedParts ? escapedParts.length : 0);
},
get documentsAPI() {
return this._documentsAPI;
},

raiseFatalError: function(error) {
if (typeof error != 'string') {
Expand Down

0 comments on commit 50e986a

Please sign in to comment.