| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | |
|
| | 11 | | namespace 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 | |
|
| 425 | 40 | | public PlacesAPIClient(IWebRequestController webRequestController) |
| | 41 | | { |
| 425 | 42 | | this.webRequestController = webRequestController; |
| 425 | 43 | | } |
| | 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}"; |
| 0 | 48 | | var result = await webRequestController.GetAsync(string.Format(URL, searchString.Replace(" ", "+"), pageNumb |
| | 49 | |
|
| 0 | 50 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 51 | | throw new Exception($"Error fetching most active places info:\n{result.error}"); |
| | 52 | |
|
| 0 | 53 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 54 | |
|
| 0 | 55 | | if (response == null) |
| 0 | 56 | | throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}"); |
| | 57 | |
|
| 0 | 58 | | if (response.data == null) |
| 0 | 59 | | throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}"); |
| | 60 | |
|
| 0 | 61 | | return response; |
| 0 | 62 | | } |
| | 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}"; |
| 0 | 67 | | var result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageSize, filter, |
| | 68 | |
|
| 0 | 69 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 70 | | throw new Exception($"Error fetching most active places info:\n{result.error}"); |
| | 71 | |
|
| 0 | 72 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 73 | |
|
| 0 | 74 | | if (response == null) |
| 0 | 75 | | throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}"); |
| | 76 | |
|
| 0 | 77 | | if (response.data == null) |
| 0 | 78 | | throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}"); |
| | 79 | |
|
| 0 | 80 | | return response; |
| 0 | 81 | | } |
| | 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"; |
| 0 | 86 | | var result = await webRequestController.GetAsync(string.Format(URL, coords.x, coords.y), cancellationToken: |
| | 87 | |
|
| 0 | 88 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 89 | | throw new Exception($"Error fetching place info:\n{result.error}"); |
| | 90 | |
|
| 0 | 91 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 92 | |
|
| 0 | 93 | | if (response == null) |
| 0 | 94 | | throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}"); |
| | 95 | |
|
| 0 | 96 | | if (response.data.Count == 0) |
| 0 | 97 | | throw new NotAPlaceException(coords); |
| | 98 | |
|
| 0 | 99 | | return response.data[0]; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public async UniTask<IHotScenesController.PlaceInfo> GetPlace(string placeUUID, CancellationToken ct) |
| | 103 | | { |
| 0 | 104 | | var url = $"{BASE_URL}/{placeUUID}?with_realms_detail=true"; |
| 0 | 105 | | var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true); |
| | 106 | |
|
| 0 | 107 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 108 | | throw new Exception($"Error fetching place info:\n{result.error}"); |
| | 109 | |
|
| 0 | 110 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIGetParcelResponse>(result.downloadHandler.te |
| | 111 | |
|
| 0 | 112 | | if (response == null) |
| 0 | 113 | | throw new Exception($"Error parsing place info:\n{result.downloadHandler.text}"); |
| | 114 | |
|
| 0 | 115 | | if (response.ok == false) |
| 0 | 116 | | throw new NotAPlaceException(placeUUID); |
| | 117 | |
|
| 0 | 118 | | if (response.data == null) |
| 0 | 119 | | throw new Exception($"No place info retrieved:\n{result.downloadHandler.text}"); |
| | 120 | |
|
| 0 | 121 | | return response.data; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public async UniTask<List<IHotScenesController.PlaceInfo>> GetPlacesByCoordsList(List<Vector2Int> coordsList, Ca |
| | 125 | | { |
| 0 | 126 | | if (coordsList.Count == 0) |
| 0 | 127 | | return new List<IHotScenesController.PlaceInfo>(); |
| | 128 | |
|
| 0 | 129 | | var url = string.Concat(BASE_URL, "?"); |
| 0 | 130 | | foreach (Vector2Int coords in coordsList) |
| 0 | 131 | | url = string.Concat(url, $"positions={coords.x},{coords.y}&with_realms_detail=true&"); |
| | 132 | |
|
| 0 | 133 | | var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true); |
| | 134 | |
|
| 0 | 135 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 136 | | throw new Exception($"Error fetching places info:\n{result.error}"); |
| | 137 | |
|
| 0 | 138 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 139 | |
|
| 0 | 140 | | if (response == null) |
| 0 | 141 | | throw new Exception($"Error parsing places info:\n{result.downloadHandler.text}"); |
| | 142 | |
|
| 0 | 143 | | return response.data; |
| 0 | 144 | | } |
| | 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 | |
|
| 0 | 150 | | UnityWebRequest result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageS |
| 0 | 151 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 152 | |
|
| 0 | 153 | | if (response == null) |
| 0 | 154 | | throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}"); |
| | 155 | |
|
| 0 | 156 | | if (response.data == null) |
| 0 | 157 | | throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}"); |
| | 158 | |
|
| 0 | 159 | | return response.data; |
| 0 | 160 | | } |
| | 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 | |
|
| 0 | 166 | | UnityWebRequest result = await webRequestController.GetAsync(URL, isSigned: true, cancellationToken: ct); |
| 0 | 167 | | var response = Utils.SafeFromJson<IHotScenesController.PlacesAPIResponse>(result.downloadHandler.text); |
| | 168 | |
|
| 0 | 169 | | if (response == null) |
| 0 | 170 | | throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}"); |
| | 171 | |
|
| 0 | 172 | | if (response.data == null) |
| 0 | 173 | | throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}"); |
| | 174 | |
|
| 0 | 175 | | return response.data; |
| 0 | 176 | | } |
| | 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}"; |
| 0 | 183 | | var result = await webRequestController.PatchAsync(string.Format(URL, placeUUID), isFavorite ? FAVORITE_PAYL |
| 0 | 184 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 185 | | throw new Exception($"Error fetching place info:\n{result.error}"); |
| 0 | 186 | | } |
| | 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 | |
|
| 0 | 196 | | if (isUpvote == null) |
| 0 | 197 | | payload = NO_LIKE_PAYLOAD; |
| | 198 | | else |
| 0 | 199 | | payload = isUpvote == true ? LIKE_PAYLOAD : DISLIKE_PAYLOAD; |
| | 200 | |
|
| 0 | 201 | | var result = await webRequestController.PatchAsync(string.Format(URL, placeUUID), payload, isSigned: true, c |
| 0 | 202 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 203 | | throw new Exception($"Error fetching place info:\n{result.error}"); |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | public async UniTask<List<string>> GetPointsOfInterestCoords(CancellationToken ct) |
| | 207 | | { |
| 3 | 208 | | UnityWebRequest result = await webRequestController.PostAsync(POI_URL, "", isSigned: false, cancellationToke |
| 1 | 209 | | var response = Utils.SafeFromJson<PointsOfInterestCoordsAPIResponse>(result.downloadHandler.text); |
| | 210 | |
|
| 1 | 211 | | if (response == null) |
| 0 | 212 | | throw new Exception($"Error parsing get POIs response:\n{result.downloadHandler.text}"); |
| | 213 | |
|
| 1 | 214 | | if (response.data == null) |
| 0 | 215 | | throw new Exception($"No POIs info retrieved:\n{result.downloadHandler.text}"); |
| | 216 | |
|
| 1 | 217 | | return response.data; |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | public async UniTask ReportPlace(PlaceContentReportPayload placeContentReportPayload, CancellationToken ct) |
| | 221 | | { |
| | 222 | | // POST for getting a signed url |
| 0 | 223 | | UnityWebRequest postResult = await webRequestController.PostAsync(CONTENT_MODERATION_REPORT_URL, "", isSigne |
| 0 | 224 | | if (postResult.result != UnityWebRequest.Result.Success) |
| 0 | 225 | | throw new Exception($"Error reporting place:\n{postResult.error}"); |
| | 226 | |
|
| 0 | 227 | | var postResponse = Utils.SafeFromJson<ReportPlaceAPIResponse>(postResult.downloadHandler.text); |
| 0 | 228 | | if (postResponse?.data == null || !postResponse.ok) |
| 0 | 229 | | throw new Exception($"Error reporting place:\n{postResult.downloadHandler.text}"); |
| | 230 | |
|
| | 231 | | // PUT using the gotten signed url and sending the report payload |
| 0 | 232 | | string putData = JsonUtility.ToJson(placeContentReportPayload); |
| 0 | 233 | | UnityWebRequest putResult = await webRequestController.PutAsync(postResponse.data.signed_url, putData, isSig |
| 0 | 234 | | if (putResult.result != UnityWebRequest.Result.Success) |
| 0 | 235 | | throw new Exception($"Error reporting place:\n{putResult.error}"); |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | public async UniTask<List<PlaceCategoryInfo>> GetPlaceCategories(CancellationToken ct) |
| | 239 | | { |
| 0 | 240 | | UnityWebRequest result = await webRequestController.GetAsync(PLACE_CATEGORIES_URL, isSigned: false, cancella |
| 0 | 241 | | var response = Utils.SafeFromJson<PlaceCategoriesAPIResponse>(result.downloadHandler.text); |
| | 242 | |
|
| 0 | 243 | | if (response == null) |
| 0 | 244 | | throw new Exception($"Error parsing get place categories response:\n{result.downloadHandler.text}"); |
| | 245 | |
|
| 0 | 246 | | if (response.data == null) |
| 0 | 247 | | throw new Exception($"No place categories info retrieved:\n{result.downloadHandler.text}"); |
| | 248 | |
|
| 0 | 249 | | return response.data; |
| 0 | 250 | | } |
| | 251 | | } |
| | 252 | | } |