44 * License, v. 2.0. If a copy of the MPL was not distributed with this
55 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
66 */
7+ import { store } from '../redux/store'
8+
9+ let PREFIX_CASE_QUERIES ;
10+ let PREFIX_STUDY_QUERIES ;
11+ if ( process . env . REACT_APP_USE_AUTHENTICATION === "true" ) {
12+ PREFIX_CASE_QUERIES = process . env . REACT_APP_API_GATEWAY + "/case" ;
13+ PREFIX_STUDY_QUERIES = process . env . REACT_APP_API_GATEWAY + "/study" ;
14+ } else {
15+ PREFIX_CASE_QUERIES = process . env . REACT_APP_API_CASE_SERVER ;
16+ PREFIX_STUDY_QUERIES = process . env . REACT_APP_API_STUDY_SERVER ;
17+ }
18+
19+ function getToken ( ) {
20+ const state = store . getState ( ) ;
21+ return state . user . id_token ;
22+ }
23+
24+ function backendFetch ( url , init ) {
25+ if ( ! ( typeof ( init ) == "undefined" || typeof ( init ) == "object" ) ) {
26+ throw new TypeError ( "Argument 2 of backendFetch is not an object" + typeof ( init ) ) ;
27+ }
28+ const initCopy = Object . assign ( { } , init ) ;
29+ if ( process . env . REACT_APP_USE_AUTHENTICATION === "true" ) {
30+ initCopy . headers = new Headers ( initCopy . headers ) ;
31+ initCopy . headers . append ( "Authorization" , "Bearer: " + getToken ( ) )
32+ }
33+ return fetch ( url , initCopy ) ;
34+ }
735
836export function fetchStudies ( ) {
937 console . info ( "Fetching studies..." ) ;
10- const fetchStudiesUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies" ;
38+ const fetchStudiesUrl = PREFIX_STUDY_QUERIES + "/v1/studies" ;
1139 console . debug ( fetchStudiesUrl ) ;
12- return fetch ( fetchStudiesUrl )
13- . then ( response => response . json ( ) ) ;
40+ return backendFetch ( fetchStudiesUrl ) . then ( response => response . json ( ) ) ;
1441}
1542
1643export function fetchCases ( ) {
1744 console . info ( "Fetching cases..." ) ;
18- const fetchCasesUrl = process . env . REACT_APP_API_CASE_SERVER + "/v1/cases" ;
45+ const fetchCasesUrl = PREFIX_CASE_QUERIES + "/v1/cases" ;
1946 console . debug ( fetchCasesUrl ) ;
20- return fetch ( fetchCasesUrl )
21- . then ( response => response . json ( ) ) ;
47+ return backendFetch ( fetchCasesUrl ) . then ( response => response . json ( ) ) ;
2248}
2349
2450export function getVoltageLevelSingleLineDiagram ( studyName , voltageLevelId , useName , centerLabel , diagonalLabel , topologicalColoring ) {
2551 console . info ( `Getting url of voltage level diagram '${ voltageLevelId } ' of study '${ studyName } '...` ) ;
26- return process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/network/voltage-levels/" + voltageLevelId + "/svg-and-metadata?useName=" + useName
52+ return PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/network/voltage-levels/" + voltageLevelId + "/svg-and-metadata?useName=" + useName
2753 + "¢erLabel=" + centerLabel + "&diagonalLabel=" + diagonalLabel + "&topologicalColoring=" + topologicalColoring ;
2854}
2955
3056export function fetchSvg ( svgUrl ) {
3157 console . debug ( svgUrl ) ;
32- return fetch ( svgUrl )
33- . then ( response => response . ok ?
34- response . json ( ) :
35- response . json ( ) . then ( error =>
36- Promise . reject ( new Error ( error . error ) ) ) ) ;
58+ return backendFetch ( svgUrl ) . then ( response => response . ok ?
59+ response . json ( ) :
60+ response . json ( ) . then ( error => Promise . reject ( new Error ( error . error ) ) ) ) ;
3761}
3862
3963export function fetchSubstations ( studyName ) {
4064 console . info ( `Fetching substations of study '${ studyName } '...` ) ;
41- const fetchSubstationsUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/network-map/substations" ;
65+ const fetchSubstationsUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/network-map/substations" ;
4266 console . debug ( fetchSubstationsUrl ) ;
43- return fetch ( fetchSubstationsUrl )
44- . then ( response => response . json ( ) ) ;
67+ return backendFetch ( fetchSubstationsUrl ) . then ( response => response . json ( ) ) ;
4568}
4669
4770export function fetchSubstationPositions ( studyName ) {
4871 console . info ( `Fetching substation positions of study '${ studyName } '...` ) ;
49- const fetchSubstationPositionsUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/geo-data/substations" ;
72+ const fetchSubstationPositionsUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/geo-data/substations" ;
5073 console . debug ( fetchSubstationPositionsUrl ) ;
51- return fetch ( fetchSubstationPositionsUrl )
52- . then ( response => response . json ( ) ) ;
74+ return backendFetch ( fetchSubstationPositionsUrl ) . then ( response => response . json ( ) ) ;
5375}
5476
5577export function fetchLines ( studyName ) {
5678 console . info ( `Fetching lines of study '${ studyName } '...` ) ;
57- const fetchLinesUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/network-map/lines" ;
79+ const fetchLinesUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/network-map/lines" ;
5880 console . debug ( fetchLinesUrl ) ;
59- return fetch ( fetchLinesUrl )
60- . then ( response => response . json ( ) ) ;
81+ return backendFetch ( fetchLinesUrl ) . then ( response => response . json ( ) ) ;
6182}
6283
6384export function fetchLinePositions ( studyName ) {
6485 console . info ( `Fetching line positions of study '${ studyName } '...` ) ;
65- const fetchLinePositionsUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/geo-data/lines" ;
86+ const fetchLinePositionsUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/geo-data/lines" ;
6687 console . debug ( fetchLinePositionsUrl ) ;
67- return fetch ( fetchLinePositionsUrl )
68- . then ( response => response . json ( ) ) ;
88+ return backendFetch ( fetchLinePositionsUrl ) . then ( response => response . json ( ) ) ;
6989}
7090
7191export function createStudy ( caseExist , studyName , studyDescription , caseName , selectedFile ) {
7292 console . info ( "Creating a new study..." ) ;
7393 if ( caseExist ) {
74- const createStudyWithExistingCaseUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/cases/" + caseName + "?description=" + studyDescription ;
94+ const createStudyWithExistingCaseUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "/cases/" + caseName + "?description=" + studyDescription ;
7595 console . debug ( createStudyWithExistingCaseUrl ) ;
76- return fetch ( createStudyWithExistingCaseUrl , { method : 'post' } ) ;
96+ return backendFetch ( createStudyWithExistingCaseUrl , {
97+ method : 'post' ,
98+ } ) ;
7799 } else {
78- const createStudyWithNewCaseUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "?description=" + studyDescription ;
100+ const createStudyWithNewCaseUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName + "?description=" + studyDescription ;
79101 const formData = new FormData ( ) ;
80102 formData . append ( 'caseFile' , selectedFile ) ;
81103 console . debug ( createStudyWithNewCaseUrl ) ;
82- return fetch ( createStudyWithNewCaseUrl , {
104+ return backendFetch ( createStudyWithNewCaseUrl , {
83105 method : 'post' ,
84106 body : formData
85107 } )
@@ -88,16 +110,18 @@ export function createStudy(caseExist, studyName, studyDescription, caseName, se
88110
89111export function deleteStudy ( studyName ) {
90112 console . info ( "Deleting study" + studyName + " ..." ) ;
91- const deleteStudyUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName ;
113+ const deleteStudyUrl = PREFIX_STUDY_QUERIES + "/v1/studies/" + studyName ;
92114 console . debug ( deleteStudyUrl ) ;
93- return fetch ( deleteStudyUrl , { method :'delete' } ) ;
115+ return backendFetch ( deleteStudyUrl , {
116+ method :'delete'
117+ } ) ;
94118}
95119
96120export function renameStudy ( studyName , newStudyName ) {
97121 console . info ( "Renaming study " + studyName ) ;
98122 const renameStudiesUrl = process . env . REACT_APP_API_STUDY_SERVER + "/v1/studies/" + studyName + "/rename" ;
99123 console . debug ( renameStudiesUrl ) ;
100- return fetch ( renameStudiesUrl , {
124+ return backendFetch ( renameStudiesUrl , {
101125 method : 'POST' ,
102126 headers : {
103127 'Accept' : 'application/json' ,
0 commit comments