< Summary

Class:DCLServices.SubscriptionsAPIService.SubscriptionsAPIClient
Assembly:SubscriptionsAPIService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/SubscriptionsAPIService/SubscriptionsAPIClient.cs
Covered lines:3
Uncovered lines:21
Coverable lines:24
Total lines:81
Line coverage:12.5% (3 of 24)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:4
Method coverage:25% (1 of 4)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SubscriptionsAPIClient(...)0%110100%
CreateSubscription()0%90900%
DeleteSubscription()0%20400%
GetSubscription()0%90900%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/SubscriptionsAPIService/SubscriptionsAPIClient.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine;
 8using UnityEngine.Networking;
 9
 10namespace 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
 42526        public SubscriptionsAPIClient(IWebRequestController webRequestController)
 27        {
 42528            this.webRequestController = webRequestController;
 42529        }
 30
 31        public async UniTask<Subscription> CreateSubscription(string email, CancellationToken ct)
 32        {
 033            string postData = JsonUtility.ToJson(new CreateSubscriptionPayload
 34            {
 35                email = email,
 36                source = UTM_SOURCE,
 37            });
 38
 039            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
 045            if (postResult.result != UnityWebRequest.Result.Success)
 046                throw new Exception($"Error creating subscription:\n{postResult.error}");
 47
 048            var postResponse = Utils.SafeFromJson<SubscriptionAPIResponse>(postResult.downloadHandler.text);
 049            if (postResponse?.data?.data == null)
 050                throw new Exception($"Error creating subscription:\n{postResult.downloadHandler.text}");
 51
 052            return postResponse.data.data;
 053        }
 54
 55        public async UniTask DeleteSubscription(string subscriptionId, CancellationToken ct)
 56        {
 057            UnityWebRequest deleteResult = await webRequestController.DeleteAsync(
 58                url: $"{BASE_SUBSCRIPTION_URL}/{subscriptionId}",
 59                cancellationToken: ct);
 60
 061            if (deleteResult.result != UnityWebRequest.Result.Success)
 062                throw new Exception($"Error deleting subscription:\n{deleteResult.error}");
 063        }
 64
 65        public async UniTask<Subscription> GetSubscription(string subscriptionId, CancellationToken ct)
 66        {
 067            UnityWebRequest getResult = await webRequestController.GetAsync(
 68                url: $"{BASE_SUBSCRIPTION_URL}/{subscriptionId}",
 69                cancellationToken: ct);
 70
 071            if (getResult.result != UnityWebRequest.Result.Success)
 072                throw new Exception($"Error getting subscription:\n{getResult.error}");
 73
 074            var getResponse = Utils.SafeFromJson<SubscriptionAPIResponse>(getResult.downloadHandler.text);
 075            if (getResponse?.data?.data == null)
 076                throw new Exception($"Error parsing get subscription response:\n{getResult.downloadHandler.text}");
 77
 078            return getResponse.data.data;
 079        }
 80    }
 81}