forked from BAPostma/PostcodeAPI.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostcodeApiClientBase.cs
More file actions
53 lines (42 loc) · 1.57 KB
/
Copy pathPostcodeApiClientBase.cs
File metadata and controls
53 lines (42 loc) · 1.57 KB
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
using System;
using System.Text.RegularExpressions;
using PostcodeAPI.V1;
using RestSharp;
namespace PostcodeAPI
{
public abstract class PostcodeApiClientBase
{
protected IRestClient Client;
public string EndpointUrl { get; set; }
public string HeaderKey { get; set; }
public string APIKey { get; set; }
protected PostcodeApiClientBase(string apiKey, string endpointUrl, string headerKey)
{
APIKey = apiKey;
EndpointUrl = endpointUrl;
HeaderKey = headerKey;
}
protected void InitialiseClient(IRestClient client)
{
Client = client;
Client.BaseUrl = new Uri(EndpointUrl);
Client.AddDefaultHeader(HeaderKey, APIKey);
}
protected void InitialiseClient()
{
InitialiseClient(new RestClient(EndpointUrl));
}
//public abstract ApiResultWrapper GetAddress(string postcode);
//public abstract ApiResultWrapper GetAddress(string postcode, int number);
/// <summary>
/// Returns the P4, P5 or P6 format detected by the input.
/// </summary>
public string FindPostcodeType(string postcode)
{
if (Regex.IsMatch(postcode, @"^[0-9]{4}[a-zA-Z]{2}$")) return Constants.PostcodeFormatTypes.P6;
if (Regex.IsMatch(postcode, @"^[0-9]{4}[a-zA-Z]{1}$")) return Constants.PostcodeFormatTypes.P5;
if (Regex.IsMatch(postcode, @"^[0-9]{4}$")) return Constants.PostcodeFormatTypes.P4;
return string.Empty;
}
}
}