|
| 1 | +// Copyright 2018, Google Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | + |
| 17 | +namespace FirebaseAdmin.Messaging |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// The result of an individual send operation that was executed as part of a batch. See |
| 21 | + /// <see cref="BatchResponse"/> for more details. |
| 22 | + /// </summary> |
| 23 | + public sealed class SendResponse |
| 24 | + { |
| 25 | + private SendResponse(string messageId) |
| 26 | + { |
| 27 | + this.MessageId = messageId; |
| 28 | + } |
| 29 | + |
| 30 | + private SendResponse(FirebaseMessagingException exception) |
| 31 | + { |
| 32 | + this.Exception = exception; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets a message ID string if the send operation was successful. Otherwise returns null. |
| 37 | + /// </summary> |
| 38 | + public string MessageId { get; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets an exception if the send operation failed. Otherwise returns null. |
| 42 | + /// </summary> |
| 43 | + public FirebaseMessagingException Exception { get; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets a value indicating whether the send operation was successful or not. When this property |
| 47 | + /// is <see langword="true"/>, <see cref="MessageId"/> is guaranteed to return a |
| 48 | + /// non-<see langword="null"/> value. When this property is <see langword="false"/>, |
| 49 | + /// <see cref="Exception"/> is guaranteed to return a non-<see langword="null"/> value. |
| 50 | + /// </summary> |
| 51 | + public bool IsSuccessful => !string.IsNullOrEmpty(this.MessageId); |
| 52 | + |
| 53 | + internal static SendResponse FromMessageId(string messageId) |
| 54 | + { |
| 55 | + if (messageId == null) |
| 56 | + { |
| 57 | + throw new ArgumentNullException(nameof(messageId)); |
| 58 | + } |
| 59 | + |
| 60 | + if (messageId == string.Empty) |
| 61 | + { |
| 62 | + throw new ArgumentException("Cannot be empty.", nameof(messageId)); |
| 63 | + } |
| 64 | + |
| 65 | + return new SendResponse(messageId); |
| 66 | + } |
| 67 | + |
| 68 | + internal static SendResponse FromException(FirebaseMessagingException exception) |
| 69 | + { |
| 70 | + if (exception == null) |
| 71 | + { |
| 72 | + throw new ArgumentNullException(nameof(exception)); |
| 73 | + } |
| 74 | + |
| 75 | + return new SendResponse(exception); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments