-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_service_integration.js
More file actions
76 lines (68 loc) · 2.05 KB
/
test_service_integration.js
File metadata and controls
76 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// paste this and run it into a browser console
async function testLogin() {
const gatekeeper_url = 'http://localhost:8001';
const url = `${gatekeeper_url}/api/login/`;
// User credentials in test database
const data = {
username: "admin",
password: "admin"
};
try {
// Send POST request to get the token
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(data)
});
if (response.ok) {
// Extract tokens from the JSON response
const tokens = await response.json();
const token = tokens.access;
console.log('Login successful! Access token:', token);
return token;
} else {
const errorData = await response.json();
console.error('Login failed:', errorData);
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
console.error('Error during login:', error);
}
}
async function fetchFarmData() {
const apiUrl = 'http://localhost:8001/api/proxy/farmcalendar/api/v1/Farm/';
try {
if (!window.testToken) {
throw new Error('No authentication token found. Please login first.');
}
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${window.testToken}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const farmData = await response.json();
console.log('Farm data:', farmData);
return farmData;
} else {
const errorData = await response.json();
console.error('Failed to fetch farm data:', errorData);
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
console.error('Error fetching farm data:', error);
}
}
// Run the function
testLogin().then(token => {
if (token) {
console.log('You can now use this token for authenticated requests');
// You can store it for subsequent requests
window.testToken = token;
fetchFarmData()
}
});