< Summary

Class:DCLServices.PlacesAPIService.PlacesAPIClient
Assembly:PlacesAPIService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/PlacesAPIService/PlacesAPIClient.cs
Covered lines:9
Uncovered lines:104
Coverable lines:113
Total lines:252
Line coverage:7.9% (9 of 113)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:13
Method coverage:15.3% (2 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlacesAPIClient(...)0%110100%
SearchPlaces()0%42600%
GetMostActivePlaces()0%42600%
GetPlace()0%42600%
GetPlace()0%56700%
GetPlacesByCoordsList()0%72800%
GetFavorites()0%30500%
GetAllFavorites()0%30500%
SetPlaceFavorite()0%42600%
SetPlaceVote()0%56700%
GetPointsOfInterestCoords()0%5.25080%
ReportPlace()0%1321100%
GetPlaceCategories()0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/PlacesAPIService/PlacesAPIClient.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using MainScripts.DCL.Controllers.HotScenes;
 5using System;
 6using System.Collections.Generic;
 7using System.Threading;
 8using UnityEngine;
 9using UnityEngine.Networking;
 10
 11namespace DCLServices.PlacesAPIService
 12{
 13    public interface IPlacesAPIClient
 14    {
 15        UniTask<IHotScenesController.PlacesAPIResponse> SearchPlaces(string searchString, int pageNumber, int pageSize, 
 16        UniTask<IHotScenesController.PlacesAPIResponse> GetMostActivePlaces(int pageNumber, int pageSize, string filter 
 17        UniTask<IHotScenesController.PlaceInfo> GetPlace(Vector2Int coords, CancellationToken ct);
 18        UniTask<IHotScenesController.PlaceInfo> GetPlace(string placeUUID, CancellationToken ct);
 19        UniTask<List<IHotScenesController.PlaceInfo>> GetFavorites(int pageNumber, int pageSize, CancellationToken ct);
 20        UniTask<List<IHotScenesController.PlaceInfo>> GetAllFavorites(CancellationToken ct);
 21        UniTask<List<IHotScenesController.PlaceInfo>> GetPlacesByCoordsList(List<Vector2Int> coordsList, CancellationTok
 22        UniTask ReportPlace(PlaceContentReportPayload placeContentReportPayload, CancellationToken ct);
 23        UniTask<List<PlaceCategoryInfo>> GetPlaceCategories(CancellationToken ct);
 24
 25
 26        UniTask SetPlaceFavorite(string placeUUID, bool isFavorite, CancellationToken ct);
 27        UniTask SetPlaceVote(bool? isUpvote, string placeUUID, CancellationToken ct);
 28        UniTask<List<string>> GetPointsOfInterestCoords(CancellationToken ct);
 29    }
 30
 31    public class PlacesAPIClient: IPlacesAPIClient
 32    {
 33        private const string BASE_URL = "https://places.decentraland.org/api/places";
 34        private const string BASE_URL_ZONE = "https://places.decentraland.zone/api/places";
 35        private const string POI_URL = "https://dcl-lists.decentraland.org/pois";
 36        private const string CONTENT_MODERATION_REPORT_URL = "https://places.decentraland.org/api/report";
 37        private const string PLACE_CATEGORIES_URL = "https://places.decentraland.org/api/categories";
 38        private readonly IWebRequestController webRequestController;
 39
 42540        public PlacesAPIClient(IWebRequestController webRequestController)
 41        {
 42542            this.webRequestController = webRequestController;
 42543        }
 44
 45        public async UniTask<IHotScenesController.PlacesAPIResponse> SearchPlaces(string searchString, int pageNumber, i
 46        {
 47            const string URL = BASE_URL + "?with_realms_detail=true&search={0}&offset={1}&limit={2}";
 048            var result = await webRequestController.GetAsync(string.Format(URL, searchString.Replace(" ", "+"), pageNumb
 49
 050            if (result.result != UnityWebRequest.Result.Success)
 051                throw new Exception($"Error fetching most active places info:\n{result.error}");
 52
 053            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 54
 055            if (response == null)
 056                throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}");
 57
 058            if (response.data == null)
 059                throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}");
 60
 061            return response;
 062        }
 63
 64        public async UniTask<IHotScenesController.PlacesAPIResponse> GetMostActivePlaces(int pageNumber, int pageSize, s
 65        {
 66            const string URL = BASE_URL + "?order_by={3}&order=desc&with_realms_detail=true&offset={0}&limit={1}&{2}";
 067            var result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageSize, filter,
 68
 069            if (result.result != UnityWebRequest.Result.Success)
 070                throw new Exception($"Error fetching most active places info:\n{result.error}");
 71
 072            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 73
 074            if (response == null)
 075                throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}");
 76
 077            if (response.data == null)
 078                throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}");
 79
 080            return response;
 081        }
 82
 83        public async UniTask<IHotScenesController.PlaceInfo> GetPlace(Vector2Int coords, CancellationToken ct)
 84        {
 85            const string URL = BASE_URL + "?positions={0},{1}&with_realms_detail=true";
 086            var result = await webRequestController.GetAsync(string.Format(URL, coords.x, coords.y), cancellationToken: 
 87
 088            if (result.result != UnityWebRequest.Result.Success)
 089                throw new Exception($"Error fetching place info:\n{result.error}");
 90
 091            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 92
 093            if (response == null)
 094                throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}");
 95
 096            if (response.data.Count == 0)
 097                throw new NotAPlaceException(coords);
 98
 099            return response.data[0];
 0100        }
 101
 102        public async UniTask<IHotScenesController.PlaceInfo> GetPlace(string placeUUID, CancellationToken ct)
 103        {
 0104            var url = $"{BASE_URL}/{placeUUID}?with_realms_detail=true";
 0105            var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true);
 106
 0107            if (result.result != UnityWebRequest.Result.Success)
 0108                throw new Exception($"Error fetching place info:\n{result.error}");
 109
 0110            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIGetParcelResponse>(result.downloadHandler.te
 111
 0112            if (response == null)
 0113                throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}");
 114
 0115            if (response.ok == false)
 0116                throw new NotAPlaceException(placeUUID);
 117
 0118            if (response.data == null)
 0119                throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}");
 120
 0121            return response.data;
 0122        }
 123
 124        public async UniTask<List<IHotScenesController.PlaceInfo>> GetPlacesByCoordsList(List<Vector2Int> coordsList, Ca
 125        {
 0126            if (coordsList.Count == 0)
 0127                return new List<IHotScenesController.PlaceInfo>();
 128
 0129            var url = string.Concat(BASE_URL, "?");
 0130            foreach (Vector2Int coords in coordsList)
 0131                url = string.Concat(url, $"positions={coords.x},{coords.y}&with_realms_detail=true&");
 132
 0133            var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true);
 134
 0135            if (result.result != UnityWebRequest.Result.Success)
 0136                throw new Exception($"Error fetching places info:\n{result.error}");
 137
 0138            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 139
 0140            if (response == null)
 0141                throw new Exception($"Error parsing places info:\n{result.downloadHandler.text}");
 142
 0143            return response.data;
 0144        }
 145
 146        public async UniTask<List<IHotScenesController.PlaceInfo>> GetFavorites(int pageNumber, int pageSize, Cancellati
 147        {
 148            const string URL = BASE_URL + "?only_favorites=true&with_realms_detail=true&offset={0}&limit={1}";
 149
 0150            UnityWebRequest result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageS
 0151            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 152
 0153            if (response == null)
 0154                throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}");
 155
 0156            if (response.data == null)
 0157                throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}");
 158
 0159            return response.data;
 0160        }
 161
 162        public async UniTask<List<IHotScenesController.PlaceInfo>> GetAllFavorites(CancellationToken ct)
 163        {
 164            const string URL = BASE_URL + "?only_favorites=true&with_realms_detail=true";
 165
 0166            UnityWebRequest result = await webRequestController.GetAsync(URL, isSigned: true, cancellationToken: ct);
 0167            var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text);
 168
 0169            if (response == null)
 0170                throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}");
 171
 0172            if (response.data == null)
 0173                throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}");
 174
 0175            return response.data;
 0176        }
 177
 178        public async UniTask SetPlaceFavorite(string placeUUID, bool isFavorite, CancellationToken ct)
 179        {
 180            const string URL = BASE_URL + "/{0}/favorites";
 181            const string FAVORITE_PAYLOAD = "{\"favorites\": true}";
 182            const string NOT_FAVORITE_PAYLOAD = "{\"favorites\": false}";
 0183            var result = await webRequestController.PatchAsync(string.Format(URL, placeUUID), isFavorite ? FAVORITE_PAYL
 0184            if (result.result != UnityWebRequest.Result.Success)
 0185                throw new Exception($"Error fetching place info:\n{result.error}");
 0186        }
 187
 188        public async UniTask SetPlaceVote(bool? isUpvote, string placeUUID, CancellationToken ct)
 189        {
 190            const string URL = BASE_URL + "/{0}/likes";
 191            const string LIKE_PAYLOAD = "{\"like\": true}";
 192            const string DISLIKE_PAYLOAD = "{\"like\": false}";
 193            const string NO_LIKE_PAYLOAD = "{\"like\": null}";
 194            string payload;
 195
 0196            if (isUpvote == null)
 0197                payload = NO_LIKE_PAYLOAD;
 198            else
 0199                payload = isUpvote == true ? LIKE_PAYLOAD : DISLIKE_PAYLOAD;
 200
 0201            var result = await webRequestController.PatchAsync(string.Format(URL, placeUUID), payload, isSigned: true, c
 0202            if (result.result != UnityWebRequest.Result.Success)
 0203                throw new Exception($"Error fetching place info:\n{result.error}");
 0204        }
 205
 206        public async UniTask<List<string>> GetPointsOfInterestCoords(CancellationToken ct)
 207        {
 3208            UnityWebRequest result = await webRequestController.PostAsync(POI_URL, "", isSigned: false, cancellationToke
 1209            var response = Utils.SafeFromJson<PointsOfInterestCoordsAPIResponse>(result.downloadHandler.text);
 210
 1211            if (response == null)
 0212                throw new Exception($"Error parsing get POIs response:\n{result.downloadHandler.text}");
 213
 1214            if (response.data == null)
 0215                throw new Exception($"No POIs info retrieved:\n{result.downloadHandler.text}");
 216
 1217            return response.data;
 1218        }
 219
 220        public async UniTask ReportPlace(PlaceContentReportPayload placeContentReportPayload, CancellationToken ct)
 221        {
 222            // POST for getting a signed url
 0223            UnityWebRequest postResult = await webRequestController.PostAsync(CONTENT_MODERATION_REPORT_URL, "", isSigne
 0224            if (postResult.result != UnityWebRequest.Result.Success)
 0225                throw new Exception($"Error reporting place:\n{postResult.error}");
 226
 0227            var postResponse = Utils.SafeFromJson<ReportPlaceAPIResponse>(postResult.downloadHandler.text);
 0228            if (postResponse?.data == null || !postResponse.ok)
 0229                throw new Exception($"Error reporting place:\n{postResult.downloadHandler.text}");
 230
 231            // PUT using the gotten signed url and sending the report payload
 0232            string putData = JsonUtility.ToJson(placeContentReportPayload);
 0233            UnityWebRequest putResult = await webRequestController.PutAsync(postResponse.data.signed_url, putData, isSig
 0234            if (putResult.result != UnityWebRequest.Result.Success)
 0235                throw new Exception($"Error reporting place:\n{putResult.error}");
 0236        }
 237
 238        public async UniTask<List<PlaceCategoryInfo>> GetPlaceCategories(CancellationToken ct)
 239        {
 0240            UnityWebRequest result = await webRequestController.GetAsync(PLACE_CATEGORIES_URL, isSigned: false, cancella
 0241            var response = Utils.SafeFromJson<PlaceCategoriesAPIResponse>(result.downloadHandler.text);
 242
 0243            if (response == null)
 0244                throw new Exception($"Error parsing get place categories response:\n{result.downloadHandler.text}");
 245
 0246            if (response.data == null)
 0247                throw new Exception($"No place categories info retrieved:\n{result.downloadHandler.text}");
 248
 0249            return response.data;
 0250        }
 251    }
 252}