This looks generic can/should we move it to some utility class (and also write integration test for it)?
Happy to be deferred, so not in this PR.