File size: 1,873 Bytes
2fcec7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(function () {

    var request = require('request');
    var fs = require('fs');

    function getHeaders(apiToken, contentType) {
        return {
            'Accept': 'text/html',
            'Content-Type': contentType,
            'Authentication': 'Token ' + apiToken
        };
    }

    exports.fetch = function (apiName, apiToken, callback, errorCallback) {
        callback = callback || false;
        errorCallback = errorCallback || false;
        request({
            method: 'GET',
            url: 'https://api.apiary.io/blueprint/get/' + apiName,
            headers: getHeaders(apiToken, 'text/plain')
        }, function (error, response, body) {
            if (response.statusCode == 200) {
                if (callback) {
                    callback(JSON.parse(body).code);
                }
            } else {
                if (errorCallback) {
                    errorCallback(response);
                }
            }
        });
    };

    exports.publish = function (code, apiName, apiToken, callback, errorCallback) {
        callback = callback || false;
        errorCallback = errorCallback || false;
        var body = {
            code: code,
            messageToSave: "Published from local repository"
        };
        request({
            method: 'POST',
            url: 'https://api.apiary.io/blueprint/publish/' + apiName,
            headers: getHeaders(apiToken, 'application/json'),
            body: JSON.stringify(body)
        }, function (error, response, body) {
            if (response.statusCode == 200 || response.statusCode == 201) {
                if (callback) {
                    callback(response, body);
                }
            } else {
                if (errorCallback) {
                    errorCallback(response, body);
                }
            }
        });
    };

}).call(this);