diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 10 | ||||
-rw-r--r-- | src/client.rs | 27 | ||||
-rw-r--r-- | src/error/mod.rs | 26 | ||||
-rw-r--r-- | src/lib.rs | 5 |
5 files changed, 70 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f3f4bf6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "inwx" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwest = { version = "0.11", features = ["blocking", "cookies"] } +url = "2.2.2" diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..f0a06f3 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,27 @@ +use crate::{Error, Result}; + +use reqwest::Url; + +/// The INWX environment to use. The Sandbox is good for testing +/// or debugging purposes. +pub enum Endpoint { + Production, + Sandbox, +} + +impl From<Endpoint> for &str { + fn from(endpoint: Endpoint) -> &'static str { + match endpoint { + Endpoint::Production => "https://api.domrobot.com/xmlrpc/", + Endpoint::Sandbox => "https://api.ote.domrobot.com/xmlrpc/", + } + } +} + +impl TryInto<Url> for Endpoint { + type Error = Error; + fn try_into(self) -> Result<Url> { + let url = Url::parse(self.into())?; + Ok(url) + } +} diff --git a/src/error/mod.rs b/src/error/mod.rs new file mode 100644 index 0000000..8c45015 --- /dev/null +++ b/src/error/mod.rs @@ -0,0 +1,26 @@ +use std::fmt; + +/// The Errors that may occur around Clients. +#[derive(Debug)] +pub enum Error { + ParseUrl(url::ParseError), +} + +impl std::error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::ParseUrl(err) => write!(fmt, "can't parse Url: {}", err), + } + } +} + +impl From<url::ParseError> for Error { + fn from(err: url::ParseError) -> Self { + Self::ParseUrl(err) + } +} + +/// A `Result` alias where the `Err` case is `inwx::Error`. +pub type Result<T> = std::result::Result<T, Error>; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f4687c8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub mod client; +pub mod error; + +pub use client::Endpoint; +pub use error::{Error, Result}; |