1+ // Creates a POST request.
2+ export function buildPostPayload ( data ) {
3+ return {
4+ method : 'POST' ,
5+ credentials : 'include' ,
6+ body : JSON . stringify ( data ) ,
7+ cache : 'no-cache' ,
8+ headers : new Headers ( {
9+ 'content-type' : 'application/json' ,
10+ } ) ,
11+ } ;
12+ }
13+
114// export makes this available for import from other files, e.g. App.js
215// async makes it so we can use "await" inside. It also makes it return a Promise.
316export async function getTemperature ( ) {
4- // if we got this page from localhost:3001, this will request localhost:3001/temperature
5- // await is kind of like a dotted line where the interpreter snips the function in two.
6- // Everything that would execute after the await keyword is shelved until the network
7- // request completes.
8- const response = await fetch ( '/api/getTemperature' )
9- // The same applies here - we make another dotted line between trying to read the response
10- // body as JSON and the remainder of the function
11- const data = await response . json ( )
12- console . log ( data )
13- // Remember that async makes this return a Promise. This return statement "resolves" the
14- // promise. If some other part of our code awaits getTemperature(), it will resume after
15- // after this return statement.
16- return JSON . stringify ( data )
17- // return await response.json()
17+ // if we got this page from localhost:3001, this will request localhost:3001/temperature
18+ // await is kind of like a dotted line where the interpreter snips the function in two.
19+ // Everything that would execute after the await keyword is shelved until the network
20+ // request completes.
21+ const response = await fetch ( '/api/getTemperature' ) ;
22+ // The same applies here - we make another dotted line between trying to read the response
23+ // body as JSON and the remainder of the function
24+ const data = await response . json ( ) ;
25+ console . log ( data ) ;
26+ // Remember that async makes this return a Promise. This return statement "resolves" the
27+ // promise. If some other part of our code awaits getTemperature(), it will resume after
28+ // after this return statement.
29+ return JSON . stringify ( data ) ;
30+ // return await response.json()
1831}
1932
2033export async function setTemperature ( input ) {
21- //need to pass in input variable into Flask server
22- console . log ( typeof input )
23- const response = await fetch ( '/api/setTemperature' , {
24- method :" POST" ,
25- credentials :" include" ,
26- body : JSON . stringify ( { 'temperature' : input . toString ( ) } ) ,
27- cache : " no-cache" ,
28- headers : new Headers ( {
29- " content-type" : " application/json"
30- } )
31- } )
32-
33- const data = await response . json ( )
34-
35- return JSON . stringify ( data )
34+ //need to pass in input variable into Flask server
35+ console . log ( typeof input ) ;
36+ const response = await fetch ( '/api/setTemperature' , {
37+ method : ' POST' ,
38+ credentials : ' include' ,
39+ body : JSON . stringify ( { temperature : input . toString ( ) } ) ,
40+ cache : ' no-cache' ,
41+ headers : new Headers ( {
42+ ' content-type' : ' application/json' ,
43+ } ) ,
44+ } ) ;
45+
46+ const data = await response . json ( ) ;
47+
48+ return JSON . stringify ( data ) ;
3649}
3750
3851export async function capture ( input ) {
39- const response = await fetch ( '/api/capture' , {
40- method :" POST" ,
41- credentials :" include" ,
42- body : JSON . stringify ( input ) ,
43- cache : " no-cache" ,
44- headers : new Headers ( {
45- " content-type" : " application/json"
46- } )
47- } )
48-
49- const data = await response . json ( )
50-
51- return data
52+ const response = await fetch ( '/api/capture' , {
53+ method : ' POST' ,
54+ credentials : ' include' ,
55+ body : JSON . stringify ( input ) ,
56+ cache : ' no-cache' ,
57+ headers : new Headers ( {
58+ ' content-type' : ' application/json' ,
59+ } ) ,
60+ } ) ;
61+
62+ const data = await response . json ( ) ;
63+
64+ return data ;
5265}
5366
5467export async function setFilter ( input ) {
55-
56- const response = await fetch ( '/api/setFilter' , {
57- method :"POST" ,
58- credentials :"include" ,
59- body : JSON . stringify ( input ) ,
60- cache : "no-cache" ,
61- headers : new Headers ( {
62- "content-type" : "application/json"
63- } )
64- } )
65-
66- const data = await response . json ( )
67-
68- return data
68+ const response = await fetch ( '/api/setFilter' , {
69+ method : 'POST' ,
70+ credentials : 'include' ,
71+ body : JSON . stringify ( input ) ,
72+ cache : 'no-cache' ,
73+ headers : new Headers ( {
74+ 'content-type' : 'application/json' ,
75+ } ) ,
76+ } ) ;
77+
78+ const data = await response . json ( ) ;
79+
80+ return data ;
6981}
7082
7183export async function getStatusTEC ( ) {
84+ const response = await fetch ( '/api/getStatusTEC' ) ;
7285
73- const response = await fetch ( '/api/getStatusTEC' )
86+ const data = await response . json ( ) ;
7487
75- const data = await response . json ( )
76-
77- return data
88+ return data ;
7889}
7990
8091export async function getStatus ( ) {
81- const response = await fetch ( '/api/getStatus' )
82- const data = await response . json ( )
83- return JSON . stringify ( data )
92+ const response = await fetch ( '/api/getStatus' ) ;
93+ const data = await response . json ( ) ;
94+ return JSON . stringify ( data ) ;
8495}
8596
86- export async function getFilterWheelStatus ( ) {
87-
88- const response = await fetch ( '/api/fw_test' )
97+ export async function getFilterWheel ( ) {
98+ const response = await fetch ( '/api/getFilterWheel' ) ;
99+ const data = await response . json ( ) ;
100+ return data ;
101+ }
89102
90- const data = await response . json ( )
103+ export async function setFilterWheel ( filter ) {
104+ const payload = buildPostPayload ( { filter } ) ;
105+ console . log ( payload ) ;
106+ const response = await fetch ( '/api/setFilterWheel' , payload ) ;
107+ const data = await response . json ( ) ;
108+ return data ;
109+ }
91110
92- return data
93- }
111+ export async function homeFilterWheel ( ) {
112+ const response = await fetch ( '/api/homeFilterWheel' ) ;
113+ const data = await response . json ( ) ;
114+ return data ;
115+ }
0 commit comments