1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 |
|
9 | var util = require('util');
|
10 | var https = require('https');
|
11 | var http = require('http');
|
12 |
|
13 | var ContextsRequest = require('./contexts_request').ContextsRequest;
|
14 | var TextRequest = require('./text_request').TextRequest;
|
15 | var VoiceRequest = require('./voice_request').VoiceRequest;
|
16 | var UserEntitiesRequest = require('./user_entities_request').UserEntitiesRequest;
|
17 |
|
18 | var version = '20150910';
|
19 | var language = 'en';
|
20 | var hostname = 'api.api.ai';
|
21 | var endpoint = '/v1/';
|
22 | var defaultSource = 'node';
|
23 |
|
24 | function 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 |
|
34 | function 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 |
|
44 | function 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 |
|
58 | exports = module.exports = createApplication;
|
59 |
|
60 | function 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 |
|
83 | Application.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 |
|
94 | Application.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 |
|
109 | Application.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 |
|
124 | Application.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 | };
|