| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Net; |
| | 6 | | using System.Net.Http; |
| | 7 | | using DCL; |
| | 8 | | using DCL.Builder; |
| | 9 | | using DCL.Builder.Manifest; |
| | 10 | | using DCL.Helpers; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | | using Newtonsoft.Json.Linq; |
| | 13 | | using UnityEngine; |
| | 14 | | using UnityEngine.Networking; |
| | 15 | | using UnityEngine.SocialPlatforms.Impl; |
| | 16 | |
|
| | 17 | | public class BuilderAPIController : IBuilderAPIController |
| | 18 | | { |
| | 19 | | internal const string API_DATEFORMAT = "yyyy-MM-ddThh:mm:ss.fff%K"; |
| | 20 | |
|
| | 21 | | internal const string CATALOG_ENDPOINT = "/assetPacks"; |
| | 22 | | internal const string ASSETS_ENDPOINT = "/assets?"; |
| | 23 | | internal const string GET_PROJECTS_ENDPOINT = "/projects"; |
| | 24 | | internal const string PROJECT_MANIFEST_ENDPOINT = "/projects/{ID}/manifest"; |
| | 25 | | internal const string PROJECT_THUMBNAIL_ENDPOINT = "/projects/{ID}/media"; |
| | 26 | |
|
| | 27 | | internal const string API_KO_RESPONSE_ERROR = "API response is KO"; |
| | 28 | |
|
| | 29 | | internal const string GET = "get"; |
| | 30 | | internal const string PUT = "put"; |
| | 31 | | internal const string POST = "post"; |
| | 32 | |
|
| | 33 | | public event Action<IWebRequestAsyncOperation> OnWebRequestCreated; |
| | 34 | |
|
| | 35 | | internal IBuilderAPIResponseResolver apiResponseResolver; |
| | 36 | |
|
| | 37 | | private BuilderInWorldBridge builderInWorldBridge; |
| | 38 | |
|
| 4 | 39 | | private Dictionary<string, List<Promise<RequestHeader>>> headersRequests = new Dictionary<string, List<Promise<Reque |
| | 40 | |
|
| | 41 | | public void Initialize(IContext context) |
| | 42 | | { |
| 4 | 43 | | apiResponseResolver = new BuilderAPIResponseResolver(); |
| 4 | 44 | | builderInWorldBridge = context.sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| 4 | 45 | | if (builderInWorldBridge != null) |
| 0 | 46 | | builderInWorldBridge.OnHeadersReceived += HeadersReceived; |
| 4 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Dispose() |
| | 50 | | { |
| 4 | 51 | | if (builderInWorldBridge != null) |
| 0 | 52 | | builderInWorldBridge.OnHeadersReceived -= HeadersReceived; |
| 4 | 53 | | } |
| | 54 | |
|
| | 55 | | internal Promise<RequestHeader> AskHeadersToKernel(string method, string endpoint) |
| | 56 | | { |
| 5 | 57 | | Promise<RequestHeader> promise = new Promise<RequestHeader>(); |
| 5 | 58 | | if (headersRequests.ContainsKey(endpoint)) |
| 1 | 59 | | headersRequests[endpoint].Add(promise); |
| | 60 | | else |
| 4 | 61 | | headersRequests.Add(endpoint, new List<Promise<RequestHeader>>() { promise }); |
| | 62 | |
|
| 5 | 63 | | if (builderInWorldBridge != null) |
| 0 | 64 | | builderInWorldBridge.AskKernelForCatalogHeadersWithParams(method, endpoint); |
| 5 | 65 | | return promise; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | internal void HeadersReceived(RequestHeader requestHeader) |
| | 69 | | { |
| 5 | 70 | | string keyToRemove = ""; |
| 18 | 71 | | foreach (var request in headersRequests) |
| | 72 | | { |
| 4 | 73 | | if (request.Key == requestHeader.endpoint) |
| | 74 | | { |
| 4 | 75 | | keyToRemove = request.Key; |
| 18 | 76 | | foreach (Promise<RequestHeader> promise in request.Value) |
| | 77 | | { |
| 5 | 78 | | promise.Resolve(requestHeader); |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| 5 | 83 | | if (headersRequests.ContainsKey(keyToRemove)) |
| 4 | 84 | | headersRequests.Remove(keyToRemove); |
| 5 | 85 | | } |
| | 86 | |
|
| | 87 | | internal Promise<string> CallUrl(string method, string endpoint, string callParams = "", byte[] body = null, string |
| | 88 | | { |
| 4 | 89 | | Promise<string> resultPromise = new Promise<string>(); |
| 4 | 90 | | Promise<RequestHeader> headersPromise = AskHeadersToKernel(method, endpoint); |
| 4 | 91 | | headersPromise.Then(request => |
| | 92 | | { |
| | 93 | | switch (method) |
| | 94 | | { |
| | 95 | | case GET: |
| 4 | 96 | | IWebRequestAsyncOperation webRequest = BIWUtils.MakeGetCall(BIWUrlUtils.GetBuilderAPIBaseUrl() + req |
| 4 | 97 | | OnWebRequestCreated?.Invoke(webRequest); |
| 0 | 98 | | break; |
| | 99 | | case PUT: |
| 0 | 100 | | request.body = body; |
| 0 | 101 | | CoroutineStarter.Start(CallWeb(request, resultPromise, contentType)); |
| 0 | 102 | | break; |
| | 103 | | case POST: |
| 0 | 104 | | request.body = body; |
| 0 | 105 | | CoroutineStarter.Start(CallWeb(request, resultPromise, contentType, false)); |
| | 106 | | break; |
| | 107 | | } |
| 0 | 108 | | }); |
| | 109 | |
|
| 4 | 110 | | return resultPromise; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | //This will disappear when we implement the signed fetch call |
| | 114 | | IEnumerator CallWeb (RequestHeader requestHeader, Promise<string> resultPromise, string contentType, bool isPut = tr |
| | 115 | | { |
| 0 | 116 | | UnityWebRequest www = null; |
| 0 | 117 | | if (isPut) |
| 0 | 118 | | www = UnityWebRequest.Put (BIWUrlUtils.GetBuilderAPIBaseUrl() + requestHeader.endpoint, requestHeader.body); |
| | 119 | | else |
| | 120 | | { |
| 0 | 121 | | WWWForm form = new WWWForm(); |
| 0 | 122 | | form.AddBinaryData("thumbnail", requestHeader.body); |
| 0 | 123 | | www = UnityWebRequest.Post(BIWUrlUtils.GetBuilderAPIBaseUrl() + requestHeader.endpoint, form ); |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | if (!string.IsNullOrEmpty(contentType)) |
| 0 | 127 | | www.SetRequestHeader("Content-Type", contentType); |
| | 128 | |
|
| 0 | 129 | | foreach (var header in requestHeader.headers) |
| | 130 | | { |
| 0 | 131 | | www.SetRequestHeader(header.Key, header.Value); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | yield return www.SendWebRequest(); |
| | 135 | |
|
| 0 | 136 | | if (www.result != UnityWebRequest.Result.Success) |
| | 137 | | { |
| 0 | 138 | | resultPromise.Reject(www.error); |
| 0 | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 0 | 142 | | byte[] byteArray = www.downloadHandler.data; |
| 0 | 143 | | string result = System.Text.Encoding.UTF8.GetString(byteArray); |
| 0 | 144 | | resultPromise.Resolve(result); |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | public Promise<bool> SetManifest(Manifest manifest) |
| | 150 | | { |
| 0 | 151 | | Promise<bool> fullPromise = new Promise<bool>(); |
| | 152 | |
|
| 0 | 153 | | JsonSerializerSettings dateFormatSettings = new JsonSerializerSettings |
| | 154 | | { |
| | 155 | | DateFormatString = API_DATEFORMAT, |
| | 156 | | }; |
| | 157 | |
|
| 0 | 158 | | string jsonManifest = JsonConvert.SerializeObject(manifest, dateFormatSettings); |
| | 159 | |
|
| 0 | 160 | | byte[] myData = System.Text.Encoding.UTF8.GetBytes(BIWUrlUtils.GetManifestJSON(jsonManifest)); |
| | 161 | |
|
| 0 | 162 | | string endpoint = PROJECT_MANIFEST_ENDPOINT.Replace("{ID}", manifest.project.id); |
| 0 | 163 | | var promise = CallUrl(PUT, endpoint, "", myData, "application/json"); |
| | 164 | |
|
| 0 | 165 | | promise.Then(result => |
| | 166 | | { |
| 0 | 167 | | var apiResponse = apiResponseResolver.GetResponseFromCall(result); |
| 0 | 168 | | if (apiResponse.ok) |
| 0 | 169 | | fullPromise.Resolve(true); |
| | 170 | | else |
| 0 | 171 | | fullPromise.Reject(apiResponse.error); |
| 0 | 172 | | }); |
| | 173 | |
|
| 0 | 174 | | promise.Catch(error => |
| | 175 | | { |
| 0 | 176 | | fullPromise.Reject(error); |
| 0 | 177 | | }); |
| | 178 | |
|
| 0 | 179 | | return fullPromise; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | public Promise<bool> SetThumbnail(string id, Texture2D thumbnail) |
| | 183 | | { |
| 0 | 184 | | Promise<bool> fullPromise = new Promise<bool>(); |
| | 185 | |
|
| 0 | 186 | | byte[] myData = thumbnail.EncodeToPNG(); |
| | 187 | |
|
| 0 | 188 | | string endpoint = PROJECT_THUMBNAIL_ENDPOINT.Replace("{ID}", id); |
| 0 | 189 | | var promise = CallUrl(POST, endpoint, "", myData); |
| | 190 | |
|
| 0 | 191 | | promise.Then(result => |
| | 192 | | { |
| 0 | 193 | | var apiResponse = apiResponseResolver.GetResponseFromCall(result); |
| 0 | 194 | | if (apiResponse.ok) |
| 0 | 195 | | fullPromise.Resolve(true); |
| | 196 | | else |
| 0 | 197 | | fullPromise.Reject(apiResponse.error); |
| 0 | 198 | | }); |
| | 199 | |
|
| 0 | 200 | | promise.Catch(error => |
| | 201 | | { |
| 0 | 202 | | fullPromise.Reject(error); |
| 0 | 203 | | }); |
| | 204 | |
|
| 0 | 205 | | return fullPromise; |
| | 206 | | } |
| | 207 | |
|
| | 208 | | public Promise<Manifest> GetManifestById(string idProject) |
| | 209 | | { |
| 0 | 210 | | Promise<Manifest> fullNewProjectPromise = new Promise<Manifest>(); |
| | 211 | |
|
| 0 | 212 | | string url = PROJECT_MANIFEST_ENDPOINT.Replace("{ID}", idProject); |
| 0 | 213 | | var promise = CallUrl(GET, url); |
| | 214 | |
|
| 0 | 215 | | promise.Then(result => |
| | 216 | | { |
| 0 | 217 | | Manifest manifest = null; |
| | 218 | |
|
| | 219 | | try |
| | 220 | | { |
| 0 | 221 | | manifest = JsonConvert.DeserializeObject<Manifest>(result); |
| 0 | 222 | | } |
| 0 | 223 | | catch (Exception e) |
| | 224 | | { |
| 0 | 225 | | fullNewProjectPromise.Reject(e.Message); |
| 0 | 226 | | return; |
| | 227 | | } |
| | 228 | |
|
| 0 | 229 | | fullNewProjectPromise.Resolve(manifest); |
| 0 | 230 | | }); |
| 0 | 231 | | promise.Catch(error => |
| | 232 | | { |
| 0 | 233 | | fullNewProjectPromise.Reject(error); |
| 0 | 234 | | }); |
| 0 | 235 | | return fullNewProjectPromise; |
| | 236 | | } |
| | 237 | |
|
| | 238 | | public Promise<Manifest> CreateNewProject(ProjectData newProject) |
| | 239 | | { |
| 0 | 240 | | Promise<Manifest> fullNewProjectPromise = new Promise<Manifest>(); |
| 0 | 241 | | Manifest builderManifest = BIWUtils.CreateManifestFromProject(newProject); |
| | 242 | |
|
| 0 | 243 | | JsonSerializerSettings dateFormatSettings = new JsonSerializerSettings |
| | 244 | | { |
| | 245 | | DateFormatString = API_DATEFORMAT, |
| | 246 | | }; |
| | 247 | |
|
| 0 | 248 | | string jsonManifest = JsonConvert.SerializeObject(builderManifest, dateFormatSettings); |
| 0 | 249 | | byte[] myData = System.Text.Encoding.UTF8.GetBytes(BIWUrlUtils.GetManifestJSON(jsonManifest)); |
| | 250 | |
|
| 0 | 251 | | string endpoint = PROJECT_MANIFEST_ENDPOINT.Replace("{ID}", newProject.id); |
| 0 | 252 | | var promise = CallUrl(PUT, endpoint, "", myData, "application/json"); |
| | 253 | |
|
| 0 | 254 | | promise.Then(result => |
| | 255 | | { |
| 0 | 256 | | var apiResponse = apiResponseResolver.GetResponseFromCall(result); |
| 0 | 257 | | if (apiResponse.ok) |
| 0 | 258 | | fullNewProjectPromise.Resolve((Manifest)apiResponse.data); |
| | 259 | | else |
| 0 | 260 | | fullNewProjectPromise.Reject(apiResponse.error); |
| 0 | 261 | | }); |
| | 262 | |
|
| 0 | 263 | | promise.Catch(error => |
| | 264 | | { |
| 0 | 265 | | fullNewProjectPromise.Reject(error); |
| 0 | 266 | | }); |
| | 267 | |
|
| 0 | 268 | | return fullNewProjectPromise; |
| | 269 | | } |
| | 270 | |
|
| | 271 | | public Promise<bool> GetCompleteCatalog(string ethAddres) |
| | 272 | | { |
| 1 | 273 | | Promise<bool> fullCatalogPromise = new Promise<bool>(); |
| | 274 | |
|
| 1 | 275 | | var promiseDefaultCatalog = CallUrl(GET, CATALOG_ENDPOINT, "?owner=default"); |
| 1 | 276 | | var promiseOwnedCatalog = CallUrl(GET, CATALOG_ENDPOINT, "?owner=" + ethAddres); |
| 1 | 277 | | int amountOfCatalogReceived = 0; |
| | 278 | |
|
| | 279 | | // Note: In order to get the full catalog we need to do 2 calls, the default one and the specific one |
| | 280 | | // This is done in order to cache the response in the server |
| 1 | 281 | | promiseDefaultCatalog.Then(catalogJson => |
| | 282 | | { |
| 1 | 283 | | AssetCatalogBridge.i.AddFullSceneObjectCatalog(catalogJson); |
| | 284 | |
|
| 1 | 285 | | amountOfCatalogReceived++; |
| 1 | 286 | | if (amountOfCatalogReceived >= 2) |
| 0 | 287 | | fullCatalogPromise.Resolve(true); |
| 1 | 288 | | }); |
| 1 | 289 | | promiseDefaultCatalog.Reject("Unable to get default catalog"); |
| | 290 | |
|
| 1 | 291 | | promiseOwnedCatalog.Then(catalogJson => |
| | 292 | | { |
| 1 | 293 | | AssetCatalogBridge.i.AddFullSceneObjectCatalog(catalogJson); |
| | 294 | |
|
| 1 | 295 | | amountOfCatalogReceived++; |
| 1 | 296 | | if (amountOfCatalogReceived >= 2) |
| 1 | 297 | | fullCatalogPromise.Resolve(true); |
| 1 | 298 | | }); |
| | 299 | |
|
| 1 | 300 | | promiseOwnedCatalog.Reject("Unable to get owned catalog"); |
| | 301 | |
|
| 1 | 302 | | return fullCatalogPromise; |
| | 303 | | } |
| | 304 | |
|
| | 305 | | public Promise<bool> GetAssets(List<string> assetsIds) |
| | 306 | | { |
| 1 | 307 | | List<string> assetsToAsk = new List<string>(); |
| 2 | 308 | | foreach (string assetsId in assetsIds) |
| | 309 | | { |
| 0 | 310 | | if (!AssetCatalogBridge.i.sceneObjectCatalog.ContainsKey(assetsId)) |
| 0 | 311 | | assetsToAsk.Add(assetsId); |
| | 312 | | } |
| | 313 | |
|
| 1 | 314 | | string query = ""; |
| 2 | 315 | | foreach (string assetsId in assetsToAsk) |
| | 316 | | { |
| 0 | 317 | | if (string.IsNullOrEmpty(query)) |
| 0 | 318 | | query += "id=" + assetsId; |
| | 319 | | else |
| 0 | 320 | | query += "&id=" + assetsId; |
| | 321 | | } |
| | 322 | |
|
| 1 | 323 | | Promise<bool> fullCatalogPromise = new Promise<bool>(); |
| | 324 | |
|
| 1 | 325 | | var promise = CallUrl(GET, ASSETS_ENDPOINT , query); |
| | 326 | |
|
| 1 | 327 | | promise.Then(apiResult => |
| | 328 | | { |
| 1 | 329 | | var assets = apiResponseResolver.GetArrayFromCall<SceneObject>(apiResult); |
| 1 | 330 | | if (assets != null) |
| | 331 | | { |
| 1 | 332 | | AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(assets); |
| 1 | 333 | | fullCatalogPromise.Resolve(true); |
| 1 | 334 | | } |
| | 335 | | else |
| | 336 | | { |
| 0 | 337 | | fullCatalogPromise.Reject(API_KO_RESPONSE_ERROR); |
| | 338 | | } |
| 0 | 339 | | }); |
| | 340 | |
|
| 1 | 341 | | return fullCatalogPromise; |
| | 342 | | } |
| | 343 | |
|
| | 344 | | public Promise<List<ProjectData>> GetAllManifests() |
| | 345 | | { |
| 1 | 346 | | Promise<List<ProjectData>> fullCatalogPromise = new Promise< List<ProjectData>>(); |
| | 347 | |
|
| 1 | 348 | | var promise = CallUrl(GET, GET_PROJECTS_ENDPOINT); |
| | 349 | |
|
| 1 | 350 | | promise.Then(result => |
| | 351 | | { |
| 1 | 352 | | List<ProjectData> allManifest = apiResponseResolver.GetArrayFromCall<ProjectData>(result).ToList(); |
| 1 | 353 | | fullCatalogPromise.Resolve(allManifest); |
| 1 | 354 | | }); |
| 1 | 355 | | promise.Catch(error => |
| | 356 | | { |
| 0 | 357 | | fullCatalogPromise.Reject(error); |
| 0 | 358 | | }); |
| 1 | 359 | | return fullCatalogPromise; |
| | 360 | | } |
| | 361 | |
|
| | 362 | | public Manifest GetManifestFromProjectId(string projectId) |
| | 363 | | { |
| | 364 | | //TODO: Implement functionality |
| 0 | 365 | | return null; |
| | 366 | | } |
| | 367 | |
|
| | 368 | | public Manifest GetManifestFromLandCoordinates(string landCoords) |
| | 369 | | { |
| | 370 | | //TODO: Implement functionality |
| 0 | 371 | | return null; |
| | 372 | | } |
| | 373 | |
|
| | 374 | | public void UpdateProjectManifest(Manifest manifest) |
| | 375 | | { |
| | 376 | | //TODO: Implement functionality |
| 0 | 377 | | } |
| | 378 | |
|
| | 379 | | public void CreateEmptyProjectWithCoords(string coords) |
| | 380 | | { |
| 0 | 381 | | Manifest manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(coords); |
| | 382 | | //TODO: Implement functionality |
| 0 | 383 | | } |
| | 384 | | } |