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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! This library can be used to get tax rates for addresses in WA state! Meant to be super simple.
//! 
//! It gets data from DOR using its [XML URL interface defined here](https://dor.wa.gov/find-taxes-rates/retail-sales-tax/destination-based-sales-tax-and-streamlined-sales-tax/wa-sales-tax-rate-lookup-url-interface).
//! 
//! Note that this needs [`tokio`](https://crates.io/crates/tokio), as [`reqwest`](https://crates.io/crates/reqwest) needs `tokio`!

#[macro_use]
extern crate log;

use reqwest::Error as ReqwestError;
use std::convert::TryFrom;
use strong_xml::{XmlRead, XmlWrite};
use url::form_urlencoded;


const DOR_ADDR_PREFIX: &'static str = "https://webgis.dor.wa.gov/webapi/AddressRates.aspx?output=xml";

/// These codes are taken from [the DOR spec](https://dor.wa.gov/find-taxes-rates/retail-sales-tax/destination-based-sales-tax-and-streamlined-sales-tax/wa-sales-tax-rate-lookup-url-interface);
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Code {
    AddrFound,
    AddrNotFoundZipFound,
    AdrrUpdatedAndFoundValidate,
    AddrUpdatedAndZipFoundValidate,
    AddrCorrectedAndFoundValidate,
    Zip5FoundNoAddrOrZip4,
    NoAddrNoZips,
    InvalidLongLat,
    InternalError,
}

impl Code {
    /// True when the returned values are garbage, as in the tax rate could be -1 or something
    pub fn is_error(&self) -> bool {
        use Code::*;
        match self {
            NoAddrNoZips => true,
            InvalidLongLat => true,
            InternalError => true,
            _ => false,
        }
    }
    pub fn retryable(&self) -> bool {
        false
    }
}

use std::str::FromStr;

impl FromStr for Code {
    type Err = &'static str;
    fn from_str(s: &std::primitive::str) -> Result<Self, Self::Err> {
        let num: u8 = s.parse().map_err(|_| "did not use integer as code")?;
        Self::try_from(num)
    }
}

impl TryFrom<u8> for Code {
    type Error = &'static str;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use Code::*;
        Ok(match value {
            0 => AddrFound,
            1 => AddrNotFoundZipFound,
            2 => AdrrUpdatedAndFoundValidate,
            3 => AddrUpdatedAndZipFoundValidate,
            4 => AddrCorrectedAndFoundValidate,
            5 => Zip5FoundNoAddrOrZip4,
            6 => NoAddrNoZips,
            7 => InvalidLongLat,
            9 => InternalError,
            _ => return Err("code provided not valid"),
        })
    }
}

/// Error retreiving tax info. DOR errors most likely mean bad input, as in a weird address
#[derive(Debug)]
pub enum TaxInfoError {
    Http(ReqwestError),
    /// DOR gave a code that means there as an error. We return the raw TaxInfo object in case
    /// you'd like to inspect it
    Dor((Code, TaxInfo)),
    Internal(&'static str),
    NoMoreRetries,
}

impl TaxInfoError {
    pub fn retryable(&self) -> bool {
        match self {
            TaxInfoError::NoMoreRetries => false,
            TaxInfoError::Dor((code,  _)) => code.retryable(),
            TaxInfoError::Http(re) => re.status().map(|s| {
                s.is_server_error()
            }).unwrap_or(true),
            TaxInfoError::Internal(_) => false,
        }
    }
}

impl From<ReqwestError> for TaxInfoError {
    fn from(re: ReqwestError) -> Self {
        Self::Http(re)
    }
}


/// The Address parsed by DOR, returned as part of TaxInfo
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "addressline")]
pub struct Address {
    #[xml(attr = "househigh")]
    pub househigh: Option<u32>,
    #[xml(attr = "houselow")]
    pub houselow: Option<u32>,
    #[xml(attr = "evenodd")]
    pub evenodd: Option<String>,
    #[xml(attr = "street")]
    pub street: Option<String>,
    #[xml(attr = "zip")]
    pub zip: Option<u32>,
    #[xml(attr = "plus4")]
    pub plus4: Option<u32>,
    #[xml(attr = "period")]
    pub period: Option<String>,
    #[xml(attr = "rta")]
    pub rta: Option<String>,
    #[xml(attr = "ptba")]
    pub ptba: Option<String>,
    #[xml(attr = "cez")]
    pub cez: Option<String>,
}

/// Tax Rate information, returned as part of TaxInfo
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "rate")]
pub struct TaxRate {
    #[xml(attr = "name")]
    pub name: String,
    #[xml(attr = "code")]
    pub code: String,
    #[xml(attr = "localrate")]
    pub localrate: f32,
    #[xml(attr = "staterate")]
    pub staterate: f32,
}


/// Tax Info provided by WA State DOR
/// 
/// See [the DOR website](https://dor.wa.gov/find-taxes-rates/retail-sales-tax/destination-based-sales-tax-and-streamlined-sales-tax/wa-sales-tax-rate-lookup-url-interface) for specifics.
#[derive(XmlRead, PartialEq, Debug)]
#[xml(tag = "response")]
pub struct TaxInfo {
    #[xml(attr = "loccode")]
    pub loccode: i32,
    #[xml(attr = "rate")]
    pub rate: f32,
    #[xml(attr = "code")]
    pub code: Code,
    #[xml(attr = "localrate")]
    pub localrate: f32,
    #[xml(attr = "debughint")]
    pub debughint: Option<String>,
    // Children
    #[xml(child = "addressline")]
    pub address: Option<Address>,
    #[xml(child = "rate")]
    pub taxrate: Option<TaxRate>,
}

const MAX_ATTEMPTS: usize = 3;
const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(7_000);

/// Has retries, reasonable timeouts, defaults, fully ready to go.
pub async fn get(addr: &str, city: &str, zip: &str) -> Result<TaxInfo, TaxInfoError> {
    let mut remaining_attempts = MAX_ATTEMPTS;
    while remaining_attempts > 0 {
        remaining_attempts -= 1;
        match tokio::time::timeout(DEFAULT_TIMEOUT, get_basic(addr, city, zip)).await {
            Ok(Ok(r)) => return Ok(r),
            Ok(Err(e)) => if !e.retryable() {
                return Err(e);
            }
            Err(_) => {
                // continue
            }
        }
    }
    Err(TaxInfoError::NoMoreRetries)
}

/// No retries, just one attempt, no timeout, nothing
pub async fn get_basic(addr: &str, city: &str, zip: &str) -> Result<TaxInfo, TaxInfoError> {
    let request: String = form_urlencoded::Serializer::new(DOR_ADDR_PREFIX.to_string())
        .append_pair("addr", addr)
        .append_pair("city", city)
        .append_pair("zip", zip)
        .finish();

    debug!("URL to GET from dor {}", request);
    let raw_string = reqwest::get(&request).await?.text().await?;

    debug!("raw string from DOR {}", raw_string);

    match TaxInfo::from_str(&raw_string) {
        Ok(rti) => {
            if rti.code.is_error() {
                Err(TaxInfoError::Dor((rti.code, rti)))
            } else {
                Ok(rti)
            }
        }
        Err(_e) => Err(TaxInfoError::Internal("Error parsing response from DOR")),
    }
}