Skip to content

Commit

Permalink
feat: add support for sending base64 files (#76)
Browse files Browse the repository at this point in the history
* feat: add support for sending base64 files

with contentUrl prop independently
  • Loading branch information
Dovchik committed Jun 21, 2024
1 parent b9725f8 commit 59679e3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
30 changes: 18 additions & 12 deletions src/Sinch/Fax/Faxes/FaxesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,29 @@ public async Task<List<Fax>> Send(List<string> to, SendFaxRequest request,
return new List<Fax>() { fax };
}

if (request.ContentUrl?.Any() == true)
var sendingContentUrls = request.ContentUrl?.Any() == true;
var sendingBase64Files = request.Files?.Any() == true;
if (!sendingBase64Files && !sendingContentUrls)
{
_loggerAdapter?.LogInformation("Sending fax with content urls...");
if (request.To!.Count > 1)
{
var faxResponse = await _http.Send<SendFaxRequest, SendFaxResponse>(_uri, HttpMethod.Post, request,
cancellationToken: cancellationToken);
return faxResponse.Faxes;
}
throw new InvalidOperationException(
"Neither content urls or file content or base64 files provided for a create fax request.");
}

var fax = await _http.Send<SendFaxRequest, Fax>(_uri, HttpMethod.Post, request,
_loggerAdapter?.LogInformation(
"Sending fax with content urls - {isContentUrls}; with base64 files - {isBase64Files}",
sendingContentUrls, sendingBase64Files);


if (request.To!.Count > 1)
{
var faxResponseList = await _http.Send<SendFaxRequest, SendFaxResponse>(_uri, HttpMethod.Post, request,
cancellationToken: cancellationToken);
return new List<Fax>() { fax };
return faxResponseList.Faxes;
}

throw new InvalidOperationException(
"Neither content urls or file content provided for a create fax request.");
var faxJson = await _http.Send<SendFaxRequest, Fax>(_uri, HttpMethod.Post, request,
cancellationToken: cancellationToken);
return new List<Fax>() { faxJson };
}

/// <inheritdoc />
Expand Down
33 changes: 22 additions & 11 deletions src/Sinch/Fax/Faxes/SendFaxRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ namespace Sinch.Fax.Faxes
{
public class SendFaxRequest : IDisposable, IAsyncDisposable
{
[Obsolete("Required for system text json", true)]
public SendFaxRequest()
{
}

/// <summary>
/// Creates a fax with contentUrls
/// </summary>
/// <param name="contentUrl"></param>
public SendFaxRequest(List<string> contentUrl)
{
ContentUrl = contentUrl;
}

[JsonIgnore]
Expand Down Expand Up @@ -51,6 +42,27 @@ public SendFaxRequest(string filePath)
FileName = Path.GetFileName(filePath);
}

/// <summary>
/// Creates a fax with base64 files
/// </summary>
/// <param name="base64Files"></param>
public SendFaxRequest(List<Base64File> base64Files)
{
if (base64Files.Count == 0)
{
throw new ArgumentException("Should have at least one element", nameof(base64Files));
}

Files = base64Files;
}

/// <summary>
/// An array of base64 encoded files
/// </summary>
[JsonInclude]
[JsonPropertyName("files")]
public List<Base64File>? Files { get; private set; }

/// <summary>
/// A list of phone numbers in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading &#39;+&#39;.
/// </summary>
Expand All @@ -76,7 +88,7 @@ internal void SetTo(List<string> to)
/// </summary>
[JsonPropertyName("contentUrl")]
[JsonInclude]
public List<string>? ContentUrl { get; private set; }
public List<string>? ContentUrl { get; set; }

/// <summary>
/// Text that will be displayed at the top of each page of the fax. 50 characters maximum. Default header text is \&quot;-\&quot;. Note that the header is not applied until the fax is transmitted, so it will not appear on fax PDFs or thumbnails.
Expand Down Expand Up @@ -151,7 +163,6 @@ public ValueTask DisposeAsync()
}
}

/// TODO: implement sending base 64 files.
public sealed class Base64File
{
/// <summary>
Expand Down
31 changes: 29 additions & 2 deletions tests/Sinch.Tests/e2e/Fax/FaxesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public class FaxesTests : FaxTestBase
public async Task SendContentUrls()
{
var response = await FaxClient.Faxes.Send("+12015555555",
new SendFaxRequest(new List<string>() { "http://fax-db/fax1.pdf", "http://fax-db/fax2.pdf" }));
new SendFaxRequest()
{
ContentUrl = new List<string>() { "http://fax-db/fax1.pdf", "http://fax-db/fax2.pdf" }
});
response.Should().BeEquivalentTo(new Sinch.Fax.Faxes.Fax()
{
Id = "01HXVD9FPQ8MAJ2650W0KTY7D4",
Expand Down Expand Up @@ -65,7 +68,10 @@ public async Task SendContentUrls()
public async Task SendManyRecipients()
{
var response = await FaxClient.Faxes.Send(new List<string>() { "+12015555555", "+12015555554" },
new SendFaxRequest(new List<string>() { "http://fax-db/fax1.pdf" }));
new SendFaxRequest()
{
ContentUrl = new List<string>() { "http://fax-db/fax1.pdf" }
});
response.Should().HaveCount(2);
}

Expand Down Expand Up @@ -142,5 +148,26 @@ public async Task ListNoDateOtherParams()
}
});
}

[Fact]
public async Task SendBase64Files()
{
_fax.ContentUrl = null;
var response = await FaxClient.Faxes.Send("+12015555555",
new SendFaxRequest(new List<Base64File>
{
new Base64File()
{
File = "YWRzZnNhZGZhZnNkZnNhZA==",
FileType = FileType.PDF,
},
new Base64File()
{
File = "YWRmc2hnZ2FkZmg0M2V0d3FhM3Jld2FydzEyMzQxMjM=",
FileType = FileType.PDF,
}
}));
response.Should().BeEquivalentTo(_fax);
}
}
}

0 comments on commit 59679e3

Please sign in to comment.