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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extern crate chrono;
#[macro_use]
extern crate hyper;
extern crate reqwest;
extern crate xml;
extern crate xmltree;

mod rpser;
mod http;
mod transforms;
pub mod models;
pub mod error;

use std::result;

use rpser::Method;

pub use models::partner::Partner;
pub use models::nds_response::NdsResponse;

pub use transforms::FromElement;

/// The connection point of the service
const V2_API_RPC_PATH: &'static str = "http://npchk.nalog.ru:80/FNSNDSCAWS_2";
const V2_API_REQUEST: &'static str = "http://ws.unisoft/FNSNDSCAWS2/Request";
const V2_API_NAMESPACE: &'static str = "req";

/// Checks of contractors through the service
/// [http://npchk.nalog.ru/](http://npchk.nalog.ru/)
pub fn check_fns(partners: Vec<Partner>) -> Result<NdsResponse> {
    use self::rpser::xml::BuildElement;
    use xmltree::Element;

    if partners.len() > 10_000 {
        return Err(error::Error::TooManyRecords);
    }

    let mut nds_request2 = Method::new("NdsRequest2");
    for elem in partners {
        nds_request2 = nds_request2.with(
            Element::node(format!("{}:{}", V2_API_NAMESPACE, "NP"))
                .with_attr("INN", elem.inn)
                .with_attr("KPP", elem.kpp)
                .with_attr("DT", elem.dt.format("%d.%m.%Y").to_string()),
        );
    }

    let response = call(nds_request2)?;

    Ok(NdsResponse::from_element(response.body)?)
}

/// Checks the 1st of the contractor using the service
/// [http://npchk.nalog.ru/](http://npchk.nalog.ru/)
pub fn check_fns_partner(p: Partner) -> Result<NdsResponse> {
    let mut partners: Vec<Partner> = vec![];
    partners.push(p);

    check_fns(partners)
}

/// Calls a remote procedure through a Protocol `SOAP`
fn call(method: rpser::Method) -> Result<rpser::Response> {
    let envelope = method.as_xml(V2_API_REQUEST, V2_API_NAMESPACE);

    let http_response = http::soap_action(V2_API_RPC_PATH, &method.name, &envelope)?;

    Ok(rpser::Response::from_xml(&http_response.body)?)
}

pub type Result<T> = result::Result<T, error::Error>;

#[cfg(test)]
mod tests {}