Skip to content
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

feat(fax): support services #74

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions src/Sinch/Fax/Emails/Emails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using System.Web;
using Sinch.Core;
using Sinch.Fax.Services;
using Sinch.Logger;

namespace Sinch.Fax.Emails
Expand Down Expand Up @@ -120,44 +121,26 @@ internal class EmailsClient : ISinchFaxEmails
private readonly string _projectId;
private readonly Uri _apiBasePath;
private readonly IHttp _http;
private readonly ISinchFaxServices _services;
private readonly ILoggerAdapter<ISinchFaxEmails>? _logger;
private readonly Uri _baseAddress;


internal EmailsClient(string projectId, Uri baseAddress, ILoggerAdapter<ISinchFaxEmails>? loggerAdapter,
IHttp httpClient)
IHttp httpClient, ISinchFaxServices services)
{
_logger = loggerAdapter;
_http = httpClient;
_services = services;
_projectId = projectId;
_baseAddress = baseAddress;
_apiBasePath = new Uri(baseAddress, $"/v3/projects/{projectId}/emails");
}

/// <inheritdoc />

public Task<ListEmailsResponse<string>> ListForNumber(string serviceId, string phoneNumber, int? page,
int? pageSize,
CancellationToken cancellationToken = default)
{
_logger?.LogInformation("Listing emails for {serviceId} and {number}", serviceId, phoneNumber);
ExceptionUtils.CheckEmptyString(nameof(serviceId), serviceId);
ExceptionUtils.CheckEmptyString(nameof(phoneNumber), phoneNumber);

var uriBuilder = new UriBuilder(_baseAddress);
uriBuilder.Path += $"services/{serviceId}/numbers/{phoneNumber}/emails";
var queryString = HttpUtility.ParseQueryString(string.Empty);
if (page.HasValue)
{
queryString.Add("page", page.Value.ToString());
}

if (pageSize.HasValue)
{
queryString.Add("pageSize", pageSize.Value.ToString());
}

uriBuilder.Query = queryString.ToString();
return _http.Send<ListEmailsResponse<string>>(uriBuilder.Uri, HttpMethod.Get, cancellationToken);
return _services.ListEmailsForNumber(serviceId, phoneNumber, page, pageSize, cancellationToken);
}

public Task<ListEmailsResponse<EmailAddress>> List(int? page, int? pageSize,
Expand All @@ -181,23 +164,14 @@ public Task<ListEmailsResponse<EmailAddress>> List(int? page, int? pageSize,
return _http.Send<ListEmailsResponse<EmailAddress>>(uriBuilder.Uri, HttpMethod.Get, cancellationToken);
}


public async IAsyncEnumerable<string> ListForNumberAuto(string serviceId, string phoneNumber, int? page,
public IAsyncEnumerable<string> ListForNumberAuto(string serviceId, string phoneNumber, int? page,
int? pageSize,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{
_logger?.LogDebug("Auto Listing emails");

ListEmailsResponse<string> response;
do
{
response = await ListForNumber(serviceId, phoneNumber, page, pageSize, cancellationToken);
foreach (var contact in response.Emails)
yield return contact;
page += 1;
} while (!Utils.IsLastPage(response.PageNumber, response.PageSize, response.TotalItems, PageStart.One));
return _services.ListEmailsForNumberAuto(serviceId, phoneNumber, page, pageSize, cancellationToken);
}


public async IAsyncEnumerable<EmailAddress> ListAuto(int? page, int? pageSize,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Expand Down
9 changes: 8 additions & 1 deletion src/Sinch/Fax/FaxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Sinch.Core;
using Sinch.Fax.Emails;
using Sinch.Fax.Faxes;
using Sinch.Fax.Services;
using Sinch.Logger;

namespace Sinch.Fax
Expand All @@ -17,20 +18,26 @@ public interface ISinchFax

/// <inheritdoc cref="ISinchFaxEmails" />
public ISinchFaxEmails Emails { get; }

/// <inheritdoc cref="ISinchFaxServices " />
public ISinchFaxServices Services { get; }
}

internal class FaxClient : ISinchFax
{
internal FaxClient(string projectId, Uri baseAddress, LoggerFactory? loggerFactory, IHttp http)
{
Faxes = new FaxesClient(projectId, baseAddress, loggerFactory?.Create<ISinchFaxFaxes>(), http);
Emails = new EmailsClient(projectId, baseAddress, loggerFactory?.Create<ISinchFaxEmails>(), http);
Services = new ServicesClient(projectId, baseAddress, loggerFactory?.Create<ISinchFaxServices>(), http);
Emails = new EmailsClient(projectId, baseAddress, loggerFactory?.Create<ISinchFaxEmails>(), http, Services);
}

/// <inheritdoc />
public ISinchFaxFaxes Faxes { get; }

/// <inheritdoc />
public ISinchFaxEmails Emails { get; }

public ISinchFaxServices Services { get; }
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
}
}
21 changes: 21 additions & 0 deletions src/Sinch/Fax/Faxes/CallbackUrlContentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Sinch.Core;

namespace Sinch.Fax.Faxes
{
/// <summary>
/// The content type of the callback.
/// </summary>
/// <value>The content type of the callback.</value>
[JsonConverter(typeof(EnumRecordJsonConverter<CallbackUrlContentType>))]
public record CallbackUrlContentType(string Value) : EnumRecord(Value)
{
public static readonly CallbackUrlContentType MultipartFormData = new("multipart/form-data");
public static readonly CallbackUrlContentType ApplicationJson = new("application/json");

public override string ToString()
{
return base.ToString();
}
}
}
17 changes: 0 additions & 17 deletions src/Sinch/Fax/Faxes/Fax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using Sinch.Core;

namespace Sinch.Fax.Faxes
{
Expand Down Expand Up @@ -216,20 +215,4 @@ public override string ToString()
return sb.ToString();
}
}

/// <summary>
/// The content type of the callback.
/// </summary>
/// <value>The content type of the callback.</value>
[JsonConverter(typeof(EnumRecordJsonConverter<CallbackUrlContentType>))]
public record CallbackUrlContentType(string Value) : EnumRecord(Value)
{
public static readonly CallbackUrlContentType MultipartFormData = new("multipart/form-data");
public static readonly CallbackUrlContentType ApplicationJson = new("application/json");

public override string ToString()
{
return base.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sinch.Fax.Emails
namespace Sinch.Fax
{
public class PagedResponse
{
Expand Down
108 changes: 108 additions & 0 deletions src/Sinch/Fax/Services/CreateServiceRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Text;
using System.Text.Json.Serialization;
using Sinch.Fax.Faxes;

namespace Sinch.Fax.Services
{
public sealed class CreateServiceRequest
{
/// <summary>
/// Determines how documents are converted to black and white. Value should be halftone or monochrome. Defaults to value selected on Fax Settings page
/// </summary>
[JsonPropertyName("imageConversionMethod")]
public ImageConversionMethod? ImageConversionMethod { get; set; }

/// <summary>
/// The content type of the webhook.
/// </summary>
[JsonPropertyName("webhookContentType")]
public CallbackUrlContentType? WebhookContentType { get; set; }


/// <summary>
/// ID of the fax service used.
/// </summary>
[JsonPropertyName("id")]
public string? Id { get; set; }


/// <summary>
/// A friendly name for the service. Maximum is 60 characters.
/// </summary>
[JsonPropertyName("name")]
public string? Name { get; set; }


/// <summary>
/// The URL to which Sinch will post when someone sends a fax to your Sinch number. To accept incoming faxes this must be set and your Sinch phone number must be configured to receive faxes.
/// </summary>
[JsonPropertyName("incomingWebhookUrl")]
public string? IncomingWebhookUrl { get; set; }


/// <summary>
/// If set to true this is the service used to create faxes when no serviceId is specified in the API endpoints.
/// </summary>
[JsonPropertyName("defaultForProject")]
public bool? DefaultForProject { get; set; }


/// <summary>
/// One of your sinch numbers connected to this service or any of your verified numbers
/// </summary>
[JsonPropertyName("defaultFrom")]
public string? DefaultFrom { get; set; }


/// <summary>
/// The number of times to retry sending a fax if it fails. Default is 3. Maximum is 5.
/// </summary>
[JsonPropertyName("numberOfRetries")]
public int? NumberOfRetries { get; set; }


/// <summary>
/// The number of seconds to wait between retries if the fax is not yet completed.
/// </summary>
[JsonPropertyName("retryDelaySeconds")]
public int? RetryDelaySeconds { get; set; }


/// <summary>
/// Save fax documents with sinch when you send faxes
/// </summary>
[JsonPropertyName("saveOutboundFaxDocuments")]
public bool? SaveOutboundFaxDocuments { get; set; }


/// <summary>
/// Save fax documents with sinch when you receive faxes
/// </summary>
[JsonPropertyName("saveInboundFaxDocuments")]
public bool? SaveInboundFaxDocuments { get; set; }


/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append($"class {nameof(Service)} {{\n");
sb.Append($" {nameof(Id)}: ").Append(Id).Append('\n');
sb.Append($" {nameof(Name)}: ").Append(Name).Append('\n');
sb.Append($" {nameof(IncomingWebhookUrl)}: ").Append(IncomingWebhookUrl).Append('\n');
sb.Append($" {nameof(WebhookContentType)}: ").Append(WebhookContentType).Append('\n');
sb.Append($" {nameof(DefaultForProject)}: ").Append(DefaultForProject).Append('\n');
sb.Append($" {nameof(DefaultFrom)}: ").Append(DefaultFrom).Append('\n');
sb.Append($" {nameof(NumberOfRetries)}: ").Append(NumberOfRetries).Append('\n');
sb.Append($" {nameof(RetryDelaySeconds)}: ").Append(RetryDelaySeconds).Append('\n');
sb.Append($" {nameof(ImageConversionMethod)}: ").Append(ImageConversionMethod).Append('\n');
sb.Append($" {nameof(SaveOutboundFaxDocuments)}: ").Append(SaveOutboundFaxDocuments).Append('\n');
sb.Append($" {nameof(SaveInboundFaxDocuments)}: ").Append(SaveInboundFaxDocuments).Append('\n');
sb.Append("}\n");
return sb.ToString();
}
}
}
11 changes: 11 additions & 0 deletions src/Sinch/Fax/Services/ListServicesResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Sinch.Fax.Services
{
public sealed class ListServicesResponse : PagedResponse
{
[JsonPropertyName("services")]
public List<Service> Services { get; set; } = new();
}
}
Loading
Loading