forked from CartoDB/cdb-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
111 lines (94 loc) · 3.13 KB
/
api.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let api = angular.module("api", []);
// generic function for sending requests through a $http-like http service
// obj is the api object that makes the request
// action and error are promises to be run on success and error respectively and after the
// corresponding default promises have been run
function sendRequest(obj, request, httpService, action, error) {
obj.running = true;
obj.valid = null;
obj.raw = null;
// action and error are functions for the promise. if defined they'll be run after the default action and error functions
let _action = function (result) {
obj.raw = result;
obj.valid = true;
if (action) {
action(result);
}
obj.running = false;
};
let _error = function (result) {
obj.raw = result;
obj.valid = false;
if (error) {
error(result);
}
obj.running = false;
};
httpService(request).then(_action).catch(_error);
}
// SQLClient makes request to CARTO's SQL API
// internal objects:
// running, valid and raw from sendRequest
// items: list of objects returned by the database on success
// errorMessage: error message on error
api.factory('SQLClient', ["$http", "endpoints", "alerts", function ($http, endpoints, alerts) {
return function () {
let self = this;
this.items = null;
this.errorMessage = null;
// action and error are functions for the promise. if defined they'll be run after the default action and error functions
this.send = function (query, action, error, maxRetry) {
let currentEndpoint = endpoints.current;
maxRetry = maxRetry || 0;
if (currentEndpoint && currentEndpoint.sqlURL) {
let params = {
q: query
};
if (currentEndpoint.apiKey) {
params.api_key = currentEndpoint.apiKey;
}
let req = {
method: 'GET',
url: currentEndpoint.sqlURL,
params: params
};
let _action = function (result) {
if (result.data && result.data.rows.length > 0) {
self.items = result.data.rows;
} else {
self.items = null;
}
self.errorMessage = null;
if (action) {
action(result);
}
};
let _error = function (result) {
self.items = null;
if (result.status == 400) {
// PostgreSQL error
self.errorMessage = result.data.error[0];
} else if (result.status == 429 && maxRetry < 6) {
const retryAfter = Number(result.headers()['retry-after']) * 1000 || 1000;
maxRetry += 1;
setTimeout(() => {
this.send(query, action, error, maxRetry);
}, retryAfter);
} else {
// Network error
self.errorMessage = result.statusText;
if (self.errorMessage) {
alerts.add("error", "Endpoint error: " + self.errorMessage);
} else {
alerts.add("error", "Unknown endpoint error");
}
}
if (error) {
error(result);
}
};
sendRequest(self, req, $http, _action, _error);
}
};
};
}]);