Browse Source

initial commit

Steven Jacobs 2 years ago
commit
84264a20ec
3 changed files with 157 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 12 0
      Cargo.toml
  3. 142 0
      src/lib.rs

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+target/
+**/*.rs.bk
+Cargo.lock

+ 12 - 0
Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "linode"
+version = "0.1.0"
+authors = ["Steven Jacobs <stjacobs@fastmail.fm>"]
+
+[dependencies]
+hyper = "0.10"
+reqwest = "0.5"
+serde = "0.9"
+serde_derive = "0.9"
+serde_json = "0.9"
+url = "*"

+ 142 - 0
src/lib.rs

@@ -0,0 +1,142 @@
+extern crate hyper;
+extern crate url;
+
+#[macro_use]
+extern crate serde_derive;
+
+pub mod linode {
+    pub mod instances {
+        extern crate reqwest;
+        extern crate hyper;
+        extern crate url;
+
+        use helper;
+        use hyper::header::Headers;
+        use url::Url;
+
+        #[derive(Debug, Deserialize)]
+        pub struct Instance {
+            pub id: u32,
+            pub group: String,
+            pub label: String,
+            pub status: String,
+            pub hypervisor: String,
+            pub ipv4: Vec<String>,
+        }
+
+        #[derive(Debug, Deserialize)]
+        struct ResponseInstances {
+            page: u32,
+            total_results: u32,
+            linodes: Vec<Instance>,
+            total_pages: u32,
+        }
+
+        pub fn index() -> Vec<Instance> {
+            let mut res = helper::gen_request("get", "linode/instances");
+
+            let results: ResponseInstances = res.json().expect("Can't parse API data");
+
+            results.linodes
+        }
+
+        pub fn get(id: &u32) -> Instance {
+            let client = reqwest::Client::new().expect("client failed to construct");
+            let headers: Headers = helper::gen_headers();
+            let url: Url = helper::gen_url(format!("linode/instances/{}", id).as_str());
+
+            let mut res = client.get(url)
+                .headers(headers)
+                .send()
+                .unwrap();
+
+            res.json().expect("Can't parse API data")
+        }
+    }
+
+    pub mod types {
+        extern crate reqwest;
+        extern crate hyper;
+        extern crate url;
+
+        use helper;
+
+        #[derive(Debug, Deserialize)]
+        pub struct Type {
+            pub ram: u32,
+            pub class: String,
+            pub backups_price: u32,
+            pub storage: u32,
+            pub label: String,
+            pub monthly_price: u32,
+            pub hourly_price: u32,
+            pub vcpus: u32,
+            pub transfer: u32,
+            pub mbits_out: u32,
+            pub id: String,
+        }
+
+        #[derive(Debug, Deserialize)]
+        struct ResponseTypes {
+            page: u32,
+            total_results: u32,
+            types: Vec<Type>,
+            total_pages: u32,
+        }
+
+        pub fn index() -> Vec<Type> {
+            let mut res = helper::gen_request("get", "linode/types");
+
+            let results: ResponseTypes = res.json().expect("Can't parse API data");
+
+            results.types
+        }
+    }
+}
+
+
+pub mod helper {
+    extern crate hyper;
+    extern crate reqwest;
+    extern crate url;
+
+    use std;
+    use hyper::header::{Headers, Authorization, UserAgent};
+    use url::Url;
+
+    pub fn get_token() -> &'static str {
+        env!("LINODE_API_TOKEN")
+    }
+
+    pub fn gen_url(path: &str) -> Url {
+        let mut url = std::string::String::new();
+        url.push_str("https://api.alpha.linode.com/v4/");
+        url.push_str(path);
+        Url::parse(&url).unwrap()
+    }
+
+    pub fn gen_headers() -> Headers {
+        let token = get_token();
+        let mut headers = Headers::new();
+
+        // TODO: use environment variables for user-agent
+        headers.set(UserAgent("linode-curses/0.1.0".to_owned()));
+        headers.set(Authorization(format!("token {}", token).to_owned()));
+
+        headers
+    }
+
+    pub fn gen_request(method: &str, path: &str) -> reqwest::Response {
+        let client = reqwest::Client::new().expect("client failed to construct");
+        let headers: Headers = gen_headers();
+        let url: Url = gen_url(path);
+
+        match method {
+            "get" => client.get(url).headers(headers).send().unwrap(),
+            "put" => client.put(url).headers(headers).send().unwrap(),
+            "post" => client.post(url).headers(headers).send().unwrap(),
+            "delete" => client.delete(url).headers(headers).send().unwrap(),
+            _ => panic!("method not supported"),
+        }
+    }
+}