Skip to content

Commit d0a0a2d

Browse files
Merge pull request #90 from StanDotNet/master
feat(CodePush): added same response to Apn and Firebase senders
2 parents f5c6494 + 4938a53 commit d0a0a2d

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

CorePush/Apple/ApnSender.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Org.BouncyCastle.Security;
1212

1313
using CorePush.Interfaces;
14+
using CorePush.Models;
1415
using CorePush.Utils;
1516
using CorePush.Serialization;
1617

@@ -65,7 +66,7 @@ public ApnSender(ApnSettings settings, HttpClient http, IJsonSerializer serializ
6566
/// to receive too many requests and may occasionally respond with HTTP 429. Just try/catch this call and retry as needed.
6667
/// </summary>
6768
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
68-
public async Task<ApnsResponse> SendAsync(
69+
public async Task<PushResult> SendAsync(
6970
object notification,
7071
string deviceToken,
7172
string apnsId = null,
@@ -96,15 +97,13 @@ public async Task<ApnsResponse> SendAsync(
9697
}
9798

9899
using var response = await http.SendAsync(message, cancellationToken);
99-
100-
var success = response.IsSuccessStatusCode;
100+
101101
var content = await response.Content.ReadAsStringAsync(cancellationToken);
102+
var error = response.IsSuccessStatusCode
103+
? null
104+
: serializer.Deserialize<ApnsError>(content).Reason;
102105

103-
return new ApnsResponse
104-
{
105-
IsSuccess = success,
106-
Error = success ? null : serializer.Deserialize<ApnsError>(content)
107-
};
106+
return new PushResult((int)response.StatusCode, response.IsSuccessStatusCode, content, error);
108107
}
109108

110109
private string GetJwtToken()

CorePush/Firebase/FirebaseError.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CorePush.Firebase;
2+
3+
public class FirebaseError
4+
{
5+
public string Message { get; set; }
6+
public string Status { get; set; }
7+
public int Code { get; set; }
8+
}

CorePush/Firebase/FirebaseResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
public class FirebaseResponse
44
{
55
public string Name { get; set; }
6+
public FirebaseError Error { get; set; }
67
}

CorePush/Firebase/FirebaseSender.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77

88
using CorePush.Interfaces;
9+
using CorePush.Models;
910
using CorePush.Serialization;
1011
using CorePush.Utils;
1112

@@ -86,7 +87,7 @@ public FirebaseSender(FirebaseSettings settings, HttpClient http, IJsonSerialize
8687
/// <param name="payload">Notification payload that will be serialized using Newtonsoft.Json package</param>
8788
/// <param name="cancellationToken">Cancellation token</param>
8889
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
89-
public async Task<FirebaseResponse> SendAsync(object payload, CancellationToken cancellationToken = default)
90+
public async Task<PushResult> SendAsync(object payload, CancellationToken cancellationToken = default)
9091
{
9192
var json = serializer.Serialize(payload);
9293

@@ -102,12 +103,12 @@ public async Task<FirebaseResponse> SendAsync(object payload, CancellationToken
102103
using var response = await http.SendAsync(message, cancellationToken);
103104
var responseString = await response.Content.ReadAsStringAsync(cancellationToken);
104105

105-
if (!response.IsSuccessStatusCode)
106-
{
107-
throw new HttpRequestException("Firebase notification error: " + responseString);
108-
}
109-
110-
return serializer.Deserialize<FirebaseResponse>(responseString);
106+
var firebaseResponse = serializer.Deserialize<FirebaseResponse>(responseString);
107+
108+
return new PushResult((int) response.StatusCode,
109+
response.IsSuccessStatusCode,
110+
firebaseResponse.Name ?? firebaseResponse.Error?.Message,
111+
responseString);
111112
}
112113

113114
private async Task<string> GetJwtTokenAsync()

CorePush/Interfaces/IApnSender.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using System.Threading.Tasks;
33

44
using CorePush.Apple;
5+
using CorePush.Models;
56

67
namespace CorePush.Interfaces;
78

89
public interface IApnSender
910
{
10-
Task<ApnsResponse> SendAsync(
11+
Task<PushResult> SendAsync(
1112
object notification,
1213
string deviceToken,
1314
string apnsId = null,

CorePush/Interfaces/IFirebaseSender.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Threading.Tasks;
33

44
using CorePush.Firebase;
5+
using CorePush.Models;
56

67
namespace CorePush.Interfaces;
78

89
public interface IFirebaseSender
910
{
10-
Task<FirebaseResponse> SendAsync(object payload, CancellationToken cancellationToken = default);
11+
Task<PushResult> SendAsync(object payload, CancellationToken cancellationToken = default);
1112
}

CorePush/Models/PushResult.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CorePush.Models;
2+
3+
public record PushResult(
4+
int StatusCode,
5+
bool IsSuccessStatusCode,
6+
string Message,
7+
string Error);

0 commit comments

Comments
 (0)