scpsl_api/ip.rs
1//! This module contains functionality that can be used for
2//! working with the `ip` API request.
3
4use std::{
5 net::{AddrParseError, IpAddr},
6 str::FromStr,
7};
8use url::Url;
9
10/// An enum representing an error for the `ip` request.
11pub enum Error {
12 /// An enum variant representing [`AddrParseError`].
13 AddrParseError(AddrParseError),
14 /// An enum variant representing [`reqwest::Error`].
15 ReqwestError(reqwest::Error),
16}
17
18/// Returns current ip.
19/// # Errors
20/// Returns [`Error::AddrParseError`] if there was a returned ip address parse error.
21/// Returns [`Error::ReqwestError`] if there was a [`reqwest::Error`].
22pub async fn get(url: Url) -> Result<IpAddr, Error> {
23 match reqwest::get(url).await {
24 Ok(response) => match response.text().await {
25 Ok(text) => match IpAddr::from_str(text.as_str()) {
26 Ok(ip) => Ok(ip),
27 Err(error) => Err(Error::AddrParseError(error)),
28 },
29 Err(error) => Err(Error::ReqwestError(error)),
30 },
31 Err(error) => Err(Error::ReqwestError(error)),
32 }
33}