This repository was archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathepo-ops.js
More file actions
147 lines (125 loc) · 2.94 KB
/
epo-ops.js
File metadata and controls
147 lines (125 loc) · 2.94 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Module name: epo-ops
Description: Access some basic EPO Open Patent Services with node.js
Author: Franklin van de Meent (https://frankl.in);
Source & docs: https://github.com/fvdm/nodejs-epo-ops
Feedback: https://github.com/fvdm/nodejs-epo-ops/issues
License: Unlicense (public domain) - see UNLICENSE file
Service name: European Patent Office - Open Patent Services
Service docs: http://www.epo.org/searching/free/ops.html
*/
var xml2json = require ('simple-xml2json') .parser;
var http = require ('httpreq');
var config = {};
var app = {
status: {}
};
/**
* Base64 encode a string
*
* @param {String} str
* @return {void}
*/
function base64_encode (str) {
return new Buffer (str, 'utf8') .toString ('base64');
}
/**
* Send HTTP request to API
*
* @param {String} method
* @param {String} path
* @param {Object} params
* @param {Function} callback
* @return {void}
*/
function talk (method, path, params, callback) {
var options = {
url: 'https://ops.epo.org/3.1' + path,
parameters: params,
method: method,
timeout: config.timeout,
headers: {
'User-Agent': 'epo-ops.js (https://github.com/fvdm/nodejs-epo-ops)'
}
};
if (config.access_token) {
options.headers.Authorization = 'Bearer ' + config.access_token;
} else {
options.auth = base64_encode (config.consumer_key + ':' + config.consumer_secret);
}
http.doRequest (options, function (err, res) {
var data = null;
var error = null;
var key;
if (err) {
return callback (err);
}
try {
data = xml2json (res.body);
if (data.error) {
error = new Error ('API error');
error.error = data.error;
data = null;
}
} catch (e) {
error = e;
}
if (res.headers instanceof Object) {
for (key in res.headers) {
if (key.match (/^x_/)) {
app.status [key] = res.headers [key];
}
}
}
callback (error, data);
});
}
/**
* Get access_token from API
*
* @param {Function} callback
* @return {void}
*/
app.accessToken = function oauth_accesstoken (callback) {
var params = {
grant_type: 'client_credentials'
};
talk ('POST', '/auth/accesstoken', params, function (err, data) {
if (err) {
return callback (err);
}
if (data.access_token) {
config.access_token = data.access_token;
}
callback (null, data);
});
}
/**
* Authenticated HTTP request
*
* @param {String} path
* @param {Object} params
* @param {Function} callback
* @return {void}
*/
app.get = function oauth_get (path, params, callback) {
if (typeof params === 'function') {
callback = params;
params = null;
}
talk ('GET', path, params, callback);
};
/**
* Module configuration
* returns object with methods
*
* @param {Object} params
* @return {Object}
*/
module.exports = function (params) {
var key;
for (key in params) {
config [key] = params [key];
}
return app;
};