Skip to content

Commit 81b32ee

Browse files
author
Chris Park
committed
Merge branch 'SamHausmann-RCB-330' into develop - multiPart add, base64 remove
2 parents f7f261e + 1e985d5 commit 81b32ee

15 files changed

+150
-35
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ api.rosette(endpoint, function(err, res){
3030
| ------------- |------------- |-------------
3131
| content | categories, entities, language, morphology, relationships, sentences, sentiment, tokens | Either content or contentUri required |
3232
| contentUri | categories, entities, language, morphology, relationships, sentences, sentiment, tokens | Either content or contentUri required |
33-
| contentType | categories, entities, language, morphology, relationships, sentences, sentiment, tokens | No |
3433
| language | categories, entities, language, morphology, relationships, sentences, sentiment, tokens, name similarity | No |
3534
| documentFile | categories, entities, language, morphology, relationships, sentences, sentiment, tokens | No |
3635
| name1 | name similarity | Yes |

examples/sentiment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ parser.addArgument(["--url"], {help: "Rosette API alt-url", required: false});
1313
var args = parser.parseArgs();
1414

1515
var file = new tmp.File();
16-
var sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.</p></body></html>";
16+
var sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.</p></body></html>";
1717
var fileContents = sentiment_file_data;
1818

1919
file.writeFileSync(fileContents);

lib/categories.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function categories() {
4848
categories.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949

5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams().content === null && parameters.loadParams().contentUri === null) {
@@ -122,6 +122,7 @@ categories.prototype.getResults = function(parameters, userKey, serviceURL, call
122122
req.end();
123123

124124
}
125+
}
125126

126127
};
127128

lib/checkVersion.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var RosetteException = require("./rosetteExceptions");
2626
*
2727
* @type string
2828
*/
29-
var BINDING_VERSION = "0.8";
29+
var BINDING_VERSION = "0.10";
3030

3131
/**
3232
* @class

lib/entities.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function entities() {
4848
entities.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949

5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams().content == null && parameters.loadParams().contentUri == null) {
@@ -128,6 +128,7 @@ entities.prototype.getResults = function(parameters, userKey, serviceURL, callba
128128
req.end();
129129

130130
}
131+
}
131132

132133
};
133134

lib/language.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function language() {
4848
language.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949

5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams().content == null && parameters.loadParams().contentUri == null) {
@@ -64,6 +64,11 @@ language.prototype.getResults = function(parameters, userKey, serviceURL, callba
6464
protocol = http;
6565
}
6666

67+
var contentType = "application/json";
68+
if(parameters.loadParams().multipart === true){
69+
contentType = "multipart/mixed";
70+
}
71+
6772
var headers = {
6873
"accept": "application/json",
6974
"accept-encoding": "gzip",
@@ -123,6 +128,7 @@ language.prototype.getResults = function(parameters, userKey, serviceURL, callba
123128
req.end();
124129

125130
}
131+
}
126132

127133
};
128134

lib/morphology.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function morphology() {
4848
morphology.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949
//console.log(parameters)
5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams().content === null && parameters.loadParams().contentUri === null) {
@@ -126,6 +126,7 @@ morphology.prototype.getResults = function(parameters, userKey, serviceURL, call
126126
req.end();
127127

128128
}
129+
}
129130

130131
};
131132

lib/nameSimilarity.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function nameSimilarity() {
4848
nameSimilarity.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949

5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams().name1 == null || parameters.loadParams().name2 == null) {
@@ -121,6 +121,7 @@ nameSimilarity.prototype.getResults = function(parameters, userKey, serviceURL,
121121
req.end();
122122

123123
}
124+
}
124125

125126
};
126127

lib/nameTranslation.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function nameTranslation() {
4848
nameTranslation.prototype.getResults = function(parameters, userKey, serviceURL, callback) {
4949

5050
if (parameters.loadParams().documentFile != null) {
51-
parameters.loadFile(parameters.loadParams().documentFile);
52-
}
51+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, serviceURL, callback);
52+
} else {
5353

5454
// validate parameters
5555
if (parameters.loadParams.name === null) {
@@ -123,6 +123,7 @@ nameTranslation.prototype.getResults = function(parameters, userKey, serviceURL,
123123
req.end();
124124

125125
}
126+
}
126127

127128
};
128129

lib/parameters.js

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
var RosetteException = require("./rosetteExceptions");
1717
var RosetteConstants = require("./rosetteConstants");
1818
var fs = require("fs");
19+
var path = require("path");
20+
var Multipart = require('multipart-stream')
21+
var URL = require("url");
22+
var http = require("http");
23+
24+
/**
25+
* Compatible server version.
26+
*
27+
* @type string
28+
*/
29+
var BINDING_VERSION = "0.9";
1930

2031
/**
2132
* @class
@@ -28,7 +39,6 @@ function parameters() {
2839
// generic parameters
2940
this.content = null;
3041
this.contentUri = null;
31-
this.contentType = null;
3242
this.language = null;
3343
this.documentFile = null;
3444

@@ -70,7 +80,6 @@ parameters.prototype.loadParams = function() {
7080
var paramJSON = {
7181
"content": this.content,
7282
"contentUri": this.contentUri,
73-
"contentType": this.contentType,
7483
"language": this.language,
7584
"documentFile": this.documentFile,
7685
"name1": this.name1,
@@ -88,7 +97,7 @@ parameters.prototype.loadParams = function() {
8897
"linked": this.linked,
8998
"explain": this.explain,
9099
"short-string": this.shortString,
91-
"morphology": this.morphology
100+
"morphology": this.morphology,
92101
};
93102

94103
for (var key in paramJSON) {
@@ -101,14 +110,102 @@ parameters.prototype.loadParams = function() {
101110
};
102111

103112
/**
104-
* Reads a file and sets the content, contentType, and unit parameters accordingly
113+
* Reads a file and sets the content, and unit parameters accordingly
105114
*@param {file} filePath - The file path from which the desired file will be loaded
106115
*/
107-
parameters.prototype.loadFile = function(filePath) {
116+
parameters.prototype.loadFile = function(filePath, loadedParameters, userKey, serviceURL, callback) {
108117
var str = fs.readFileSync(filePath, "utf8");
118+
var mp = new Multipart()
119+
120+
mp.addPart({
121+
headers: {
122+
"Content-Type": "application/json",
123+
"Content-Disposition": "form-data; name=\"request\""
124+
},
125+
body: JSON.stringify(loadedParameters.loadParams())
126+
})
127+
128+
mp.addPart({
129+
headers: {
130+
"Content-Type": "text/plain",
131+
"Content-Disposition": "form-data; name=\"content\"; filename=\"" + path.basename(filePath) + "\""
132+
},
133+
body: str.toString()
134+
})
135+
136+
var data = ''
137+
mp.on('data', function(d) {
138+
data += d
139+
}).on('end', function() {
140+
141+
var urlParts = URL.parse(serviceURL + "sentiment");
142+
var protocol = http;
143+
if (urlParts.protocol === "http:") {
144+
protocol = http;
145+
}
146+
147+
var headers = {
148+
"accept": '*/*',
149+
"accept-encoding": "gzip",
150+
"content-type": 'multipart/form-data',
151+
"user-agent": "rosetteapinode/" + BINDING_VERSION
152+
}
153+
headers["user_key"] = userKey;
154+
155+
var result = new Buffer("");
156+
157+
var options = {
158+
hostname: urlParts.hostname,
159+
path: urlParts.path,
160+
method: 'POST',
161+
headers: headers,
162+
agent: false
163+
};
164+
165+
if (urlParts.port) {
166+
options.port = urlParts.port;
167+
}
168+
169+
// execute the http/https request
170+
var req = protocol.request(options, function(res) {
171+
res.on("data", function(docs) {
172+
result = Buffer.concat([result, docs]);
173+
});
174+
175+
res.on("end", function(err) {
176+
if (err) {
177+
console.log(err)
178+
}
179+
// will need for other endpoints
180+
if (res.headers["content-encoding"] === "gzip") {
181+
result = zlib.gunzipSync(result);
182+
}
183+
184+
result = JSON.parse(result);
185+
result.headers = res.headers;
186+
result = JSON.stringify(result);
187+
188+
if (res.statusCode === 200) {
189+
return callback(err, JSON.parse(result.toString()));
190+
} else if (res.statusCode != 200) {
191+
return callback(err, JSON.parse(result.toString()));
192+
}
193+
});
194+
});
195+
196+
// error catching for request
197+
req.on("error", function(e) {
198+
return console.log(e)
199+
});
200+
201+
req.write(data);
202+
203+
req.end();
204+
205+
})
109206

110-
this.content = str;
111-
this.contentType = RosetteConstants.dataFormat.UNSPECIFIED;
112207
}
113208

209+
210+
114211
module.exports = parameters;

0 commit comments

Comments
 (0)