|
| 1 | +using System.Text.Json.Serialization; |
| 2 | + |
| 3 | +namespace Termii; |
| 4 | + |
| 5 | +internal sealed class TermiiMessagingClient : ITermiiMessagingClient |
| 6 | +{ |
| 7 | + private readonly TermiiJsonHttpPipeline _pipeline; |
| 8 | + |
| 9 | + public TermiiMessagingClient(TermiiJsonHttpPipeline pipeline) |
| 10 | + { |
| 11 | + _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline)); |
| 12 | + } |
| 13 | + |
| 14 | + public Task<TermiiMessageResponse> SendAsync( |
| 15 | + SendMessageRequest request, |
| 16 | + CancellationToken cancellationToken = default) |
| 17 | + { |
| 18 | + if (request is null) |
| 19 | + { |
| 20 | + throw new ArgumentNullException(nameof(request)); |
| 21 | + } |
| 22 | + |
| 23 | + request.Validate(); |
| 24 | + |
| 25 | + return _pipeline.SendJsonAsync<TermiiMessageResponse>( |
| 26 | + HttpMethod.Post, |
| 27 | + "/api/sms/send", |
| 28 | + new SendMessagePayload(request), |
| 29 | + TermiiAuthenticationLocation.Body, |
| 30 | + cancellationToken); |
| 31 | + } |
| 32 | + |
| 33 | + public Task<TermiiMessageResponse> SendBulkAsync( |
| 34 | + SendBulkMessageRequest request, |
| 35 | + CancellationToken cancellationToken = default) |
| 36 | + { |
| 37 | + if (request is null) |
| 38 | + { |
| 39 | + throw new ArgumentNullException(nameof(request)); |
| 40 | + } |
| 41 | + |
| 42 | + request.Validate(); |
| 43 | + |
| 44 | + return _pipeline.SendJsonAsync<TermiiMessageResponse>( |
| 45 | + HttpMethod.Post, |
| 46 | + "/api/sms/send/bulk", |
| 47 | + new SendBulkMessagePayload(request), |
| 48 | + TermiiAuthenticationLocation.Body, |
| 49 | + cancellationToken); |
| 50 | + } |
| 51 | + |
| 52 | + private sealed class SendMessagePayload |
| 53 | + { |
| 54 | + public SendMessagePayload(SendMessageRequest request) |
| 55 | + { |
| 56 | + To = request.To; |
| 57 | + From = request.From; |
| 58 | + Sms = request.Sms; |
| 59 | + Type = request.Type.ToWireValue(); |
| 60 | + Channel = request.Channel.ToWireValue(); |
| 61 | + Media = request.Media; |
| 62 | + } |
| 63 | + |
| 64 | + [JsonPropertyName("to")] |
| 65 | + public string To { get; } |
| 66 | + |
| 67 | + [JsonPropertyName("from")] |
| 68 | + public string From { get; } |
| 69 | + |
| 70 | + [JsonPropertyName("sms")] |
| 71 | + public string Sms { get; } |
| 72 | + |
| 73 | + [JsonPropertyName("type")] |
| 74 | + public string Type { get; } |
| 75 | + |
| 76 | + [JsonPropertyName("channel")] |
| 77 | + public string Channel { get; } |
| 78 | + |
| 79 | + [JsonPropertyName("media")] |
| 80 | + public TermiiMessageMedia? Media { get; } |
| 81 | + } |
| 82 | + |
| 83 | + private sealed class SendBulkMessagePayload |
| 84 | + { |
| 85 | + public SendBulkMessagePayload(SendBulkMessageRequest request) |
| 86 | + { |
| 87 | + To = request.To; |
| 88 | + From = request.From; |
| 89 | + Sms = request.Sms; |
| 90 | + Type = request.Type.ToWireValue(); |
| 91 | + Channel = request.Channel.ToWireValue(); |
| 92 | + } |
| 93 | + |
| 94 | + [JsonPropertyName("to")] |
| 95 | + public IReadOnlyCollection<string> To { get; } |
| 96 | + |
| 97 | + [JsonPropertyName("from")] |
| 98 | + public string From { get; } |
| 99 | + |
| 100 | + [JsonPropertyName("sms")] |
| 101 | + public string Sms { get; } |
| 102 | + |
| 103 | + [JsonPropertyName("type")] |
| 104 | + public string Type { get; } |
| 105 | + |
| 106 | + [JsonPropertyName("channel")] |
| 107 | + public string Channel { get; } |
| 108 | + } |
| 109 | +} |
0 commit comments