| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCLServices.EnvironmentProvider; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | |
|
| | 11 | | namespace DCLServices.CameraReelService |
| | 12 | | { |
| | 13 | | public class CameraReelWebRequestClient : ICameraReelNetworkClient |
| | 14 | | { |
| | 15 | | private readonly IWebRequestController webRequestController; |
| | 16 | | private readonly IEnvironmentProviderService environmentProviderService; |
| | 17 | |
|
| | 18 | | private readonly string imageBaseURL; |
| | 19 | | private readonly string userBaseURL; |
| | 20 | |
|
| 425 | 21 | | public CameraReelWebRequestClient(IWebRequestController webRequestController, |
| | 22 | | IEnvironmentProviderService environmentProviderService) |
| | 23 | | { |
| 425 | 24 | | this.webRequestController = webRequestController; |
| 425 | 25 | | this.environmentProviderService = environmentProviderService; |
| | 26 | |
|
| 425 | 27 | | imageBaseURL = $"https://camera-reel-service.decentraland.{(IsProdEnv() ? "org" : "zone")}/api/images"; |
| 425 | 28 | | userBaseURL = $"https://camera-reel-service.decentraland.{(IsProdEnv() ? "org" : "zone")}/api/users"; |
| 425 | 29 | | } |
| | 30 | |
|
| | 31 | | private bool IsProdEnv() => |
| 850 | 32 | | environmentProviderService.IsProd(); |
| | 33 | |
|
| | 34 | | public async UniTask<CameraReelStorageResponse> GetUserGalleryStorageInfoRequest(string userAddress, Cancellatio |
| | 35 | | { |
| 0 | 36 | | UnityWebRequest result = await webRequestController.GetAsync($"{userBaseURL}/{userAddress}", isSigned: true, |
| | 37 | |
|
| 0 | 38 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 39 | | throw new Exception($"Error fetching user gallery storage info :\n{result.error}"); |
| | 40 | |
|
| 0 | 41 | | CameraReelStorageResponse responseData = Utils.SafeFromJson<CameraReelStorageResponse>(result.downloadHandle |
| | 42 | |
|
| 0 | 43 | | if (responseData == null) |
| 0 | 44 | | throw new Exception($"Error parsing gallery storage info response:\n{result.downloadHandler.text}"); |
| | 45 | |
|
| 0 | 46 | | return responseData; |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public async UniTask<CameraReelResponses> GetScreenshotGalleryRequest(string userAddress, int limit, int offset, |
| | 50 | | { |
| 0 | 51 | | UnityWebRequest result = await webRequestController.GetAsync($"{userBaseURL}/{userAddress}/images?limit={lim |
| | 52 | |
|
| 0 | 53 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 54 | | throw new Exception($"Error fetching user screenshots gallery :\n{result.error}"); |
| | 55 | |
|
| 0 | 56 | | CameraReelResponses responseData = Utils.SafeFromJson<CameraReelResponses>(result.downloadHandler.text); |
| | 57 | |
|
| 0 | 58 | | if (responseData == null) |
| 0 | 59 | | throw new Exception($"Error parsing screenshots gallery response:\n{result.downloadHandler.text}"); |
| | 60 | |
|
| 0 | 61 | | foreach (CameraReelResponse response in responseData.images) |
| 0 | 62 | | ResponseSanityCheck(response, result.downloadHandler.text); |
| | 63 | |
|
| 0 | 64 | | return responseData; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public async UniTask<CameraReelUploadResponse> UploadScreenshotRequest(byte[] image, ScreenshotMetadata metadata |
| | 68 | | { |
| 0 | 69 | | var formData = new List<IMultipartFormSection> |
| | 70 | | { |
| | 71 | | new MultipartFormFileSection("image", image, $"{metadata.dateTime}.jpg", "image/jpeg"), |
| | 72 | | new MultipartFormDataSection("metadata", JsonUtility.ToJson(metadata)), |
| | 73 | | }; |
| | 74 | |
|
| 0 | 75 | | UnityWebRequest result = await webRequestController.PostAsync(imageBaseURL, formData, isSigned: true, cancel |
| | 76 | |
|
| 0 | 77 | | CameraReelUploadResponse response = Utils.SafeFromJson<CameraReelUploadResponse>(result.downloadHandler.text |
| | 78 | |
|
| 0 | 79 | | if (response == null) |
| 0 | 80 | | throw new Exception($"Error parsing screenshot response:\n{result.downloadHandler.text}"); |
| | 81 | |
|
| 0 | 82 | | ResponseSanityCheck(response.image, result.downloadHandler.text); |
| | 83 | |
|
| 0 | 84 | | return response; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public async UniTask<CameraReelStorageResponse> DeleteScreenshotRequest(string uuid, CancellationToken ct) |
| | 88 | | { |
| 0 | 89 | | UnityWebRequest result = await webRequestController.DeleteAsync($"{imageBaseURL}/{uuid}", isSigned: true, ca |
| | 90 | |
|
| 0 | 91 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 92 | | throw new Exception($"error during deleting screenshot from the gallery:\n{result.error}"); |
| | 93 | |
|
| 0 | 94 | | CameraReelStorageResponse response = Utils.SafeFromJson<CameraReelStorageResponse>(result.downloadHandler.te |
| | 95 | |
|
| 0 | 96 | | if (response == null) |
| 0 | 97 | | throw new Exception($"Error parsing screenshot delete response:\n{result.downloadHandler.text}"); |
| | 98 | |
|
| 0 | 99 | | return response; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | private static void ResponseSanityCheck(CameraReelResponse response, string downloadHandlerText) |
| | 103 | | { |
| 0 | 104 | | if (response == null) |
| 0 | 105 | | throw new Exception($"Error parsing screenshot response:\n{downloadHandlerText}"); |
| | 106 | |
|
| 0 | 107 | | if (string.IsNullOrEmpty(response.url)) |
| 0 | 108 | | throw new Exception($"No screenshot image url info retrieved:\n{downloadHandlerText}"); |
| | 109 | |
|
| 0 | 110 | | if (string.IsNullOrEmpty(response.thumbnailUrl)) |
| 0 | 111 | | throw new Exception($"No screenshot thumbnail url info retrieved:\n{downloadHandlerText}"); |
| | 112 | |
|
| 0 | 113 | | if (response.metadata == null) |
| 0 | 114 | | throw new Exception($"No screenshot metadata info retrieved:\n{downloadHandlerText}"); |
| 0 | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | | [Serializable] |
| | 119 | | public class CameraReelResponses |
| | 120 | | { |
| | 121 | | public List<CameraReelResponse> images = new (); |
| | 122 | | public int currentImages; |
| | 123 | | public int maxImages; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | [Serializable] |
| | 127 | | public class CameraReelResponse |
| | 128 | | { |
| | 129 | | public string id; |
| | 130 | | public string url; |
| | 131 | | public string thumbnailUrl; |
| | 132 | |
|
| | 133 | | public ScreenshotMetadata metadata; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | [Serializable] |
| | 137 | | public class CameraReelUploadResponse |
| | 138 | | { |
| | 139 | | public int currentImages; |
| | 140 | | public int maxImages; |
| | 141 | |
|
| | 142 | | public CameraReelResponse image; |
| | 143 | | } |
| | 144 | |
|
| | 145 | | [Serializable] |
| | 146 | | public class CameraReelStorageResponse |
| | 147 | | { |
| | 148 | | public int currentImages; |
| | 149 | | public int maxImages; |
| | 150 | | } |
| | 151 | |
|
| | 152 | | [Serializable] |
| | 153 | | public class CameraReelErrorResponse |
| | 154 | | { |
| | 155 | | public string message; |
| | 156 | | public string reason; |
| | 157 | | } |
| | 158 | | } |