-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Breckland Council #110
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
Merged
BadgerHobbs
merged 4 commits into
main
from
collector/BrecklandCouncil-issue-97-1768147499
Feb 7, 2026
+255
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
219 changes: 219 additions & 0 deletions
219
BinDays.Api.Collectors/Collectors/Councils/BrecklandCouncil.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,219 @@ | ||
| 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.Linq; | ||
| using System.Text.Json; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Breckland Council. | ||
| /// </summary> | ||
| internal sealed class BrecklandCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Breckland Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.breckland.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "breckland"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "Refuse Collection Service" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Recycling", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Recycling Collection Service" ], | ||
| }, | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Garden Waste Collection Service" ], | ||
| Type = BinType.Sack, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Food Waste", | ||
| Colour = BinColour.Grey, | ||
| Keys = [ "Food Waste Collection Service" ], | ||
| Type = BinType.Caddy, | ||
| }, | ||
| ]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting addresses | ||
| if (clientSideResponse == null) | ||
| { | ||
| var requestBody = JsonSerializer.Serialize(new | ||
| { | ||
| jsonrpc = "2.0", | ||
| id = "1", | ||
| method = "Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode", | ||
| @params = new | ||
| { | ||
| postcode, | ||
| environment = "live", | ||
| }, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://www.breckland.gov.uk/apiserver/ajaxlibrary", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "Content-Type", "application/json" }, | ||
| { "X-Requested-With", "XMLHttpRequest" }, | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| Body = requestBody, | ||
| }; | ||
|
|
||
| var response = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return response; | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
|
|
||
| // Iterate through each address, and create a new address object | ||
| var addresses = new List<Address>(); | ||
| foreach (var addressElement in jsonDoc.RootElement.GetProperty("result").EnumerateArray()) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var number = addressElement.GetProperty("number").GetString()?.Trim(); | ||
| var name = addressElement.GetProperty("name").GetString()?.Trim(); | ||
| var address1 = addressElement.GetProperty("address1").GetString()?.Trim(); | ||
| var address2 = addressElement.GetProperty("address2").GetString()?.Trim(); | ||
| var town = addressElement.GetProperty("town").GetString()?.Trim(); | ||
| var county = addressElement.GetProperty("county").GetString()?.Trim(); | ||
|
|
||
| var propertyParts = new[] { number, name, address1, address2, town, county } | ||
| .Where(part => !string.IsNullOrWhiteSpace(part)); | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = string.Join(", ", propertyParts), | ||
| Postcode = postcode, | ||
| Uid = addressElement.GetProperty("uprn").GetString()!.Trim(), | ||
| }; | ||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| var response = new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting bin days | ||
| if (clientSideResponse == null) | ||
| { | ||
| var requestBody = JsonSerializer.Serialize(new | ||
| { | ||
| jsonrpc = "2.0", | ||
| id = "1", | ||
| method = "Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn", | ||
| @params = new | ||
| { | ||
| uprn = address.Uid, | ||
| environment = "live", | ||
| }, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://www.breckland.gov.uk/apiserver/ajaxlibrary", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "Content-Type", "application/json" }, | ||
| { "X-Requested-With", "XMLHttpRequest" }, | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| Body = requestBody, | ||
| }; | ||
|
|
||
| var response = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return response; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
|
|
||
| // Iterate through each bin day, and create a new bin day object | ||
| var binDays = new List<BinDay>(); | ||
| foreach (var binDayElement in jsonDoc.RootElement.GetProperty("result").EnumerateArray()) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var binType = binDayElement.GetProperty("collectiontype").GetString()!.Trim(); | ||
| var collectionDate = binDayElement.GetProperty("nextcollection").GetString()!.Trim(); | ||
|
|
||
| var parsedDate = DateTime.ParseExact( | ||
| collectionDate, | ||
| "dd/MM/yyyy HH:mm:ss", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
|
|
||
| var matchedBins = ProcessingUtilities.GetMatchingBins(_binTypes, binType); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = DateOnly.FromDateTime(parsedDate), | ||
| Address = address, | ||
| Bins = matchedBins, | ||
| }; | ||
|
|
||
| binDays.Add(binDay); | ||
| } | ||
|
|
||
| var response = new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
BinDays.Api.IntegrationTests/Collectors/Councils/BrecklandCouncilTests.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 BrecklandCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new BrecklandCouncil(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public BrecklandCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("NR172BJ")] | ||
| 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.