From a3ecf4abdee1d81ecc5256218910f3a5335a8d5c Mon Sep 17 00:00:00 2001 From: navaneeth Date: Fri, 30 Apr 2021 11:18:38 +0530 Subject: [PATCH] Refactor API services to remove repeated code --- frontend/src/_helpers/auth-header.js | 5 ++- frontend/src/_services/app.service.js | 21 +++-------- frontend/src/_services/appVersion.service.js | 14 ++------ frontend/src/_services/dataquery.service.js | 21 ++++------- frontend/src/_services/datasource.service.js | 35 ++++--------------- .../_services/organization_user.service.js | 14 +++----- frontend/src/_services/user.service.js | 20 +++-------- 7 files changed, 34 insertions(+), 96 deletions(-) diff --git a/frontend/src/_helpers/auth-header.js b/frontend/src/_helpers/auth-header.js index 26de05283b..bb96927a58 100644 --- a/frontend/src/_helpers/auth-header.js +++ b/frontend/src/_helpers/auth-header.js @@ -4,7 +4,10 @@ export function authHeader() { // return authorization header with jwt token const currentUser = authenticationService.currentUserValue; if (currentUser && currentUser.auth_token) { - return { Authorization: `${currentUser.auth_token}` }; + return { + Authorization: `${currentUser.auth_token}`, + 'Content-Type': 'application/json' + }; } else { return {}; } diff --git a/frontend/src/_services/app.service.js b/frontend/src/_services/app.service.js index 258b209efe..2b04998eaf 100644 --- a/frontend/src/_services/app.service.js +++ b/frontend/src/_services/app.service.js @@ -18,11 +18,8 @@ function getAll() { function createApp() { const body = { } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/apps`, requestOptions).then(handleResponse); } @@ -33,12 +30,7 @@ function getApp(id) { function saveApp(id, attributes) { - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - - const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify({app: attributes}) }; + const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify({app: attributes}) }; return fetch(`${config.apiUrl}/apps/${id}`, requestOptions).then(handleResponse); } @@ -53,10 +45,7 @@ function createAppUser(app_id, org_user_id, role) { org_user_id, role } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/app_users`, requestOptions).then(handleResponse); } diff --git a/frontend/src/_services/appVersion.service.js b/frontend/src/_services/appVersion.service.js index e4f1c2e37a..147c5bf000 100644 --- a/frontend/src/_services/appVersion.service.js +++ b/frontend/src/_services/appVersion.service.js @@ -17,24 +17,16 @@ function create(appId, versionName) { const body = { versionName } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/apps/${appId}/versions`, requestOptions).then(handleResponse); } function save(appId, versionId, definition) { - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const body = { definition }; - const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify(body) }; + const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/apps/${appId}/versions/${versionId}`, requestOptions).then(handleResponse); } diff --git a/frontend/src/_services/dataquery.service.js b/frontend/src/_services/dataquery.service.js index 6159d803a3..574d1c86c3 100644 --- a/frontend/src/_services/dataquery.service.js +++ b/frontend/src/_services/dataquery.service.js @@ -21,11 +21,8 @@ function create(app_id, name, kind, options, data_source_id) { options, data_source_id } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_queries`, requestOptions).then(handleResponse); } @@ -34,11 +31,8 @@ function update(id, name, options) { options, name } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'PATCH', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'PATCH', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_queries/${id}`, requestOptions).then(handleResponse); } @@ -46,10 +40,7 @@ function run(queryId, options) { const body = { options: options } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_queries/${queryId}/run`, requestOptions).then(handleResponse); } diff --git a/frontend/src/_services/datasource.service.js b/frontend/src/_services/datasource.service.js index ab873dd179..0c48c32416 100644 --- a/frontend/src/_services/datasource.service.js +++ b/frontend/src/_services/datasource.service.js @@ -22,11 +22,8 @@ function create(app_id, name, kind, options) { kind, options } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_sources`, requestOptions).then(handleResponse); } @@ -36,11 +33,8 @@ function save(id, app_id, name, options) { name, options } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_sources/${id}`, requestOptions).then(handleResponse); } @@ -51,31 +45,16 @@ function test(kind, options) { options }; - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_sources/test_connection`, requestOptions).then(handleResponse); } function setOauth2Token(dataSourceId, body) { - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/data_sources/${dataSourceId}/authorize_oauth2`, requestOptions).then(handleResponse); } function fetchOauth2BaseUrl(provider) { - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify({provider}) }; + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify({provider}) }; return fetch(`${config.apiUrl}/data_sources/fetch_oauth2_base_url`, requestOptions).then(handleResponse); } diff --git a/frontend/src/_services/organization_user.service.js b/frontend/src/_services/organization_user.service.js index f1b2baee19..d2ef5703e1 100644 --- a/frontend/src/_services/organization_user.service.js +++ b/frontend/src/_services/organization_user.service.js @@ -13,11 +13,8 @@ function create(first_name, last_name, email, role) { email, role } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/organization_users`, requestOptions).then(handleResponse); } @@ -26,10 +23,7 @@ function changeRole(id, role) { const body = { role } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/organization_users/${id}/change_role`, requestOptions).then(handleResponse); } diff --git a/frontend/src/_services/user.service.js b/frontend/src/_services/user.service.js index c443079181..a95130de6c 100644 --- a/frontend/src/_services/user.service.js +++ b/frontend/src/_services/user.service.js @@ -20,20 +20,13 @@ function createUser(first_name, last_name, email, role) { email, role } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/users`, requestOptions).then(handleResponse); } function deleteUser(id) { - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'DELETE', headers: headers, body: JSON.stringify({}) }; + const requestOptions = { method: 'DELETE', headers: authHeader(), body: JSON.stringify({}) }; return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse); } @@ -42,11 +35,8 @@ function setPasswordFromToken(token, password) { token, password } - const headers = { - ...authHeader(), - 'Content-Type': 'application/json' - } - const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) }; + + const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) }; return fetch(`${config.apiUrl}/user/set_password_from_token`, requestOptions).then(handleResponse); }