API시나리오 단계별 실행
This commit is contained in:
@@ -11,14 +11,16 @@ class APIClient {
|
||||
window.globals = {};
|
||||
this.globalVariables = window.globals;
|
||||
this.variables = {};
|
||||
this.currentAjaxRequest = null;
|
||||
this.abortController = null;
|
||||
}
|
||||
|
||||
async sendAPIRequest(model) {
|
||||
async sendAPIRequest(model, callback) {
|
||||
try {
|
||||
const updatedModel = await this.executePreRequestScript(model);
|
||||
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel);
|
||||
if (callback){
|
||||
callback(processedRequest);
|
||||
}
|
||||
return this.makeAjaxRequest(processedRequest, model);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -45,14 +47,17 @@ class APIClient {
|
||||
body: JSON.stringify(processedRequest),
|
||||
signal: signal // pass the signal to the fetch
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(async response => {
|
||||
const responseData = await response.json(); // Parse the JSON response
|
||||
if (!response.ok) {
|
||||
// If response is not OK, use the parsed error information
|
||||
const errorMessage = responseData.error || 'Unknown error';
|
||||
throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`);
|
||||
}
|
||||
return responseData;
|
||||
})
|
||||
.then(data => {
|
||||
try {
|
||||
model.responseModel.status = data.status;
|
||||
model.responseModel.body = data.body;
|
||||
model.responseModel.headers = data.headers;
|
||||
model.responseModel.size = data.size;
|
||||
|
||||
let response = {};
|
||||
response.body = data.body;
|
||||
response.status = data.status;
|
||||
@@ -61,7 +66,8 @@ class APIClient {
|
||||
_.forEach(data.headers, (header) => {
|
||||
response.headers[header.key] = header.value;
|
||||
});
|
||||
return this.executePostRequestScript(model.postRequestScript, model.responseModel);
|
||||
|
||||
return this.executePostRequestScript(model.postRequestScript, response);
|
||||
} catch (error) {
|
||||
console.log('error1')
|
||||
$('.toast-body').text(error);
|
||||
@@ -69,14 +75,7 @@ class APIClient {
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('error2')
|
||||
console.log(error.name)
|
||||
if (error.name === 'AbortError') {
|
||||
console.log('Fetch aborted');
|
||||
} else {
|
||||
// $('.toast-body').text(response.responseJSON.error);
|
||||
// $('.toast').toast('show');
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user