| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Networking; |
| | 9 | |
|
| | 10 | | namespace DCLServices.SubscriptionsAPIService |
| | 11 | | { |
| | 12 | | public interface ISubscriptionsAPIClient |
| | 13 | | { |
| | 14 | | UniTask<Subscription> CreateSubscription(string email, CancellationToken ct); |
| | 15 | | UniTask DeleteSubscription(string subscriptionId, CancellationToken ct); |
| | 16 | | UniTask<Subscription> GetSubscription(string subscriptionId, CancellationToken ct); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class SubscriptionsAPIClient : ISubscriptionsAPIClient |
| | 20 | | { |
| | 21 | | private const string UTM_SOURCE = "explorer"; |
| | 22 | | private const string BASE_SUBSCRIPTION_URL = "https://builder-api.decentraland.org/v1/newsletter"; |
| | 23 | |
|
| | 24 | | private readonly IWebRequestController webRequestController; |
| | 25 | |
|
| 425 | 26 | | public SubscriptionsAPIClient(IWebRequestController webRequestController) |
| | 27 | | { |
| 425 | 28 | | this.webRequestController = webRequestController; |
| 425 | 29 | | } |
| | 30 | |
|
| | 31 | | public async UniTask<Subscription> CreateSubscription(string email, CancellationToken ct) |
| | 32 | | { |
| 0 | 33 | | string postData = JsonUtility.ToJson(new CreateSubscriptionPayload |
| | 34 | | { |
| | 35 | | email = email, |
| | 36 | | source = UTM_SOURCE, |
| | 37 | | }); |
| | 38 | |
|
| 0 | 39 | | UnityWebRequest postResult = await webRequestController.PostAsync( |
| | 40 | | url: BASE_SUBSCRIPTION_URL, |
| | 41 | | postData: postData, |
| | 42 | | cancellationToken: ct, |
| | 43 | | headers: new Dictionary<string, string> { { "Content-Type", "application/json" } }); |
| | 44 | |
|
| 0 | 45 | | if (postResult.result != UnityWebRequest.Result.Success) |
| 0 | 46 | | throw new Exception($"Error creating subscription:\n{postResult.error}"); |
| | 47 | |
|
| 0 | 48 | | var postResponse = Utils.SafeFromJson<SubscriptionAPIResponse>(postResult.downloadHandler.text); |
| 0 | 49 | | if (postResponse?.data?.data == null) |
| 0 | 50 | | throw new Exception($"Error creating subscription:\n{postResult.downloadHandler.text}"); |
| | 51 | |
|
| 0 | 52 | | return postResponse.data.data; |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public async UniTask DeleteSubscription(string subscriptionId, CancellationToken ct) |
| | 56 | | { |
| 0 | 57 | | UnityWebRequest deleteResult = await webRequestController.DeleteAsync( |
| | 58 | | url: $"{BASE_SUBSCRIPTION_URL}/{subscriptionId}", |
| | 59 | | cancellationToken: ct); |
| | 60 | |
|
| 0 | 61 | | if (deleteResult.result != UnityWebRequest.Result.Success) |
| 0 | 62 | | throw new Exception($"Error deleting subscription:\n{deleteResult.error}"); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async UniTask<Subscription> GetSubscription(string subscriptionId, CancellationToken ct) |
| | 66 | | { |
| 0 | 67 | | UnityWebRequest getResult = await webRequestController.GetAsync( |
| | 68 | | url: $"{BASE_SUBSCRIPTION_URL}/{subscriptionId}", |
| | 69 | | cancellationToken: ct); |
| | 70 | |
|
| 0 | 71 | | if (getResult.result != UnityWebRequest.Result.Success) |
| 0 | 72 | | throw new Exception($"Error getting subscription:\n{getResult.error}"); |
| | 73 | |
|
| 0 | 74 | | var getResponse = Utils.SafeFromJson<SubscriptionAPIResponse>(getResult.downloadHandler.text); |
| 0 | 75 | | if (getResponse?.data?.data == null) |
| 0 | 76 | | throw new Exception($"Error parsing get subscription response:\n{getResult.downloadHandler.text}"); |
| | 77 | |
|
| 0 | 78 | | return getResponse.data.data; |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | | } |