]> git.proxmox.com Git - proxmox-backup.git/commitdiff
move parse_query into json_schema
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 5 Nov 2018 14:20:27 +0000 (15:20 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 5 Nov 2018 14:20:27 +0000 (15:20 +0100)
src/json_schema.rs
src/main.rs

index 6e7d7eb60548c2d16e0fbd304f1f450a0ab1df8e..737b58055c584dad050f7e026eb63b62e532557e 100644 (file)
@@ -1,4 +1,7 @@
+use failure::*;
 use std::collections::HashMap;
+use url::form_urlencoded;
+use serde_json::{json, Value};
 
 pub type PropertyMap = HashMap<&'static str, Jss>;
 
@@ -39,7 +42,7 @@ pub struct JssObject {
     pub description: &'static str,
     pub optional: Option<bool>,
     pub additional_properties: Option<bool>,
-    pub properties: Box<HashMap<&'static str, Jss>>,
+    pub properties: HashMap<&'static str, Jss>,
 }
 
 #[derive(Debug)]
@@ -107,7 +110,7 @@ macro_rules! parameter {
                 $(
                     map.insert(stringify!($name), $e);
                 )*
-                Box::new(map)
+                map
             }
         };
 
@@ -116,6 +119,20 @@ macro_rules! parameter {
 }
 
 
+pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss) -> Result<Value, Error> {
+
+    println!("QUERY Strings {:?}", data);
+
+    Ok(json!(null))
+}
+
+pub fn parse_query(query: &str, schema: &Jss) -> Result<Value, Error> {
+
+    let raw_param: Vec<(String, String)> =
+        form_urlencoded::parse(query.as_bytes()).into_owned().collect();
+
+    parse_parameter_strings(&raw_param, schema)
+}
 
 #[test]
 fn test_shema1() {
index 9f203dbee0d552b04663027fd1fd32b061cb93a8..f22613662dd03cab68632951f2d818ba75c3ba5e 100644 (file)
@@ -1,12 +1,13 @@
 extern crate apitest;
 
-//use failure::*;
+use failure::*;
 
 use std::collections::HashMap;
 use lazy_static::lazy_static;
 
 //use apitest::json_schema::*;
 use apitest::api_info::*;
+use apitest::json_schema::*;
 
 //use serde_derive::{Serialize, Deserialize};
 use serde_json::{json, Value};
@@ -25,20 +26,6 @@ macro_rules! http_error {
     }}
 }
 
-fn parse_query(query: &str) -> Value {
-
-    println!("PARSE QUERY {}", query);
-
-    // fixme: what about repeated parameters (arrays?)
-    let mut raw_param = HashMap::new();
-    for (k, v) in form_urlencoded::parse(query.as_bytes()) {
-        println!("QUERY PARAM {} value {}", k, v);
-        raw_param.insert(k, v);
-    }
-    println!("QUERY HASH {:?}", raw_param);
-
-    return json!(null);
-}
 
 fn handle_request(req: Request<Body>) -> Response<Body> {
 
@@ -63,9 +50,9 @@ fn handle_request(req: Request<Body>) -> Response<Body> {
                 println!("FOUND INFO");
                 let api_method_opt = match method {
                     &Method::GET => &info.get,
-                  //  &Method::PUT => info.put,
-                  //  &Method::POST => info.post,
-                  //  &Method::DELETE => info.delete,
+                    &Method::PUT => &info.put,
+                    &Method::POST => &info.post,
+                    &Method::DELETE => &info.delete,
                     _ => &None,
                 };
                 let api_method = match api_method_opt {
@@ -77,7 +64,12 @@ fn handle_request(req: Request<Body>) -> Response<Body> {
 
                 // extract param
                 let param = match query {
-                    Some(data) => parse_query(data),
+                    Some(data) => {
+                        match parse_query(data, &api_method.parameters) {
+                            Ok(query) => query,
+                            Err(err) => http_error!(NOT_FOUND, format!("Unable to parse query parameters '{}' - {}", data, err)),
+                        }
+                    }
                     None => json!({}),
                 };