mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
34 lines
972 B
JavaScript
34 lines
972 B
JavaScript
class RestAPI {
|
|
constructor({logger}) {
|
|
this.csrftoken = this.getCookie('csrftoken');
|
|
this.logger = logger
|
|
}
|
|
|
|
getCookie(name) {
|
|
let cookieValue = null;
|
|
if (document.cookie && document.cookie !== '') {
|
|
const cookies = document.cookie.split(';');
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
const cookie = cookies[i].trim();
|
|
// Does this cookie string begin with the name we want?
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return cookieValue;
|
|
}
|
|
|
|
write(packet) {
|
|
$.ajax({
|
|
type: "PUT",
|
|
url: "/api/v1/dcc/command",
|
|
data: packet,
|
|
success: function (data) { displayLog('[RECEIVE] '+data.response.replace(/\n/g,"")); },
|
|
contentType: "text/plain",
|
|
headers: {'X-CSRFToken': this.csrftoken}
|
|
});
|
|
}
|
|
}
|