>> results)
71 | {
72 | Assert.That(results[0].Result.Any(), Is.True);
73 | Assert.That(results[1].Result.Any(), Is.True);
74 | Assert.That(results.Single(r => r.Query.Equals(_lookups[0])).Result.Exists(p => p.Postcode == "GU1 1AA"), Is.True);
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------
/tools/phantom/License.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Microsoft Public License (Ms-PL)
5 |
6 |
7 |
8 | Microsoft Public License (Ms-PL)
9 |
10 | This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
11 |
12 | The software is governed by the Microsoft Public License, the terms of which
13 | are below; except for the Ruby Standard libraries which are governed by the
14 | Ruby license. We have included a copy of the Ruby license in the same
15 | directory as this file
16 |
17 | 1. Definitions
18 | The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law.
19 | A “contribution” is the original software, or any additions or changes to the software.
20 | A “contributor” is any person that distributes its contribution under this license.
21 | “Licensed patents” are a contributor’s patent claims that read directly on its contribution.
22 | 2. Grant of Rights
23 | (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright
24 | license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
25 |
26 | (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide,
27 | royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative
28 | works of the contribution in the software.
29 |
30 | 3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks.
31 | (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
32 | (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
33 | (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution.
34 | If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
35 |
36 | (E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional
37 | consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability,
38 | fitness for a particular purpose and non-infringement.
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/tests/MarkEmbling.PostcodesIO.Tests/Integration/NearestTests.cs:
--------------------------------------------------------------------------------
1 | using MarkEmbling.PostcodesIO.Data;
2 | using NUnit.Framework;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 |
6 | namespace MarkEmbling.PostcodesIO.Tests.Integration
7 | {
8 | [TestFixture, Explicit("Hits live Postcodes.io API")]
9 | public class NearestTests
10 | {
11 | private PostcodesIOClient _client;
12 |
13 | [SetUp]
14 | public void Setup()
15 | {
16 | _client = new PostcodesIOClient();
17 | }
18 |
19 | [Test]
20 | public void Nearest_returns_populated_response()
21 | {
22 | var results = _client.Nearest("GU1 1AA");
23 |
24 | foreach (var result in results)
25 | {
26 | TestLookup_returns_populated_responseResult(result);
27 | }
28 | }
29 |
30 | [Test]
31 | public async Task Nearest_returns_populated_response_async()
32 | {
33 | var results = await _client.NearestAsync("GU1 1AA");
34 |
35 | foreach (var result in results)
36 | {
37 | TestLookup_returns_populated_responseResult(result);
38 | }
39 | }
40 |
41 | private static void TestLookup_returns_populated_responseResult(NearestResult result)
42 | {
43 | Assert.That(result.Postcode, Is.Not.Null);
44 | Assert.That(result.Quality, Is.Not.Null);
45 | Assert.That(result.Eastings, Is.Not.Null);
46 | Assert.That(result.Northings, Is.Not.Null);
47 | Assert.That(result.Country, Is.Not.Null);
48 | Assert.That(result.NHSHealthAuthority, Is.Not.Null);
49 | Assert.That(result.Longitude, Is.Not.Null);
50 | Assert.That(result.ParliamentaryConstituency, Is.Not.Null);
51 | Assert.That(result.EuropeanElectoralRegion, Is.Not.Null);
52 | Assert.That(result.PrimaryCareTrust, Is.Not.Null);
53 | Assert.That(result.Region, Is.Not.Null);
54 | Assert.That(result.LSOA, Is.Not.Null);
55 | Assert.That(result.MSOA, Is.Not.Null);
56 | Assert.That(result.NUTS, Is.Not.Null);
57 | Assert.That(result.InCode, Is.Not.Null);
58 | Assert.That(result.OutCode, Is.Not.Null);
59 | Assert.That(result.AdminDistrict, Is.Not.Null);
60 | Assert.That(result.Parish, Is.Not.Null);
61 | Assert.That(result.AdminCounty, Is.Not.Null);
62 | Assert.That(result.AdminWard, Is.Not.Null);
63 | Assert.That(result.CED, Is.Not.Null);
64 | Assert.That(result.CCG, Is.Not.Null);
65 | Assert.That(result.Codes, Is.Not.Null);
66 |
67 | Assert.That(result.Distance, Is.Not.Null);
68 | }
69 |
70 | [Test]
71 | public void Nearest_returns_populated_response_limit()
72 | {
73 | var limit = 20;
74 | var results = _client.Nearest("GU1 1AA", limit);
75 |
76 | Assert.That(results.Count(), Is.EqualTo(limit));
77 | }
78 |
79 | [Test]
80 | public async Task Nearest_returns_populated_response_limit_async()
81 | {
82 | var limit = 20;
83 | var results = await _client.NearestAsync("GU1 1AA", limit);
84 |
85 | Assert.That(results.Count(), Is.EqualTo(limit));
86 | }
87 |
88 | //TODO: tests on radius, need to find postcode with other postcodes that are not near
89 |
90 | }
91 | }
--------------------------------------------------------------------------------
/tests/MarkEmbling.PostcodesIO.Tests/Unit/SerializabilityTests.cs:
--------------------------------------------------------------------------------
1 | using MarkEmbling.PostcodesIO.Data;
2 | using NUnit.Framework;
3 | using System.Text.Json;
4 |
5 | namespace MarkEmbling.PostcodesIO.Tests.Unit
6 | {
7 | [TestFixture]
8 | public class SerializabilityTests
9 | {
10 | static byte[] Serialize(object o)
11 | {
12 | if (o == null) return null;
13 | return JsonSerializer.SerializeToUtf8Bytes(o);
14 | }
15 |
16 | static T Deserialize(byte[] bytes)
17 | {
18 | if (bytes == null || bytes.Length == 0) return default;
19 | return JsonSerializer.Deserialize(bytes);
20 | }
21 |
22 | [Test]
23 | public void PostcodeData_should_be_serializable()
24 | {
25 | var expected = new PostcodeData
26 | {
27 | AdminCounty = "AdminCounty",
28 | AdminWard = "AdminWard",
29 | AdminDistrict = "AdminDistrict",
30 | CCG = "CCG",
31 | CED = "CED",
32 | Country = "Country",
33 | Eastings = 100000,
34 | Northings = 200000,
35 | EuropeanElectoralRegion = "EuropeanElectoralRegion",
36 | InCode = "InCode",
37 | LSOA = "LSOA",
38 | Latitude = 53.9999,
39 | Longitude = -3.9999,
40 | MSOA = "MSOA",
41 | NHSHealthAuthority = "NHSHealthAuthority",
42 | NUTS = "NUTS",
43 | OutCode = "OutCode",
44 | Parish = "Parish",
45 | ParliamentaryConstituency = "ParliamentaryConstituency",
46 | Postcode = "Postcode",
47 | PrimaryCareTrust = "PrimaryCareTrust",
48 | Quality = 1,
49 | Region = "Region",
50 | Codes = new PostcodeCodesData {Parish = "Parish", CCG = "CCG", AdminDistrict = "AdminDistrict", AdminWard = "AdminWard", AdminCounty = "AdminCounty"}
51 | };
52 |
53 | var expectedBytes = Serialize(expected);
54 | var actual = Deserialize(expectedBytes);
55 |
56 | Assert.That(actual.Postcode, Is.EqualTo(expected.Postcode));
57 | Assert.That(actual.Eastings, Is.EqualTo(expected.Eastings));
58 | Assert.That(actual.Latitude, Is.EqualTo(expected.Latitude));
59 | Assert.That(actual.Codes.AdminCounty, Is.EqualTo(expected.Codes.AdminCounty));
60 | }
61 |
62 | [Test]
63 | public void OutcodeData_should_be_serializable()
64 | {
65 | var expected = new OutcodeData
66 | {
67 | AdminCounty = ["AdminCounty"],
68 | AdminDistrict = ["AdminDistrict"],
69 | AdminWard = ["AdminWard"],
70 | Country = ["Country"],
71 | Eastings = 10000,
72 | Latitude = 53.9999,
73 | Longitude = -3.9999,
74 | Northings = 20000,
75 | Outcode = "OUT",
76 | Parish = ["Parish"]
77 | };
78 | var expectedBytes = Serialize(expected);
79 | var actual = Deserialize(expectedBytes);
80 |
81 | Assert.That(actual.AdminCounty, Is.EqualTo(expected.AdminCounty));
82 | Assert.That(actual.Eastings, Is.EqualTo(expected.Eastings));
83 | Assert.That(actual.Latitude, Is.EqualTo(expected.Latitude));
84 | Assert.That(actual.Outcode, Is.EqualTo(expected.Outcode));
85 | }
86 |
87 | [Test]
88 | public void TerminatedPostcodeData_should_be_serializable()
89 | {
90 | var expected = new TerminatedPostcodeData
91 | {
92 | Latitude = 53.9999,
93 | Longitude = -3.9999,
94 | MonthTerminated = 1,
95 | Postcode = "Postcode",
96 | YearTerminated = 2000
97 | };
98 | var expectedBytes = Serialize(expected);
99 | var actual = Deserialize(expectedBytes);
100 |
101 | Assert.That(actual.Latitude, Is.EqualTo(expected.Latitude));
102 | Assert.That(actual.MonthTerminated, Is.EqualTo(expected.MonthTerminated));
103 | Assert.That(actual.Postcode, Is.EqualTo(expected.Postcode));
104 | }
105 | }
106 | }
--------------------------------------------------------------------------------
/tests/MarkEmbling.PostcodesIO.Tests/Integration/LookupTests.cs:
--------------------------------------------------------------------------------
1 | using MarkEmbling.PostcodesIO.Data;
2 | using NUnit.Framework;
3 | using System.Collections.Generic;
4 | using System.Threading.Tasks;
5 |
6 | namespace MarkEmbling.PostcodesIO.Tests.Integration
7 | {
8 | [TestFixture, Explicit("Hits live Postcodes.io API")]
9 | public class LookupTests {
10 | private PostcodesIOClient _client;
11 |
12 | [SetUp]
13 | public void Setup() {
14 | _client = new PostcodesIOClient();
15 | }
16 |
17 | [Test]
18 | public void Lookup_returns_populated_response()
19 | {
20 | var result = _client.Lookup("GU1 1AA");
21 |
22 | TestLookup_returns_populated_responseResult(result);
23 | }
24 |
25 | [Test]
26 | public void OutwardCode_Lookup_returns_populated_response()
27 | {
28 | var result = _client.OutwardCodeLookup("IP3");
29 |
30 | TestOutwardCode_Lookup_returns_populated_responseResult(result);
31 | }
32 |
33 | [Test]
34 | public async Task Lookup_returns_populated_response_async()
35 | {
36 | var result = await _client.LookupAsync("GU1 1AA");
37 |
38 | TestLookup_returns_populated_responseResult(result);
39 | }
40 |
41 | private static void TestLookup_returns_populated_responseResult(PostcodeData result)
42 | {
43 | Assert.That(result.Postcode, Is.EqualTo("GU1 1AA"));
44 | Assert.That(result.Quality, Is.EqualTo(1));
45 | Assert.That(result.Eastings, Is.EqualTo(499049));
46 | Assert.That(result.Northings, Is.EqualTo(150522));
47 | Assert.That(result.Country, Is.EqualTo("England"));
48 | Assert.That(result.NHSHealthAuthority, Is.EqualTo("South East Coast"));
49 | Assert.That(result.Longitude, Is.EqualTo(-0.582332));
50 | Assert.That(result.Latitude, Is.EqualTo(51.245283));
51 | Assert.That(result.ParliamentaryConstituency, Is.EqualTo("Guildford"));
52 | Assert.That(result.EuropeanElectoralRegion, Is.EqualTo("South East"));
53 | Assert.That(result.PrimaryCareTrust, Is.EqualTo("Surrey"));
54 | Assert.That(result.Region, Is.EqualTo("South East"));
55 | Assert.That(result.LSOA, Is.EqualTo("Guildford 015A"));
56 | Assert.That(result.MSOA, Is.EqualTo("Guildford 015"));
57 | Assert.That(result.NUTS, Is.EqualTo("Guildford"));
58 | Assert.That(result.InCode, Is.EqualTo("1AA"));
59 | Assert.That(result.OutCode, Is.EqualTo("GU1"));
60 | Assert.That(result.AdminDistrict, Is.EqualTo("Guildford"));
61 | Assert.That(result.Parish, Is.EqualTo("Guildford, unparished area"));
62 | Assert.That(result.AdminCounty, Is.EqualTo("Surrey"));
63 | Assert.That(result.AdminWard, Is.EqualTo("Stoke"));
64 | Assert.That(result.CED, Is.EqualTo("Guildford South West"));
65 | Assert.That(result.CCG, Is.EqualTo("NHS Surrey Heartlands"));
66 | Assert.That(result.Codes, Is.Not.Null);
67 | }
68 |
69 | private static void TestOutwardCode_Lookup_returns_populated_responseResult(OutcodeData result)
70 | {
71 | Assert.That(result.Outcode, Is.EqualTo("IP3"));
72 | Assert.That(result.Northings, Is.EqualTo(242900));
73 | Assert.That(result.Eastings, Is.EqualTo(618740));
74 | Assert.That(result.AdminCounty, Is.EqualTo(new List() { "Suffolk" }));
75 | Assert.That(result.AdminDistrict, Is.EqualTo(new List() { "Ipswich", "East Suffolk" }));
76 | Assert.That(result.AdminWard, Is.EqualTo(new List() { "Bixley", "Martlesham & Purdis Farm", "Gainsborough", "Holywells", "Rushmere St Andrew", "Alexandra", "St John's", "Priory Heath" }));
77 | Assert.That(result.Longitude, Is.EqualTo(1.188121282722514));
78 | Assert.That(result.Latitude, Is.EqualTo(52.041320951570555));
79 | Assert.That(result.Country, Is.EqualTo(new List() { "England" }));
80 | Assert.That(result.Parish, Is.EqualTo(new List() { "Ipswich, unparished area", "Rushmere St. Andrew", "Purdis Farm" }));
81 | }
82 |
83 | [Test]
84 | public void Lookup_returns_populated_codes_property()
85 | {
86 | var result = _client.Lookup("GU1 1AA").Codes;
87 |
88 | TestLookup_returns_populated_codes_propertyResult(result);
89 | }
90 |
91 | [Test]
92 | public async Task Lookup_returns_populated_codes_property_async()
93 | {
94 | var result = (await _client.LookupAsync("GU1 1AA")).Codes;
95 |
96 | TestLookup_returns_populated_codes_propertyResult(result);
97 | }
98 |
99 | private static void TestLookup_returns_populated_codes_propertyResult(PostcodeCodesData result)
100 | {
101 | Assert.That(result.AdminDistrict, Is.EqualTo("E07000209"));
102 | Assert.That(result.AdminCounty, Is.EqualTo("E10000030"));
103 | Assert.That(result.AdminWard, Is.EqualTo("E05014959"));
104 | Assert.That(result.Parish, Is.EqualTo("E43000138"));
105 | Assert.That(result.CCG, Is.EqualTo("E38000264"));
106 | Assert.That(result.CCGId, Is.EqualTo("92A"));
107 | Assert.That(result.CED, Is.EqualTo("E58001497"));
108 | Assert.That(result.NUTS, Is.EqualTo("TLJ25"));
109 | Assert.That(result.LAU2, Is.EqualTo("E07000209"));
110 | Assert.That(result.LSOA, Is.EqualTo("E01030452"));
111 | Assert.That(result.MSOA, Is.EqualTo("E02006358"));
112 | Assert.That(result.PFA, Is.EqualTo("E23000031"));
113 | }
114 |
115 | [Test]
116 | public void Lookup_correctly_handles_null_geolocation_values()
117 | {
118 | var result = _client.Lookup("JE2 4ST");
119 |
120 | Assert.That(result.Eastings, Is.Null);
121 | Assert.That(result.Northings, Is.Null);
122 | Assert.That(result.Latitude, Is.Null);
123 | Assert.That(result.Longitude, Is.Null);
124 | }
125 | }
126 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 |
33 | # Visual Studio 2015/2017 cache/options directory
34 | .vs/
35 | # Uncomment if you have tasks that create the project's static files in wwwroot
36 | #wwwroot/
37 |
38 | # Visual Studio 2017 auto generated files
39 | Generated\ Files/
40 |
41 | # MSTest test Results
42 | [Tt]est[Rr]esult*/
43 | [Bb]uild[Ll]og.*
44 |
45 | # NUNIT
46 | *.VisualState.xml
47 | TestResult.xml
48 |
49 | # Build Results of an ATL Project
50 | [Dd]ebugPS/
51 | [Rr]eleasePS/
52 | dlldata.c
53 |
54 | # Benchmark Results
55 | BenchmarkDotNet.Artifacts/
56 |
57 | # .NET Core
58 | project.lock.json
59 | project.fragment.lock.json
60 | artifacts/
61 |
62 | # StyleCop
63 | StyleCopReport.xml
64 |
65 | # Files built by Visual Studio
66 | *_i.c
67 | *_p.c
68 | *_h.h
69 | *.ilk
70 | *.meta
71 | *.obj
72 | *.iobj
73 | *.pch
74 | *.pdb
75 | *.ipdb
76 | *.pgc
77 | *.pgd
78 | *.rsp
79 | *.sbr
80 | *.tlb
81 | *.tli
82 | *.tlh
83 | *.tmp
84 | *.tmp_proj
85 | *_wpftmp.csproj
86 | *.log
87 | *.vspscc
88 | *.vssscc
89 | .builds
90 | *.pidb
91 | *.svclog
92 | *.scc
93 |
94 | # Chutzpah Test files
95 | _Chutzpah*
96 |
97 | # Visual C++ cache files
98 | ipch/
99 | *.aps
100 | *.ncb
101 | *.opendb
102 | *.opensdf
103 | *.sdf
104 | *.cachefile
105 | *.VC.db
106 | *.VC.VC.opendb
107 |
108 | # Visual Studio profiler
109 | *.psess
110 | *.vsp
111 | *.vspx
112 | *.sap
113 |
114 | # Visual Studio Trace Files
115 | *.e2e
116 |
117 | # TFS 2012 Local Workspace
118 | $tf/
119 |
120 | # Guidance Automation Toolkit
121 | *.gpState
122 |
123 | # ReSharper is a .NET coding add-in
124 | _ReSharper*/
125 | *.[Rr]e[Ss]harper
126 | *.DotSettings.user
127 |
128 | # JustCode is a .NET coding add-in
129 | .JustCode
130 |
131 | # TeamCity is a build add-in
132 | _TeamCity*
133 |
134 | # DotCover is a Code Coverage Tool
135 | *.dotCover
136 |
137 | # AxoCover is a Code Coverage Tool
138 | .axoCover/*
139 | !.axoCover/settings.json
140 |
141 | # Visual Studio code coverage results
142 | *.coverage
143 | *.coveragexml
144 |
145 | # NCrunch
146 | _NCrunch_*
147 | .*crunch*.local.xml
148 | nCrunchTemp_*
149 |
150 | # MightyMoose
151 | *.mm.*
152 | AutoTest.Net/
153 |
154 | # Web workbench (sass)
155 | .sass-cache/
156 |
157 | # Installshield output folder
158 | [Ee]xpress/
159 |
160 | # DocProject is a documentation generator add-in
161 | DocProject/buildhelp/
162 | DocProject/Help/*.HxT
163 | DocProject/Help/*.HxC
164 | DocProject/Help/*.hhc
165 | DocProject/Help/*.hhk
166 | DocProject/Help/*.hhp
167 | DocProject/Help/Html2
168 | DocProject/Help/html
169 |
170 | # Click-Once directory
171 | publish/
172 |
173 | # Publish Web Output
174 | *.[Pp]ublish.xml
175 | *.azurePubxml
176 | # Note: Comment the next line if you want to checkin your web deploy settings,
177 | # but database connection strings (with potential passwords) will be unencrypted
178 | *.pubxml
179 | *.publishproj
180 |
181 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
182 | # checkin your Azure Web App publish settings, but sensitive information contained
183 | # in these scripts will be unencrypted
184 | PublishScripts/
185 |
186 | # NuGet Packages
187 | *.nupkg
188 | # The packages folder can be ignored because of Package Restore
189 | **/[Pp]ackages/*
190 | # except build/, which is used as an MSBuild target.
191 | !**/[Pp]ackages/build/
192 | # Uncomment if necessary however generally it will be regenerated when needed
193 | #!**/[Pp]ackages/repositories.config
194 | # NuGet v3's project.json files produces more ignorable files
195 | *.nuget.props
196 | *.nuget.targets
197 |
198 | # Microsoft Azure Build Output
199 | csx/
200 | *.build.csdef
201 |
202 | # Microsoft Azure Emulator
203 | ecf/
204 | rcf/
205 |
206 | # Windows Store app package directories and files
207 | AppPackages/
208 | BundleArtifacts/
209 | Package.StoreAssociation.xml
210 | _pkginfo.txt
211 | *.appx
212 | *.appxbundle
213 | *.appxupload
214 |
215 | # Visual Studio cache files
216 | # files ending in .cache can be ignored
217 | *.[Cc]ache
218 | # but keep track of directories ending in .cache
219 | !?*.[Cc]ache/
220 |
221 | # Others
222 | ClientBin/
223 | ~$*
224 | *~
225 | *.dbmdl
226 | *.dbproj.schemaview
227 | *.jfm
228 | *.pfx
229 | *.publishsettings
230 | orleans.codegen.cs
231 |
232 | # Including strong name files can present a security risk
233 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
234 | #*.snk
235 |
236 | # Since there are multiple workflows, uncomment next line to ignore bower_components
237 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
238 | #bower_components/
239 |
240 | # RIA/Silverlight projects
241 | Generated_Code/
242 |
243 | # Backup & report files from converting an old project file
244 | # to a newer Visual Studio version. Backup files are not needed,
245 | # because we have git ;-)
246 | _UpgradeReport_Files/
247 | Backup*/
248 | UpgradeLog*.XML
249 | UpgradeLog*.htm
250 | ServiceFabricBackup/
251 | *.rptproj.bak
252 |
253 | # SQL Server files
254 | *.mdf
255 | *.ldf
256 | *.ndf
257 |
258 | # Business Intelligence projects
259 | *.rdl.data
260 | *.bim.layout
261 | *.bim_*.settings
262 | *.rptproj.rsuser
263 | *- Backup*.rdl
264 |
265 | # Microsoft Fakes
266 | FakesAssemblies/
267 |
268 | # GhostDoc plugin setting file
269 | *.GhostDoc.xml
270 |
271 | # Node.js Tools for Visual Studio
272 | .ntvs_analysis.dat
273 | node_modules/
274 |
275 | # Visual Studio 6 build log
276 | *.plg
277 |
278 | # Visual Studio 6 workspace options file
279 | *.opt
280 |
281 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
282 | *.vbw
283 |
284 | # Visual Studio LightSwitch build output
285 | **/*.HTMLClient/GeneratedArtifacts
286 | **/*.DesktopClient/GeneratedArtifacts
287 | **/*.DesktopClient/ModelManifest.xml
288 | **/*.Server/GeneratedArtifacts
289 | **/*.Server/ModelManifest.xml
290 | _Pvt_Extensions
291 |
292 | # Paket dependency manager
293 | .paket/paket.exe
294 | paket-files/
295 |
296 | # FAKE - F# Make
297 | .fake/
298 |
299 | # CodeRush personal settings
300 | .cr/personal
301 |
302 | # Python Tools for Visual Studio (PTVS)
303 | __pycache__/
304 | *.pyc
305 |
306 | # Cake - Uncomment if you are using it
307 | # tools/**
308 | # !tools/packages.config
309 |
310 | # Tabs Studio
311 | *.tss
312 |
313 | # Telerik's JustMock configuration file
314 | *.jmconfig
315 |
316 | # BizTalk build output
317 | *.btp.cs
318 | *.btm.cs
319 | *.odx.cs
320 | *.xsd.cs
321 |
322 | # OpenCover UI analysis results
323 | OpenCover/
324 |
325 | # Azure Stream Analytics local run output
326 | ASALocalRun/
327 |
328 | # MSBuild Binary and Structured Log
329 | *.binlog
330 |
331 | # NVidia Nsight GPU debugger configuration file
332 | *.nvuser
333 |
334 | # MFractors (Xamarin productivity tool) working folder
335 | .mfractor/
336 |
337 | # Local History for Visual Studio
338 | .localhistory/
339 |
340 | # BeatPulse healthcheck temp database
341 | healthchecksdb
342 |
343 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
344 | MigrationBackup/
345 |
--------------------------------------------------------------------------------
/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 |
37 |
38 |
39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config
40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config
41 |
42 |
43 |
44 | $(MSBuildProjectDirectory)\packages.config
45 | $(PackagesProjectConfig)
46 |
47 |
48 |
49 |
50 | $(NuGetToolsPath)\NuGet.exe
51 | @(PackageSource)
52 |
53 | "$(NuGetExePath)"
54 | mono --runtime=v4.0.30319 "$(NuGetExePath)"
55 |
56 | $(TargetDir.Trim('\\'))
57 |
58 | -RequireConsent
59 | -NonInteractive
60 |
61 | "$(SolutionDir) "
62 | "$(SolutionDir)"
63 |
64 |
65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
67 |
68 |
69 |
70 | RestorePackages;
71 | $(BuildDependsOn);
72 |
73 |
74 |
75 |
76 | $(BuildDependsOn);
77 | BuildPackage;
78 |
79 |
80 |
81 |
82 |
83 |
84 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
99 |
100 |
103 |
104 |
105 |
106 |
108 |
109 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/src/MarkEmbling.PostcodesIO/PostcodesIOClient.cs:
--------------------------------------------------------------------------------
1 | using MarkEmbling.PostcodesIO.Data;
2 | using MarkEmbling.PostcodesIO.Exceptions;
3 | using MarkEmbling.PostcodesIO.Results;
4 | using RestSharp;
5 | using RestSharp.Serializers.Json;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Net;
9 | using System.Text.Json;
10 | using System.Threading.Tasks;
11 |
12 | namespace MarkEmbling.PostcodesIO
13 | {
14 | public class PostcodesIOClient : IPostcodesIOClient {
15 | private readonly string _endpoint;
16 | private readonly string _proxyServerUrl;
17 | private readonly RestClient _client;
18 |
19 | public PostcodesIOClient(string endpoint = "https://api.postcodes.io", string proxyServerUrl = null) {
20 | _endpoint = endpoint;
21 | _proxyServerUrl = proxyServerUrl;
22 |
23 | var clientOptions = new RestClientOptions(_endpoint);
24 | if (!string.IsNullOrEmpty(_proxyServerUrl))
25 | {
26 | clientOptions.Proxy = new WebProxy(_proxyServerUrl, true)
27 | {
28 | Credentials = CredentialCache.DefaultCredentials
29 | };
30 | }
31 | _client = new RestClient(
32 | baseUrl: new Uri(_endpoint),
33 | configureSerialization: s => s.UseSystemTextJson(new JsonSerializerOptions {
34 | PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower })
35 | );
36 | }
37 |
38 | private T Execute(RestRequest request) where T : new() {
39 | var response = _client.Execute>(request);
40 |
41 | if (response.ErrorException != null)
42 | throw new PostcodesIOApiException(response.ErrorException);
43 | if (response.Data == null)
44 | throw new PostcodesIOEmptyResponseException(response.StatusCode);
45 |
46 | return response.Data.Result;
47 | }
48 |
49 | private async Task ExecuteAsync(RestRequest request) where T : new()
50 | {
51 | var response = await _client.ExecuteAsync>(request).ConfigureAwait(false);
52 |
53 | if (response.ErrorException != null)
54 | throw new PostcodesIOApiException(response.ErrorException);
55 | if (response.Data == null)
56 | throw new PostcodesIOEmptyResponseException(response.StatusCode);
57 |
58 | return response.Data.Result;
59 | }
60 |
61 | public PostcodeData Lookup(string postcode) {
62 | var request = CreateLookupRequest(postcode);
63 | return Execute(request);
64 | }
65 |
66 | public OutcodeData OutwardCodeLookup(string outcode) {
67 | var request = CreateOutwardCodeLookupRequest(outcode);
68 | return Execute(request);
69 | }
70 |
71 | public Task LookupAsync(string postcode)
72 | {
73 | var request = CreateLookupRequest(postcode);
74 | return ExecuteAsync(request);
75 | }
76 |
77 | public IEnumerable> BulkLookup(IEnumerable postcodes)
78 | {
79 | var request = CreateBulkLookupRequest(postcodes);
80 | return Execute>>(request);
81 | }
82 |
83 | public Task>> BulkLookupAsync(IEnumerable postcodes)
84 | {
85 | var request = CreateBulkLookupRequest(postcodes);
86 | return ExecuteAsync>>(request).ContinueWith(t => t.Result as IEnumerable>, TaskContinuationOptions.OnlyOnRanToCompletion);
87 | }
88 |
89 | public IEnumerable Query(string q, int? limit = null)
90 | {
91 | var request = CreateQueryRequest(q, limit);
92 | return Execute>(request);
93 | }
94 |
95 | public Task> QueryAsync(string q, int? limit = null)
96 | {
97 | var request = CreateQueryRequest(q, limit);
98 | return ExecuteAsync>(request).ContinueWith(t => t.Result as IEnumerable, TaskContinuationOptions.OnlyOnRanToCompletion);
99 | }
100 |
101 | public bool Validate(string postcode)
102 | {
103 | var request = CreateValidateRequest(postcode);
104 | return Execute(request);
105 | }
106 |
107 | public Task ValidateAsync(string postcode)
108 | {
109 | var request = CreateValidateRequest(postcode);
110 | return ExecuteAsync(request);
111 | }
112 |
113 | public IEnumerable LookupLatLon(ReverseGeocodeQuery query)
114 | {
115 | var request = CreateLookupLocationRequest(query);
116 | return Execute>(request);
117 | }
118 |
119 | public Task> LookupLatLonAsync(ReverseGeocodeQuery query)
120 | {
121 | var request = CreateLookupLocationRequest(query);
122 | return ExecuteAsync>(request).ContinueWith(t => t.Result as IEnumerable, TaskContinuationOptions.OnlyOnRanToCompletion);
123 | }
124 |
125 | public IEnumerable>> BulkLookupLatLon(IEnumerable queries)
126 | {
127 | var request = CreateBulkLookupLatLon(queries);
128 | return Execute>>>(request);
129 | }
130 |
131 | public Task>>> BulkLookupLatLonAsync(IEnumerable queries)
132 | {
133 | var request = CreateBulkLookupLatLon(queries);
134 | return ExecuteAsync>>>(request).ContinueWith(t => t.Result as IEnumerable>>, TaskContinuationOptions.OnlyOnRanToCompletion);
135 | }
136 |
137 | public IEnumerable Autocomplete(string postcode, int? limit = null)
138 | {
139 | var request = CreateAutocompleteRequest(postcode, limit);
140 | return Execute>(request);
141 | }
142 |
143 | public Task> AutocompleteAsync(string postcode, int? limit = null)
144 | {
145 | var request = CreateAutocompleteRequest(postcode, limit);
146 | return ExecuteAsync>(request).ContinueWith(t => t.Result as IEnumerable, TaskContinuationOptions.OnlyOnRanToCompletion);
147 | }
148 |
149 | public PostcodeData Random()
150 | {
151 | var request = CreateRandomRequest();
152 | return Execute(request);
153 | }
154 |
155 | public Task RandomAsync()
156 | {
157 | var request = CreateRandomRequest();
158 | return ExecuteAsync(request);
159 | }
160 |
161 | public IEnumerable Nearest(string postcode, int? limit = null, int? radius = null)
162 | {
163 | var request = CreateNearest(postcode, limit, radius);
164 | return Execute>(request);
165 | }
166 |
167 | public Task> NearestAsync(string postcode, int? limit = null, int? radius = null)
168 | {
169 | var request = CreateNearest(postcode, limit, radius);
170 | return ExecuteAsync>(request).ContinueWith(t => t.Result as IEnumerable, TaskContinuationOptions.OnlyOnRanToCompletion);
171 | }
172 |
173 | public TerminatedPostcodeData Terminated(string postcode)
174 | {
175 | var request = CreateTerminatedRequest(postcode);
176 | return Execute(request);
177 | }
178 |
179 | public Task TerminatedAsync(string postcode)
180 | {
181 | var request = CreateTerminatedRequest(postcode);
182 | return ExecuteAsync(request);
183 | }
184 |
185 | private static RestRequest CreateBulkLookupRequest(IEnumerable postcodes)
186 | {
187 | var request = new RestRequest("postcodes", Method.Post)
188 | {
189 | RequestFormat = DataFormat.Json
190 | };
191 | request.AddJsonBody(new { postcodes });
192 | return request;
193 | }
194 |
195 | private static RestRequest CreateQueryRequest(string q, int? limit)
196 | {
197 | var request = new RestRequest("postcodes", Method.Get);
198 | request.AddQueryParameter("q", q);
199 | if (limit.HasValue) request.AddParameter("limit", limit.Value);
200 | return request;
201 | }
202 |
203 | private static RestRequest CreateValidateRequest(string postcode)
204 | {
205 | var request = new RestRequest(string.Format("postcodes/{0}/validate", postcode), Method.Get);
206 | return request;
207 | }
208 |
209 | private static RestRequest CreateLookupLocationRequest(ReverseGeocodeQuery query)
210 | {
211 | var request = new RestRequest("postcodes", Method.Get);
212 | request.AddParameter("lat", query.Latitude);
213 | request.AddParameter("lon", query.Longitude);
214 | if (query.Limit.HasValue) request.AddParameter("limit", query.Limit.Value);
215 | if (query.Radius.HasValue) request.AddParameter("radius", query.Radius.Value);
216 | if (query.WideSearch.HasValue) request.AddParameter("widesearch", query.WideSearch.Value);
217 | return request;
218 | }
219 |
220 | private static RestRequest CreateLookupRequest(string postcode)
221 | {
222 | return new RestRequest(string.Format("postcodes/{0}", postcode), Method.Get);
223 | }
224 |
225 | private static RestRequest CreateOutwardCodeLookupRequest(string outcode)
226 | {
227 | return new RestRequest(string.Format("outcodes/{0}", outcode), Method.Get);
228 | }
229 |
230 | private static RestRequest CreateBulkLookupLatLon(IEnumerable queries)
231 | {
232 | var request = new RestRequest("postcodes", Method.Post);
233 | request.AddJsonBody(new { geolocations = queries });
234 | return request;
235 | }
236 |
237 | private static RestRequest CreateAutocompleteRequest(string postcode, int? limit)
238 | {
239 | var request = new RestRequest(string.Format("postcodes/{0}/autocomplete", postcode), Method.Get);
240 | if (limit.HasValue) request.AddParameter("limit", limit.Value);
241 | return request;
242 | }
243 |
244 | private static RestRequest CreateRandomRequest()
245 | {
246 | var request = new RestRequest("random/postcodes", Method.Get);
247 | return request;
248 | }
249 |
250 | private static RestRequest CreateNearest(string postcode, int? limit, int? radius)
251 | {
252 | var request = new RestRequest(string.Format("postcodes/{0}/nearest", postcode), Method.Get);
253 | if (limit.HasValue) request.AddParameter("limit", limit.Value);
254 | if (radius.HasValue) request.AddParameter("radius", radius.Value);
255 | return request;
256 | }
257 |
258 | private static RestRequest CreateTerminatedRequest(string postcode)
259 | {
260 | return new RestRequest(string.Format("terminated_postcodes/{0}", postcode), Method.Get);
261 | }
262 | }
263 | }
--------------------------------------------------------------------------------