< Summary

Class:DCLServices.WorldsAPIService.WorldsAPIClient
Assembly:WorldsAPIService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/WorldsAPIService/WorldsAPIClient.cs
Covered lines:3
Uncovered lines:51
Coverable lines:54
Total lines:129
Line coverage:5.5% (3 of 54)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:6
Method coverage:16.6% (1 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldsAPIClient(...)0%110100%
SearchWorlds()0%42600%
GetWorlds()0%42600%
GetFavorites()0%30500%
GetWorld()0%42600%
GetWorldsByNamesList()0%72800%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/WorldsAPIService/WorldsAPIClient.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine.Networking;
 8
 9namespace DCLServices.WorldsAPIService
 10{
 11    public interface IWorldsAPIClient
 12    {
 13        UniTask<WorldsResponse.WorldsAPIResponse> SearchWorlds(string searchString, int pageNumber, int pageSize, Cancel
 14
 15        UniTask<WorldsResponse.WorldsAPIResponse> GetWorlds(int pageNumber, int pageSize, string filter = "", string sor
 16
 17        UniTask<List<WorldsResponse.WorldInfo>> GetFavorites(int pageNumber, int pageSize, CancellationToken ct);
 18
 19        UniTask<WorldsResponse.WorldInfo> GetWorld(string name, CancellationToken ct);
 20
 21        UniTask<List<WorldsResponse.WorldInfo>> GetWorldsByNamesList(List<string> namesList, CancellationToken ct);
 22    }
 23
 24    public class WorldsAPIClient : IWorldsAPIClient
 25    {
 26        private const string BASE_URL = "https://places.decentraland.org/api/worlds";
 27
 28        private readonly IWebRequestController webRequestController;
 29
 42530        public WorldsAPIClient(IWebRequestController webRequestController)
 31        {
 42532            this.webRequestController = webRequestController;
 42533        }
 34
 35        public async UniTask<WorldsResponse.WorldsAPIResponse> SearchWorlds(string searchString, int pageNumber, int pag
 36        {
 37            const string URL = BASE_URL + "?with_realms_detail=true&search={0}&offset={1}&limit={2}";
 038            var result = await webRequestController.GetAsync(string.Format(URL, searchString.Replace(" ", "+"), pageNumb
 39
 040            if (result.result != UnityWebRequest.Result.Success)
 041                throw new Exception($"Error fetching worlds info:\n{result.error}");
 42
 043            var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text);
 44
 045            if (response == null)
 046                throw new Exception($"Error parsing world info:\n{result.downloadHandler.text}");
 47
 048            if (response.data == null)
 049                throw new Exception($"No world info retrieved:\n{result.downloadHandler.text}");
 50
 051            return response;
 052        }
 53
 54        public async UniTask<WorldsResponse.WorldsAPIResponse> GetWorlds(int pageNumber, int pageSize, string filter = "
 55        {
 56            const string URL = BASE_URL + "?order_by={3}&order=desc&with_realms_detail=true&offset={0}&limit={1}&{2}";
 057            var result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageSize, filter,
 58
 059            if (result.result != UnityWebRequest.Result.Success)
 060                throw new Exception($"Error fetching worlds info:\n{result.error}");
 61
 062            var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text);
 63
 064            if (response == null)
 065                throw new Exception($"Error parsing word info:\n{result.downloadHandler.text}");
 66
 067            if (response.data == null)
 068                throw new Exception($"No world info retrieved:\n{result.downloadHandler.text}");
 69
 070            return response;
 071        }
 72
 73        public async UniTask<List<WorldsResponse.WorldInfo>> GetFavorites(int pageNumber, int pageSize, CancellationToke
 74        {
 75            const string URL = BASE_URL + "?only_favorites=true&with_realms_detail=true&offset={0}&limit={1}";
 076            UnityWebRequest result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageS
 077            var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text);
 78
 079            if (response == null)
 080                throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}");
 81
 082            if (response.data == null)
 083                throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}");
 84
 085            return response.data;
 086        }
 87
 88        public async UniTask<WorldsResponse.WorldInfo> GetWorld(string name, CancellationToken ct)
 89        {
 90            const string URL = BASE_URL + "?names={0}&with_realms_detail=true";
 091            var result = await webRequestController.GetAsync(string.Format(URL, name), cancellationToken: ct, isSigned: 
 92
 093            if (result.result != UnityWebRequest.Result.Success)
 094                throw new Exception($"Error fetching world info:\n{result.error}");
 95
 096            var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text);
 97
 098            if (response == null)
 099                throw new Exception($"Error parsing world info:\n{result.downloadHandler.text}");
 100
 0101            if (response.data.Count == 0)
 0102                throw new NotAWorldException(name);
 103
 0104            return response.data[0];
 0105        }
 106
 107        public async UniTask<List<WorldsResponse.WorldInfo>> GetWorldsByNamesList(List<string> namesList, CancellationTo
 108        {
 0109            if (namesList.Count == 0)
 0110                return new List<WorldsResponse.WorldInfo>();
 111
 0112            var url = string.Concat(BASE_URL, "?");
 0113            foreach (string name in namesList)
 0114                url = string.Concat(url, $"names={name}&with_realms_detail=true&");
 115
 0116            var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true);
 117
 0118            if (result.result != UnityWebRequest.Result.Success)
 0119                throw new Exception($"Error fetching worlds info:\n{result.error}");
 120
 0121            var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text);
 122
 0123            if (response == null)
 0124                throw new Exception($"Error parsing worlds info:\n{result.downloadHandler.text}");
 125
 0126            return response.data;
 0127        }
 128    }
 129}