confluent-schema-registry-node
Version:
Confluent Schema Registry API Node.js Client
88 lines (74 loc) • 2.64 kB
JavaScript
const http = require('http');
const querystring = require('querystring');
// Get Global Schema Registry Compatibility Level
function getGlobalCompatibilityLevel() {
var options = {
host: "localhost",
port: 8081,
path: "/config",
headers: {
accept: "application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json"
}
};
http.get(options, function(res) {
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
let error;
if(statusCode !== 200) {
error = new Error(`Request Failed.\n` +
`Status Code: ${statusCode}`);
} else if(!/^application\/vnd.schemaregistry.v1\+json/.test(contentType)) {
error = new Error(`Invalid content-type.\n` +
`Expected application/vnd.schemaregistry.v1+json but received ${contentType}`);
}
if(error) {
console.log(error.message);
//consume response data to free up memory.
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
let parsedData = JSON.parse(rawData);
console.log(parsedData);
} catch (e) {
console.log(e.message);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
}
function updateGlobalCompatibilityLevel(putData) {
var options = {
host: "localhost",
port: 8081,
path: "/config",
method: "PUT",
headers: {
"accept": "application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json"
}
};
var req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(JSON.stringify(putData));
req.end();
}
module.exports.updateGlobalCompatibilityLevel = updateGlobalCompatibilityLevel;
module.exports.getGlobalCompatibilityLevel = getGlobalCompatibilityLevel;