UNPKG

3.4 kBJavaScriptView Raw
1/*!
2 * apiai
3 * Copyright(c) 2015 http://api.ai/
4 * Apache 2.0 Licensed
5 */
6
7'use strict';
8
9var util = require('util');
10var https = require('https');
11var http = require('http');
12
13var ContextsRequest = require('./contexts_request').ContextsRequest;
14var TextRequest = require('./text_request').TextRequest;
15var VoiceRequest = require('./voice_request').VoiceRequest;
16var UserEntitiesRequest = require('./user_entities_request').UserEntitiesRequest;
17
18var version = '20150910';
19var language = 'en';
20var hostname = 'api.api.ai';
21var endpoint = '/v1/';
22var defaultSource = 'node';
23
24function createApplicationDeprecated(clientAccessToken, subscriptionKey, options) {
25 var options = options || {};
26
27 if (!clientAccessToken) {
28 throw new Error('\'clientAccessToken\' cannot be empty.');
29 }
30
31 return new Application(clientAccessToken, options);
32}
33
34function createApplicationNew(clientAccessToken, options) {
35 var options = options || {};
36
37 if (!clientAccessToken) {
38 throw new Error('\'clientAccessToken\' cannot be empty.');
39 }
40
41 return new Application(clientAccessToken, options);
42}
43
44function createApplication(args) {
45 if (arguments.length > 1) {
46 if (typeof arguments[1] == "string") {
47 return createApplicationDeprecated.apply(this, arguments);
48 } else if (typeof arguments[1] == "object") {
49 return createApplicationNew.apply(this, arguments);
50 } else {
51 throw new Error('Wrong parameters of initialization.');
52 }
53 } else {
54 return createApplicationNew.apply(this, arguments);
55 }
56}
57
58exports = module.exports = createApplication;
59
60function Application (clientAccessToken, options) {
61 var self = this;
62
63 self.language = options.language || language;
64
65 self.clientAccessToken = clientAccessToken;
66
67 self.hostname = options.hostname || hostname;
68 self.version = options.version || version;
69
70 self.endpoint = options.endpoint || endpoint;
71 self.requestSource = options.requestSource || defaultSource;
72
73 if ('secure' in options) {
74 self.secure = options.secure;
75 } else {
76 self.secure = true;
77 }
78
79 var _http = self.secure ? https : http;
80 self._agent = new _http.Agent({ keepAlive: true });
81};
82
83Application.prototype.contextsRequest = function(contexts, options) {
84 var self = this;
85 var opt = options || {};
86
87 if (!('endpoint' in opt)) {
88 opt.endpoint = self.endpoint;
89 }
90
91 return new ContextsRequest(self, contexts, opt);
92};
93
94Application.prototype.textRequest = function(query, options) {
95 var self = this;
96 var opt = options || {};
97
98 if (!('endpoint' in opt)) {
99 opt.endpoint = self.endpoint;
100 }
101
102 if (!('version' in opt)) {
103 opt.version = self.version;
104 }
105
106 return new TextRequest(self, query, opt);
107};
108
109Application.prototype.voiceRequest = function(options) {
110 var self = this;
111 var opt = options || {};
112
113 if (!('endpoint' in opt)) {
114 opt.endpoint = self.endpoint;
115 }
116
117 if (!('version' in opt)) {
118 opt.version = self.version;
119 }
120
121 return new VoiceRequest(self, opt);
122};
123
124Application.prototype.userEntitiesRequest = function(user_entities, options) {
125 var self = this;
126 var opt = options || {};
127
128 if (!('endpoint' in opt)) {
129 opt.endpoint = self.endpoint;
130 }
131
132 return new UserEntitiesRequest(self, user_entities, opt);
133};