var request = require('request');

var base = 'https://api.imgur.com/3/';

function upload(image, clientId, cb) {
    var options = {
        url: base + '/image.json',
        headers: {
            Authorization: 'Client-ID ' + clientId
        },
        formData: {
            image: image
        }
    };
    request.post(options, function(err, response, body) {
        if (err) return cb(err);
        var res = JSON.parse(body);
        if (!res.success) return cb(res.data.error);
        cb(null, res.data.link);
    });
}

module.exports = upload;