| | 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.Networking; |
| | 8 | |
|
| | 9 | | namespace 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 | |
|
| 425 | 30 | | public WorldsAPIClient(IWebRequestController webRequestController) |
| | 31 | | { |
| 425 | 32 | | this.webRequestController = webRequestController; |
| 425 | 33 | | } |
| | 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}"; |
| 0 | 38 | | var result = await webRequestController.GetAsync(string.Format(URL, searchString.Replace(" ", "+"), pageNumb |
| | 39 | |
|
| 0 | 40 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 41 | | throw new Exception($"Error fetching worlds info:\n{result.error}"); |
| | 42 | |
|
| 0 | 43 | | var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text); |
| | 44 | |
|
| 0 | 45 | | if (response == null) |
| 0 | 46 | | throw new Exception($"Error parsing world info:\n{result.downloadHandler.text}"); |
| | 47 | |
|
| 0 | 48 | | if (response.data == null) |
| 0 | 49 | | throw new Exception($"No world info retrieved:\n{result.downloadHandler.text}"); |
| | 50 | |
|
| 0 | 51 | | return response; |
| 0 | 52 | | } |
| | 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}"; |
| 0 | 57 | | var result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageSize, filter, |
| | 58 | |
|
| 0 | 59 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 60 | | throw new Exception($"Error fetching worlds info:\n{result.error}"); |
| | 61 | |
|
| 0 | 62 | | var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text); |
| | 63 | |
|
| 0 | 64 | | if (response == null) |
| 0 | 65 | | throw new Exception($"Error parsing word info:\n{result.downloadHandler.text}"); |
| | 66 | |
|
| 0 | 67 | | if (response.data == null) |
| 0 | 68 | | throw new Exception($"No world info retrieved:\n{result.downloadHandler.text}"); |
| | 69 | |
|
| 0 | 70 | | return response; |
| 0 | 71 | | } |
| | 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}"; |
| 0 | 76 | | UnityWebRequest result = await webRequestController.GetAsync(string.Format(URL, pageNumber * pageSize, pageS |
| 0 | 77 | | var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text); |
| | 78 | |
|
| 0 | 79 | | if (response == null) |
| 0 | 80 | | throw new Exception($"Error parsing get favorites response:\n{result.downloadHandler.text}"); |
| | 81 | |
|
| 0 | 82 | | if (response.data == null) |
| 0 | 83 | | throw new Exception($"No favorites info retrieved:\n{result.downloadHandler.text}"); |
| | 84 | |
|
| 0 | 85 | | return response.data; |
| 0 | 86 | | } |
| | 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"; |
| 0 | 91 | | var result = await webRequestController.GetAsync(string.Format(URL, name), cancellationToken: ct, isSigned: |
| | 92 | |
|
| 0 | 93 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 94 | | throw new Exception($"Error fetching world info:\n{result.error}"); |
| | 95 | |
|
| 0 | 96 | | var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text); |
| | 97 | |
|
| 0 | 98 | | if (response == null) |
| 0 | 99 | | throw new Exception($"Error parsing world info:\n{result.downloadHandler.text}"); |
| | 100 | |
|
| 0 | 101 | | if (response.data.Count == 0) |
| 0 | 102 | | throw new NotAWorldException(name); |
| | 103 | |
|
| 0 | 104 | | return response.data[0]; |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public async UniTask<List<WorldsResponse.WorldInfo>> GetWorldsByNamesList(List<string> namesList, CancellationTo |
| | 108 | | { |
| 0 | 109 | | if (namesList.Count == 0) |
| 0 | 110 | | return new List<WorldsResponse.WorldInfo>(); |
| | 111 | |
|
| 0 | 112 | | var url = string.Concat(BASE_URL, "?"); |
| 0 | 113 | | foreach (string name in namesList) |
| 0 | 114 | | url = string.Concat(url, $"names={name}&with_realms_detail=true&"); |
| | 115 | |
|
| 0 | 116 | | var result = await webRequestController.GetAsync(url, cancellationToken: ct, isSigned: true); |
| | 117 | |
|
| 0 | 118 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 119 | | throw new Exception($"Error fetching worlds info:\n{result.error}"); |
| | 120 | |
|
| 0 | 121 | | var response = Utils.SafeFromJson<WorldsResponse.WorldsAPIResponse>(result.downloadHandler.text); |
| | 122 | |
|
| 0 | 123 | | if (response == null) |
| 0 | 124 | | throw new Exception($"Error parsing worlds info:\n{result.downloadHandler.text}"); |
| | 125 | |
|
| 0 | 126 | | return response.data; |
| 0 | 127 | | } |
| | 128 | | } |
| | 129 | | } |