<commit_before>var http = require('http')

module.exports = function( query, callback ) {
	// prevent multiple callbacks
	var complete = false
	function doCallback( err, res ) {
		if( !complete ) {
			complete = true
			callback( err, res )
		}
	}
	
	// build request
	if( query.substring(-1) !== ';' ) {
		query += ';'
	}
	
	query = encodeURIComponent( query )
	var options = {
		host: 'api.openkvk.nl',
		path: '/json/'+ query,
		header: {
			'User-Agent': 'openkvk.js (https://github.com/fvdm/nodejs-openkvk'
		}
	}
	
	var request = http.request( options )
	
	request.on( 'response', function( response ) {
		var data = []
		var size = 0
		
		response.on( 'data', function( ch ) {
			data.push( ch )
			size += ch.length
		})
		
		response.on( 'close', function() {
			doCallback( new Error('request dropped') )
		})
		
		response.on( 'end', function() {
			// process response
			var buf = new Buffer( size )
			var pos = 0
			
			for( var d in data ) {
				data[d].copy( buf, pos )
				pos += data[d].length
			}
			
			data = buf.toString('utf8').trim()
			
			// really json?
			if( ! data.match( /^\[.*\]$/ ) ) {
				doCallback( new Error('not json') )
				return
			}
			
			data = JSON.parse( data )
			
			// clean up
			delete buf
			delete pos
			delete size
			
			// data structure
			var result = []
			var props = data[0].RESULT.HEADER
			
			for( var r in data[0].RESULT.ROWS ) {
				var row = data[0].RESULT.ROWS[r]
				var record = {}
				for( var i in row ) {
					record[ props[i] ] = row[i]
				}
				result.push( record )
			}
			
			// clean up
			delete result
			delete props
			
			// done
			doCallback( null, result )<commit_msg>Added support for 'officieel' and 'api' endpoints<commit_after>var http = require('http')

module.exports = function( query, callback ) {
	if( typeof query === 'string' && !!~query.indexOf(' ') ) {
		query += query.substring(-1) !== ';' ? ';' : ''
		talk( 'api', query, function( err, data ) {
			if( !err ) {
				// data structure
				var result = []
				var props = data[0].RESULT.HEADER
				
				for( var r in data[0].RESULT.ROWS ) {
					var row = data[0].RESULT.ROWS[r]
					var record = {}
					for( var i in row ) {
						record[ props[i] ] = row[i]
					}
					result.push( record )
				}
				callback( null, result )
			} else {
				callback( err )
			}
		})
	} else {
		talk( 'officieel', query, callback )
	}
}

function talk( host, query, callback ) {
	// prevent multiple callbacks
	var complete = false
	function doCallback( err, res ) {
		if( !complete ) {
			complete = true
			callback( err, res )
		}
	}
	
	// build request
	query = encodeURIComponent( query )
	var options = {
		host: host +'.openkvk.nl',
		path: '/json/'+ query,
		header: {
			'User-Agent': 'openkvk.js (https://github.com/fvdm/nodejs-openkvk'
		}
	}
	
	
	var request = http.request( options )
	
	request.on( 'response', function( response ) {
		var data = []
		var size = 0
		
		response.on( 'data', function( ch ) {
			data.push( ch )
			size += ch.length
		})
		
		response.on( 'close', function() {
			doCallback( new Error('request dropped') )
		})
		
		response.on( 'end', function() {
			// process response
			var buf = new Buffer( size )
			var pos = 0
			
			for( var d in data ) {
				data[d].copy( buf, pos )
				pos += data[d].length
			}
			
			data = buf.toString('utf8').trim()
			
			// really json?
			if( ! data.match( /^\[.*\]$/ ) ) {
				doCallback( new Error('not json') )
				return
			}
			
			data = JSON.parse( data )
			
			// done
			doCallback( null, data )
{'fraction_non_alphanumeric': 0.16161902066232664, 'fraction_numerical': 0.003962637984715539, 'mean_word_length': 2.2752548656163114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1003603', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1222, 'n_words': 327}