Skip to content

Commit 16140ef

Browse files
chore: add different request types to APM -> NetworkScreen.tsx
1 parent 1a542cb commit 16140ef

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

examples/default/src/screens/apm/NetworkScreen.tsx

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import axios from 'axios';
1212
export const NetworkScreen: React.FC = () => {
1313
const [endpointUrl, setEndpointUrl] = useState('');
1414
const { width, height } = useWindowDimensions();
15-
const defaultRequestUrl = 'https://jsonplaceholder.typicode.com/posts/1';
15+
const defaultRequestBaseUrl = 'https://jsonplaceholder.typicode.com/posts/';
16+
const shortenLink = 'https://shorturl.at/3Ufj3';
17+
const defaultRequestUrl = `${defaultRequestBaseUrl}1`;
18+
1619
const imageUrls = [
1720
'https://fastly.picsum.photos/id/57/200/300.jpg?hmac=l908G1qVr4r7dP947-tak2mY8Vvic_vEYzCXUCKKskY',
1821
'https://fastly.picsum.photos/id/619/200/300.jpg?hmac=WqBGwlGjuY9RCdpzRaG9G-rc9Fi7TGUINX_-klAL2kA',
1922
];
2023

2124
async function sendRequestToUrl() {
22-
let urlToSend = '';
25+
let urlToSend: string;
2326

2427
if (endpointUrl.trim() !== '') {
2528
urlToSend = endpointUrl;
@@ -47,7 +50,7 @@ export const NetworkScreen: React.FC = () => {
4750
}
4851

4952
async function sendRequestToUrlUsingAxios() {
50-
let urlToSend = '';
53+
let urlToSend: string;
5154

5255
if (endpointUrl.trim() !== '') {
5356
urlToSend = endpointUrl;
@@ -72,6 +75,26 @@ export const NetworkScreen: React.FC = () => {
7275
}
7376
}
7477

78+
async function sendRedirectRequestToUrl() {
79+
try {
80+
console.log('Sending request to: ', shortenLink);
81+
const response = await fetch(shortenLink);
82+
console.log('Received from: ', response.url);
83+
84+
// Format the JSON response for better logging
85+
const data = await response.json();
86+
87+
// Format the JSON response for better logging
88+
const formattedData = JSON.stringify(data, null, 2);
89+
90+
// Log the formatted response
91+
console.log('Response:', formattedData);
92+
} catch (error) {
93+
// Handle errors appropriately
94+
console.error('Error:', error);
95+
}
96+
}
97+
7598
const fetchGraphQlData = async () => {
7699
const document = gql`
77100
query {
@@ -90,6 +113,40 @@ export const NetworkScreen: React.FC = () => {
90113

91114
const { data, isError, isSuccess, isLoading, refetch } = useQuery('helloQuery', fetchGraphQlData);
92115

116+
function generateUrls(count: number = 10) {
117+
const urls = [];
118+
for (let i = 1; i <= count; i++) {
119+
urls.push(defaultRequestBaseUrl + i);
120+
}
121+
return urls;
122+
}
123+
124+
async function makeSequentialApiCalls(urls: string[]): Promise<any[]> {
125+
// const fetchPromises = urls.map((url) => fetch(url).then((response) => response.json()));
126+
const results: any[] = [];
127+
128+
try {
129+
for (let i = 0; i < urls.length; i++) {
130+
await fetch(urls[i]);
131+
results.push(results[i]);
132+
}
133+
return results;
134+
} catch (error) {
135+
console.error('Error making parallel API calls:', error);
136+
throw error;
137+
}
138+
}
139+
async function makeParallelApiCalls(urls: string[]): Promise<any[]> {
140+
const fetchPromises = urls.map((url) => fetch(url).then((response) => response.json()));
141+
142+
try {
143+
return await Promise.all(fetchPromises);
144+
} catch (error) {
145+
console.error('Error making parallel API calls:', error);
146+
throw error;
147+
}
148+
}
149+
93150
return (
94151
<ScrollView>
95152
<Screen>
@@ -102,11 +159,29 @@ export const NetworkScreen: React.FC = () => {
102159
value={endpointUrl}
103160
/>
104161
<CustomButton onPress={sendRequestToUrl} title="Send Request To Url" />
162+
<CustomButton
163+
onPress={sendRedirectRequestToUrl}
164+
title="Send Redirection Request To Url"
165+
/>
105166
<CustomButton
106167
onPress={sendRequestToUrlUsingAxios}
107168
title="Send Request To Url Using Axios"
108169
/>
109170

171+
<CustomButton
172+
onPress={() => makeParallelApiCalls(generateUrls())}
173+
title="Send Parallel Requests"
174+
/>
175+
<CustomButton
176+
onPress={() => makeSequentialApiCalls(generateUrls())}
177+
title="Send Sequantail Requests"
178+
/>
179+
180+
<CustomButton
181+
onPress={() => makeSequentialApiCalls(generateUrls())}
182+
title="Send Sequantail Requests"
183+
/>
184+
110185
<CustomButton onPress={() => refetch} title="Reload GraphQL" />
111186
<View>
112187
{isLoading && <Text>Loading...</Text>}

0 commit comments

Comments
 (0)