-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Wealden District Council #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BadgerHobbs
wants to merge
7
commits into
main
Choose a base branch
from
collector/WealdenDistrictCouncil-issue-77-1767953982
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f0e071
Add collector for WealdenDistrictCouncil
github-actions[bot] 54031b0
Auto-format code with dotnet format
actions-user 35dc0b9
Format WealdenDistrictCouncil
BadgerHobbs 46e7576
Auto-format code with dotnet format
actions-user 69629d1
Address PR comments and style guide compliance
github-actions[bot] c4885c3
Add comments explaining postcode space removal
github-actions[bot] 2b79cfb
Use direct header access with ! for set-cookie header
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
275 changes: 275 additions & 0 deletions
275
BinDays.Api.Collectors/Collectors/Councils/WealdenDistrictCouncil.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| namespace BinDays.Api.Collectors.Collectors.Councils; | ||
|
|
||
| using BinDays.Api.Collectors.Collectors.Vendors; | ||
| using BinDays.Api.Collectors.Models; | ||
| using BinDays.Api.Collectors.Utilities; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Wealden District Council. | ||
| /// </summary> | ||
| internal sealed class WealdenDistrictCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Wealden District Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.wealden.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "wealden"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = | ||
| [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "Refuse", "Rubbish" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Recycling", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Recycling" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Garden" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| ]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Remove spaces from postcode as the Wealden API requires postcodes without spaces in form data and URL parameters | ||
| var sanitizedPostcode = postcode.Replace(" ", string.Empty); | ||
|
|
||
| // Prepare client-side request for getting cookies | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://www.wealden.gov.uk/recycling-and-waste/bin-search/", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Prepare client-side request for getting addresses | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie( | ||
| clientSideResponse.Headers["set-cookie"]! | ||
| ); | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| { "action", "wealden_get_properties_in_postcode" }, | ||
| { "postcode", sanitizedPostcode }, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = "https://www.wealden.gov.uk/wp-admin/admin-ajax.php", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" }, | ||
| { "X-Requested-With", "XMLHttpRequest" }, | ||
| { "cookie", requestCookies }, | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| Body = requestBody, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 2) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
| var properties = jsonDoc.RootElement.GetProperty("properties").EnumerateArray(); | ||
|
|
||
| // Iterate through each property, and create a new address object | ||
| var addresses = new List<Address>(); | ||
| foreach (var propertyElement in properties) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var address = new Address | ||
| { | ||
| Property = propertyElement.GetProperty("address").GetString()!.Trim(), | ||
| Postcode = postcode, | ||
| Uid = propertyElement.GetProperty("uprn").GetString()!, | ||
| }; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Remove spaces from postcode as the Wealden API requires postcodes without spaces in URL parameters and cookies | ||
| var sanitizedPostcode = (address.Postcode ?? string.Empty).Replace(" ", string.Empty); | ||
|
|
||
| // Prepare client-side request for getting cookies | ||
| if (clientSideResponse == null) | ||
| { | ||
| var requestUrl = $"https://www.wealden.gov.uk/recycling-and-waste/bin-search/?postcode={sanitizedPostcode}"; | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = requestUrl, | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Prepare client-side request for getting bin days | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie( | ||
| clientSideResponse.Headers["set-cookie"]! | ||
| ); | ||
|
|
||
| var cookies = string.IsNullOrWhiteSpace(requestCookies) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ? $"c_postcode={sanitizedPostcode}" | ||
| : $"{requestCookies}; c_postcode={sanitizedPostcode}"; | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| { "action", "wealden_get_collections_for_uprn" }, | ||
| { "uprn", address.Uid! }, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = "https://www.wealden.gov.uk/wp-admin/admin-ajax.php", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" }, | ||
| { "X-Requested-With", "XMLHttpRequest" }, | ||
| { "cookie", cookies }, | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| Body = requestBody, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 2) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
| var collection = jsonDoc.RootElement.GetProperty("collection"); | ||
|
|
||
| var binDays = new List<BinDay>(); | ||
|
|
||
| var binCollectionProperties = new Dictionary<string, string> | ||
| { | ||
| { "refuseCollectionDate", "Refuse" }, | ||
| { "recyclingCollectionDate", "Recycling" }, | ||
| { "gardenCollectionDate", "Garden" }, | ||
| }; | ||
|
|
||
| // Iterate through each bin collection property, and create a new bin day object | ||
| foreach (var property in binCollectionProperties) | ||
| { | ||
| if (!collection.TryGetProperty(property.Key, out var dateElement)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var dateString = dateElement.GetString(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(dateString)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var date = DateOnly.ParseExact( | ||
| dateString, | ||
| "yyyy-MM-dd'T'HH:mm:ss", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
|
|
||
| var bins = ProcessingUtilities.GetMatchingBins(_binTypes, property.Value); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = date, | ||
| Address = address, | ||
| Bins = bins, | ||
| }; | ||
|
|
||
| binDays.Add(binDay); | ||
| } | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
BinDays.Api.IntegrationTests/Collectors/Councils/WealdenDistrictCouncilTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| namespace BinDays.Api.IntegrationTests.Collectors.Councils; | ||
|
|
||
| using BinDays.Api.Collectors.Collectors; | ||
| using BinDays.Api.Collectors.Collectors.Councils; | ||
| using BinDays.Api.Collectors.Services; | ||
| using BinDays.Api.IntegrationTests.Helpers; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| public class WealdenDistrictCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new WealdenDistrictCouncil(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public WealdenDistrictCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("TN3 9PE")] | ||
| public async Task GetBinDaysTest(string postcode) | ||
| { | ||
| await TestSteps.EndToEnd( | ||
| _client, | ||
| _collectorService, | ||
| _collector, | ||
| postcode, | ||
| _outputHelper | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.