|
| 1 | +import { Meta, Description } from '@storybook/addon-docs' |
| 2 | + |
| 3 | +<Meta |
| 4 | + title="HTTP CAlls/get internet status" |
| 5 | + parameters={{ |
| 6 | + viewMode: 'docs', |
| 7 | + previewTabs: { |
| 8 | + canvas: { hidden: true } |
| 9 | + }, |
| 10 | + }} |
| 11 | +/> |
| 12 | + |
| 13 | +# Get Internet Status |
| 14 | + |
| 15 | +### API CALL: getInternetStatus |
| 16 | + |
| 17 | +#### Overview |
| 18 | +The `getInternetStatus` function checks if the device has an active internet connection by attempting to reach Google's homepage. It returns a boolean value indicating whether the internet is accessible. |
| 19 | + |
| 20 | +#### Imports |
| 21 | +```javascript |
| 22 | +import axios, { AxiosRequestConfig } from 'axios'; |
| 23 | +import { Sid } from '..'; |
| 24 | +``` |
| 25 | +- **axios**: Used for making HTTP requests. |
| 26 | +- **AxiosRequestConfig**: Type used for configuring the axios request. |
| 27 | +- **Sid**: A type imported from the parent directory, presumably representing session ID or similar credentials. |
| 28 | + |
| 29 | +#### Parameters |
| 30 | +- `sid`: A session identifier of type `Sid`, required to authenticate or manage the session context for the request. |
| 31 | + |
| 32 | +#### Returns |
| 33 | +- **Promise of boolean**: ```Promise<boolean>``` A promise that resolves to `true` if the internet is reachable, otherwise `false`. |
| 34 | + |
| 35 | +#### Function Logic |
| 36 | +1. **Prepare the Request:** |
| 37 | + - The function constructs a JSON string that includes the command to check internet connectivity by using `wget` to quietly fetch Google's homepage. |
| 38 | + - The command will print 'true' if the fetch is successful, and 'false' otherwise. |
| 39 | + |
| 40 | + ```javascript |
| 41 | + const data = JSON.stringify({ |
| 42 | + method: 'exec', |
| 43 | + params: ["wget -q --spider http://www.google.com 2>/dev/null && echo 'true' || echo 'false'"], |
| 44 | + }); |
| 45 | + ``` |
| 46 | + |
| 47 | +2. **Configure the Request:** |
| 48 | + - An axios configuration object is set up, specifying the HTTP method, URL endpoint, headers, data, and timeout for the request. |
| 49 | + |
| 50 | + ```javascript |
| 51 | + const config: AxiosRequestConfig = { |
| 52 | + method: 'post', |
| 53 | + url: '/cgi-bin/luci/rpc/sys', |
| 54 | + headers: { |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + }, |
| 57 | + data: data, |
| 58 | + timeout: 4000, |
| 59 | + }; |
| 60 | + ``` |
| 61 | + |
| 62 | +3. **Execute the Request and Handle Response:** |
| 63 | + - The axios library is used to send the configured request asynchronously. |
| 64 | + - If the HTTP response status is not 200, an error is thrown. |
| 65 | + - The function checks if the response contains 'true' within the result field to determine internet connectivity. |
| 66 | + |
| 67 | + ```javascript |
| 68 | + const response = await axios(config); |
| 69 | + if (response.status !== 200) { |
| 70 | + throw new Error('Error fetching getWifiConfigFile'); |
| 71 | + } |
| 72 | + return response.data.result.includes('true'); |
| 73 | + ``` |
| 74 | + |
| 75 | +4. **Error Handling:** |
| 76 | + - The catch block handles various errors such as axios cancellation due to timeout, server responses indicating failure, no response received, or other axios errors. |
| 77 | + - In any error scenario, the function returns `false`, indicating no internet connectivity. |
| 78 | + |
| 79 | + ```javascript |
| 80 | + catch (error: any) { |
| 81 | + if (axios.isCancel(error)) { |
| 82 | + // Request was cancelled due to timeout |
| 83 | + return false; |
| 84 | + } else if (error.response) { |
| 85 | + // Server responded with an error |
| 86 | + return false; |
| 87 | + } else if (error.request) { |
| 88 | + // No response was received |
| 89 | + return false; |
| 90 | + } else { |
| 91 | + // Other errors |
| 92 | + console.log('Error:', error.message); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + ``` |
| 97 | + |
| 98 | +#### Usage Example |
| 99 | +```javascript |
| 100 | +const isConnected = await getInternetStatus({ sid: yourSessionId }) |
| 101 | +``` |
0 commit comments