-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
131 lines (119 loc) · 3.49 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var request = require('https').request;
var url = require('url');
var Post = require('./post');
var Result = require('./result');
var Query = require('./query');
var Parser = require('./parser');
var TwinglyError = require('./errors').TwinglyError;
var TwinglyAuthError = require('./errors').TwinglyAuthError;
var TwinglyQueryError = require('./errors').TwinglyQueryError;
var TwinglyServerError = require('./errors').TwinglyServerError;
/**
* Creates a new Twingly Search API client
*
* @property {string} apiKey
* @property {string} userAgent
*
* @param {string} apiKey - Twingly Search API Key. If not provided, reads key from environment (TWINGLY_SEARCH_KEY)
* @param {string} userAgent - The user agent to be used for all requests
* @returns {Client}
* @constructor
*/
var Client = function(apiKey, userAgent) {
if (!(this instanceof Client)) {
return new Client(apiKey, userAgent);
}
this.BASE_URL = 'https://api.twingly.com';
this.SEARCH_API_VERSION = "v3";
this.SEARCH_PATH = "/blog/search/api/" + this.SEARCH_API_VERSION + "/search";
this.VERSION = '1.1.1';
this.apiKey = apiKey;
if((this.apiKey == undefined)||(this.apiKey == null)) {
this.apiKey = envApiKey();
}
if((this.apiKey == undefined)||(this.apiKey == null)) {
apiKeyMissing();
}
this.userAgent = userAgent;
if (this.userAgent == undefined) {
this.userAgent = 'Twingly Search JavaScript Client/' + this.VERSION;
}
};
/**
* Returns a new {Query} object connected to this client
*
* @returns {Query}
*/
Client.prototype.query = function() {
return new Query(this);
};
/**
* Executes the given Query and returns the result
*
* This method should not be called manually, instead call {@link Query#execute}.
*
* @param {Query} query
* @param {Client~executeQueryCallback} callback - the callback that handles the response
*/
Client.prototype.executeQuery = function(query, callback) {
getResponse(this, query, function(error, response_body){
(new Parser()).parse(response_body, callback);
});
};
/**
* @callback Client~executeQueryCallback
* @param {TwinglyError} error
* @param {Result} result
*/
/**
* Returns API endpoint URL
*
* @returns {string}
*/
Client.prototype.endpointUrl = function() {
return this.BASE_URL + this.SEARCH_PATH;
};
var apiKeyMissing = function() {
throw new TwinglyAuthError('No API key has been provided.');
};
var envApiKey = function() {
return process.env.TWINGLY_SEARCH_KEY;
};
var getResponse = function(client, query, callback) {
var parsed_url = url.parse(query.url());
var options = {
hostname: parsed_url.hostname,
port: 443,
path: parsed_url.path,
headers: {
'User-Agent': client.userAgent
}
};
var req = request(options, function (res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
if ((res.statusCode >= 200) && (res.statusCode < 300)) {
callback(false, body);
} else {
callback(true, body);
}
});
});
req.on('error', function (err) {
callback(true, err);
});
req.end();
};
module.exports = {
Client: Client,
Result: Result,
Post: Post,
TwinglyError: TwinglyError,
TwinglyAuthError: TwinglyAuthError,
TwinglyQueryError: TwinglyQueryError,
TwinglyServerError: TwinglyServerError
};