Handle failed API requests, reauth when token expired
This commit is contained in:
@@ -5,61 +5,80 @@ export default class Lndhub {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
async callEndpoint (method, path, payload) {
|
||||
const options = { method, headers: { 'Content-Type': 'application/json' } };
|
||||
|
||||
if (path !== '/auth') {
|
||||
options.headers['Authorization'] = `Bearer ${this.accessToken}`;
|
||||
}
|
||||
if (typeof payload !== 'undefined') {
|
||||
options.body = JSON.stringify(payload);
|
||||
}
|
||||
|
||||
const res = await fetch(`${this.baseUrl}${path}`, options);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async handleErroredRequest (result, retryFunction, args=[]) {
|
||||
console.warn('API request failed:', result.message);
|
||||
if (result.code === 1) {
|
||||
return this.reauth().then(connected => {
|
||||
if (connected) {
|
||||
console.warn('Lndhub reconnected, trying again...');
|
||||
return this[retryFunction](...args);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async auth (username, password) {
|
||||
const payload = { "login": username, "password": password };
|
||||
const data = await this.callEndpoint('post', '/auth', payload);
|
||||
console.log('data', data);
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/auth`, {
|
||||
method: 'post',
|
||||
body: JSON.stringify(payload),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
this.refreshToken = data.refresh_token;
|
||||
this.accessToken = data.access_token;
|
||||
if (data.error) {
|
||||
console.warn('Lndhub connection failed:', data.message);
|
||||
return false;
|
||||
} else {
|
||||
this.refreshToken = data.refresh_token;
|
||||
this.accessToken = data.access_token;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
async reauth (refreshToken) {
|
||||
const payload = { "refresh_token": this.refreshToken };
|
||||
const data = await this.callEndpoint('post', '/auth', payload);
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/auth`, {
|
||||
method: 'post',
|
||||
body: JSON.stringify(payload),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
this.accessToken = data.access_token;
|
||||
if (data.error) {
|
||||
console.warn('Lndhub re-auth failed:', data.message);
|
||||
return false;
|
||||
} else {
|
||||
this.accessToken = data.access_token;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
async createInvoice (amount, description) {
|
||||
const payload = { amount, description };
|
||||
let data = await this.callEndpoint('post', '/v2/invoices', payload);
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/v2/invoices`, {
|
||||
method: 'post',
|
||||
body: JSON.stringify(payload),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.accessToken}`
|
||||
}
|
||||
});
|
||||
const data = await res.json();
|
||||
// TODO re-auth if token has expired
|
||||
|
||||
return data;
|
||||
if (data.error) {
|
||||
return this.handleErroredRequest(data, 'createInvoice', Array.from(arguments));
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
async getInvoice (paymentHash) {
|
||||
const res = await fetch(`${this.baseUrl}/v2/invoices/${paymentHash}`, {
|
||||
method: 'get',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.accessToken}`
|
||||
}
|
||||
});
|
||||
const data = await res.json();
|
||||
// TODO re-auth if token has expired
|
||||
const data = await this.callEndpoint('get', `/v2/invoices/${paymentHash}`);
|
||||
|
||||
return data;
|
||||
if (data.error) {
|
||||
return this.handleErroredRequest(data, 'getInvoice', Array.from(arguments));
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user