(projectId, chartId, drId, getCache)
| 138 | } |
| 139 | |
| 140 | export function runDataRequest(projectId, chartId, drId, getCache) { |
| 141 | return (dispatch) => { |
| 142 | const token = cookie.load("brewToken"); |
| 143 | let url = `${API_HOST}/project/${projectId}/chart/${chartId}/dataRequest/${drId}/request`; |
| 144 | const method = "POST"; |
| 145 | const body = JSON.stringify({ getCache }); |
| 146 | const headers = new Headers({ |
| 147 | "Accept": "application/json", |
| 148 | "Content-Type": "application/json", |
| 149 | "Authorization": `Bearer ${token}`, |
| 150 | }); |
| 151 | |
| 152 | let status = { |
| 153 | statusCode: 500, |
| 154 | statusText: "Internal Server Error", |
| 155 | }; |
| 156 | let ok = true; |
| 157 | |
| 158 | if (getCache) { |
| 159 | url += "?getCache=true"; |
| 160 | } |
| 161 | |
| 162 | dispatch({ type: FETCHING_DATA_REQUEST, id: drId }); |
| 163 | return fetch(url, { method, body, headers }) |
| 164 | .then((response) => { |
| 165 | status = { |
| 166 | statusCode: response.status, |
| 167 | statusText: response.statusText, |
| 168 | }; |
| 169 | |
| 170 | if (!response.ok) { |
| 171 | dispatch(addError(response.status, "Error while making the request")); |
| 172 | ok = false; |
| 173 | return response.text(); |
| 174 | } |
| 175 | |
| 176 | return response.json(); |
| 177 | }) |
| 178 | .then((data) => { |
| 179 | if (!ok) { |
| 180 | return Promise.reject({ ...status, message: data }); |
| 181 | } |
| 182 | |
| 183 | dispatch({ |
| 184 | type: FETCH_DATA_REQUEST_SUCCESS, |
| 185 | dataRequest: data.dataRequest.dataRequest, |
| 186 | response: data.dataRequest.responseData, |
| 187 | }); |
| 188 | return Promise.resolve(data.dataRequest); |
| 189 | }) |
| 190 | .catch((error) => { |
| 191 | dispatch({ type: FETCH_DATA_REQUEST_FAIL, id: drId, error: error.message }); |
| 192 | return Promise.reject(error); |
| 193 | }); |
| 194 | }; |
| 195 | } |
no test coverage detected