1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! WSDL inspection helpers.

use std::collections::HashMap;
use xml::reader::{EventReader, XmlEvent};
use http;

/// WSDL operation info.
#[derive(Debug)]
pub struct Operation {
    pub url: String,
}

/// WSDL document.
#[derive(Debug)]
pub struct Wsdl {
    pub operations: HashMap<String, Operation>,
}

/// Fetch WSDL from specified URL and store results in `Wsdl` structure.
pub fn fetch(url: &str) -> http::Result<Wsdl> {
    let response = try!(http::get(&url));
    let mut bytes = response.body.as_bytes();

    let mut operations = HashMap::new();

    let parser = EventReader::new(&mut bytes);
    for e in parser {
        match e {
            Ok(XmlEvent::StartElement { ref name, ref attributes, ref namespace }) => {
                if name.to_string().contains("wsdl:operation") {
                    match (attributes.iter().find(|a| a.name.to_string() == "name"), namespace.get("impl")) {
                        (Some(name_attribute), Some(impl_url)) => {
                            operations.insert(name_attribute.value.to_string(), Operation {
                                url: impl_url.into(),
                            });
                        },
                        _ => (),
                    }
                    //println!("{}+{}+{:?}+{:?}", indent(depth), name, attributes, namespace);
                }
            }
            // Ok(XmlEvent::EndElement { name }) => {
            //     println!("{}-{}", indent(depth), name);
            // }
            Err(e) => {
                error!("Error: {}", e);
                break;
            }
            _ => {}
        }
    }

    Ok(Wsdl {
        operations: operations
    })
}