var overpassApiWrapper = function(query, cb, options) {
    var contentType;
    options = options || {};

    var toGeoJSON = function(data) {
        var geojson;

        geojson = osmtogeojson(data, {
            flatProperties: options.flatProperties || false
        });
        cb(undefined, geojson);
    };

    var handleXml = function (data) {
        var parser = new xmldom.DOMParser();
        var doc = parser.parseFromString(data);
        toGeoJSON(doc);
    }
    
    var reqOptions = preferences && JSON.parse(JSON.stringify(preferences.conOptions)) || {headers: {}}
    reqOptions.body = querystring.stringify({ data: query })
    reqOptions.headers['content-type'] = 'application/x-www-form-urlencoded'

    httpreq.post(options.overpassUrl || 'http://overpass-api.de/api/interpreter', reqOptions, function(err, response) {
            if (response && response.statusCode == 200) {
                contentType = response.headers['content-type'];
                if (contentType.indexOf('json') >= 0) {
                    toGeoJSON(JSON.parse(response.body))
                } else if (contentType.indexOf('xml') >= 0) {
                    handleXml(response.body)
                } else {
                    cb({
                        message: 'Unknown Content-Type "' + contentType + '" in response'
                    });
                }
            }else{
                if(response){
                    cb({
                        message: 'Request failed: HTTP ' + response.statusCode + '\n' + response.body || '',
                        statusCode: response.statusCode
                    });
                }else{
                    cb({
                        message: 'Request failed',
                        statusCode: ""
                    });
                }
                
            }
        }
    );
}