Skip to content

Commit

Permalink
feat: add fax autolist
Browse files Browse the repository at this point in the history
  • Loading branch information
Dovchik committed May 8, 2024
1 parent 28e8752 commit 4f22ab7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Sinch/Fax/Faxes/FaxesClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Sinch.Core;
Expand All @@ -19,6 +21,9 @@ public interface ISinchFaxFaxes
public Task<Fax> Send(CreateFaxRequest request, CancellationToken cancellationToken = default);

Task<ListFaxResponse> List(ListFaxesRequest listFaxesRequest, CancellationToken cancellationToken = default);

IAsyncEnumerable<Fax> ListAuto(ListFaxesRequest listFaxesRequest,
CancellationToken cancellationToken = default);
}

internal sealed class FaxesClient : ISinchFaxFaxes
Expand Down Expand Up @@ -60,12 +65,28 @@ public async Task<ListFaxResponse> List(ListFaxesRequest listFaxesRequest,
_loggerAdapter?.LogInformation("Fetching a list of faxes");
var uriBuilder = new UriBuilder(_uri.ToString())
{
Query =listFaxesRequest.ToQueryString()
Query = listFaxesRequest.ToQueryString()
};

return await _http.Send<ListFaxResponse>(uriBuilder.Uri, HttpMethod.Get, cancellationToken);
}

public async IAsyncEnumerable<Fax> ListAuto(ListFaxesRequest listFaxesRequest,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_loggerAdapter?.LogDebug("Auto Listing faxes");

var response = await List(listFaxesRequest, cancellationToken);
while (!Utils.IsLastPage(response.PageNumber, response.PageSize, response.TotalItems))
{
if (response.Faxes != null)
foreach (var contact in response.Faxes)
yield return contact;
listFaxesRequest.Page = (response.PageNumber + 1).ToString();
response = await List(listFaxesRequest, cancellationToken);
}
}

public async Task<Fax> GetAsync(string faxId)
{
var url = new Uri(_uri, $"/{faxId}");
Expand Down

0 comments on commit 4f22ab7

Please sign in to comment.