-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgupyapi.ts
More file actions
87 lines (75 loc) · 2.67 KB
/
Copy pathgupyapi.ts
File metadata and controls
87 lines (75 loc) · 2.67 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
77
78
79
80
81
82
83
84
85
86
87
import * as fs from 'fs/promises';
import * as dotenv from 'dotenv';
dotenv.config();
const apiKey = process.env.GUPY_API_KEY;
const BEARER_TOKEN = apiKey
const API1_URL = 'https://api.gupy.io/api/v1/jobs';
const API2_BASE_URL = 'https://api.gupy.io/api/v1/jobs';
interface ApiResponse {
results: any[]; // Adjust this based on the actual structure
}
async function fetchDataFromAPI1(): Promise<any[]> {
const myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${BEARER_TOKEN}`);
const requestOptions: RequestInit = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
try {
const response = await fetch(API1_URL, requestOptions);
const data = await response.json() as ApiResponse; // Type assertion here
return data.results;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error('Error fetching data from API1:', errorMessage);
throw error;
}
}
async function fetchDataFromAPI2(jobId: number): Promise<any[]> {
const API2_URL = `${API2_BASE_URL}/${jobId}/applications`;
const myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${BEARER_TOKEN}`);
const requestOptions: RequestInit = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
try {
const response = await fetch(API2_URL, requestOptions);
const data = await response.json() as any[]; // Type assertion here
return data;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error(`Error fetching data from API2 for jobId ${jobId}:`, errorMessage);
throw error;
}
}
async function main() {
try {
// Fetch data from API1
const jobsData = await fetchDataFromAPI1();
// Process data for each job
const applicationsData: Array<{ job: any; applications: any }> = await Promise.all(
jobsData.map(async (job) => {
const applications = await fetchDataFromAPI2(job.id);
return { job, applications };
})
);
await saveDataToFile('output.json', applicationsData);
console.log('Data saved to output.json');
} catch (error: any) {
console.error('An error occurred:', error.message);
}
}
async function saveDataToFile(filename: string, data: any): Promise<void> {
try {
await fs.writeFile(filename, JSON.stringify(data, null, 2));
} catch (error) {
const errorMessage = (error instanceof Error) ? error.message : 'Unknown error';
console.error('Error saving data to file:', errorMessage);
throw error;
}
}
// Run the main function
main();