/
WA.cs
118 lines (106 loc) · 4.72 KB
/
WA.cs
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
using Flurl.Http;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace NumberSearch.DataAccess
{
// These XML serialization classes were autogenerated by the Edit => Paste Special => Paste XML as Classes command in VS 2019.
// Then I cleaned them up to remove the complex getter and setters and the duplicate private fields they required.
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", ElementName = "response", IsNullable = false)]
public class SalesTax
{
public Addressline addressline { get; set; }
public TaxRate rate { get; set; }
[XmlAttribute()]
public int loccode { get; set; }
[XmlAttribute()]
public decimal localrate { get; set; }
[XmlAttribute("rate")]
public decimal rate1 { get; set; }
[XmlAttribute()]
public int code { get; set; }
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public partial class Addressline
{
[XmlAttribute()]
public int code { get; set; }
[XmlAttribute()]
public string street { get; set; }
[XmlAttribute()]
public int househigh { get; set; }
[XmlAttribute()]
public int houselow { get; set; }
[XmlAttribute()]
public string evenodd { get; set; }
[XmlAttribute()]
public string state { get; set; }
[XmlAttribute()]
public int zip { get; set; }
[XmlAttribute()]
public int plus4 { get; set; }
[XmlAttribute()]
public string period { get; set; }
[XmlAttribute()]
public string rta { get; set; }
[XmlAttribute()]
public string ptba { get; set; }
[XmlAttribute()]
public string cez { get; set; }
}
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public partial class TaxRate
{
[XmlAttribute()]
public string name { get; set; }
[XmlAttribute()]
public int code { get; set; }
[XmlAttribute()]
public decimal staterate { get; set; }
[XmlAttribute()]
public decimal localrate { get; set; }
}
/// <summary>
/// Get the total Sale Tax rate for a specific address.
/// </summary>
/// <param name="streetAddress"> A valid street address (ex. 6500 Linderson way) </param>
/// <param name="city"> A valid City name (ex. Seattle) </param>
/// <param name="zip"> A valid Zip code inside Washington State (ex. 98501) </param>
/// <returns></returns>
public static async Task<decimal> GetTaxRateAsync(string streetAddress, string city, string zip)
{
string baseUrl = "https://webgis.dor.wa.gov/webapi/";
string endpoint = "AddressRates.aspx";
string outputParameter = $"?output=xml";
string addrParameter = $"&addr={streetAddress}";
string cityParameter = $"&city={city}";
string zipParameter = $"&zip={zip}";
string url = $"{baseUrl}{endpoint}{outputParameter}{addrParameter}{cityParameter}{zipParameter}";
var result = await url.GetStreamAsync().ConfigureAwait(false);
var serializer = new XmlSerializer(typeof(SalesTax));
var apiResponse = (SalesTax)serializer.Deserialize(result);
return apiResponse.rate1;
}
public static async Task<SalesTax> GetAsync(string streetAddress, string city, string zip)
{
string baseUrl = "https://webgis.dor.wa.gov/webapi/";
string endpoint = "AddressRates.aspx";
string outputParameter = $"?output=xml";
string addrParameter = $"&addr={streetAddress}";
string cityParameter = $"&city={city}";
string zipParameter = $"&zip={zip}";
string url = $"{baseUrl}{endpoint}{outputParameter}{addrParameter}{cityParameter}{zipParameter}";
var result = await url.GetStreamAsync().ConfigureAwait(false);
// Learn more about this XML serialization method: https://docs.microsoft.com/en-us/dotnet/standard/serialization/how-to-deserialize-an-object
var serializer = new XmlSerializer(typeof(SalesTax));
var apiResponse = (SalesTax)serializer.Deserialize(result);
return apiResponse;
}
}
}