1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/apiService.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-09-03 06:38:27 +02:00
function ApiService(tokenService) {
this.baseUrl = 'https://api.bitwarden.com';
this.tokenService = tokenService;
};
!function () {
2016-09-03 07:13:09 +02:00
// Account APIs
2016-09-03 06:38:27 +02:00
ApiService.prototype.getProfile = function (success, error) {
var self = this;
2016-09-03 07:13:09 +02:00
this.tokenService.getToken(function (token) {
2016-09-03 06:38:27 +02:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/accounts/profile',
data: 'access_token=' + token,
dataType: 'json',
2016-09-03 07:13:09 +02:00
success: function (response) {
success(new ProfileResponse(response))
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, textStatus, errorThrown);
}
2016-09-03 06:38:27 +02:00
});
});
};
2016-09-03 07:13:09 +02:00
// Site APIs
// Folder APIs
// Cipher APIs
ApiService.prototype.getCipher = function (id, success, error) {
var self = this;
this.tokenService.getToken(function (token) {
$.ajax({
type: 'GET',
url: self.baseUrl + '/ciphers/' + id,
data: 'access_token=' + token,
dataType: 'json',
success: function (response) {
success(new CipherResponse(response))
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, textStatus, errorThrown);
}
});
});
};
ApiService.prototype.getCiphers = function (success, error) {
var self = this;
this.tokenService.getToken(function (token) {
$.ajax({
type: 'GET',
url: self.baseUrl + '/ciphers',
data: 'access_token=' + token,
dataType: 'json',
success: function (response) {
var data = [];
for (var i = 0; i < response.length; i++) {
data.push(new CipherResponse(response[i]));
}
success(new ListResponse(data))
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, textStatus, errorThrown);
}
});
});
};
function handleError(errorCallback, jqXHR, textStatus, errorThrown) {
errorCallback(new ErrorResponse(jqXHR));
}
2016-09-03 06:38:27 +02:00
}();