Exemplo n.º 1
0
        TResp CallNoRetrySync(TReq request, string accessToken)
        {
            var url = apiUrlGenerator(request);

            Debug.LogFormat("Calling RPC: {0}", url);

            var webRequest = ClusterApiUtil.CreateUnityWebRequest(accessToken, url, httpVerb);

            webRequest.downloadHandler = new DownloadHandlerBuffer();
            webRequest.SendWebRequest();

            while (!webRequest.isDone)
            {
                Thread.Sleep(50);
            }

            if (webRequest.isNetworkError)
            {
                throw new Exception(webRequest.error);
            }

            var responseText = webRequest.downloadHandler.text;

            Debug.LogFormat("Calling RPC ResponseText: {0}", responseText);
            return(JsonUtility.FromJson <TResp>(responseText));
        }
        IEnumerator PostNotifyFinishedUpload()
        {
            isProcessing = true;
            // 互換性のためRequestParameterとBody両方に入れる
            var notifyFinishedUrl    = $"{Constants.VenueApiBaseUrl}/v1/venues/{venueId.Value}/upload/{uploadRequestId.Value}/done?isPublish={isPublish}";
            var notifyFinishedRequst = ClusterApiUtil.CreateUnityWebRequest(accessToken, notifyFinishedUrl, UnityWebRequest.kHttpVerbPOST);

            notifyFinishedRequst.downloadHandler = new DownloadHandlerBuffer();
            var payload = new PostNotifyFinishedUploadPayload {
                isPublish = isPublish
            };

            notifyFinishedRequst.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload)));

            notifyFinishedRequst.SendWebRequest();

            while (!notifyFinishedRequst.isDone)
            {
                yield return(null);
            }

            if (notifyFinishedRequst.isNetworkError)
            {
                HandleError(new Exception(notifyFinishedRequst.error));
                yield break;
            }
            if (notifyFinishedRequst.isHttpError)
            {
                HandleError(new Exception(notifyFinishedRequst.downloadHandler.text));
                yield break;
            }

            try
            {
                Debug.Log(notifyFinishedRequst.downloadHandler.text);
                var response = JsonUtility.FromJson <VenueUploadRequestCompletionResponse>(notifyFinishedRequst.downloadHandler.text);
                onSuccess?.Invoke(response);
            }
            catch (Exception e)
            {
                HandleError(e);
            }
            finally
            {
                isProcessing = false;
            }
        }
        IEnumerator GetVenues()
        {
            Texture2D cache;

            if (CacheMap.TryGetValue(thumbnailUrl, out cache) && cache)
            {
                onSuccess?.Invoke(cache);
                yield break;
            }

            var downloadThumbnailRequest =
                ClusterApiUtil.CreateUnityWebRequest(thumbnailUrl.Url, UnityWebRequest.kHttpVerbGET);

            downloadThumbnailRequest.downloadHandler = new DownloadHandlerBuffer();

            downloadThumbnailRequest.SendWebRequest();

            while (!downloadThumbnailRequest.isDone)
            {
                yield return(null);
            }

            if (downloadThumbnailRequest.isNetworkError)
            {
                HandleError(new Exception(downloadThumbnailRequest.error));
                yield break;
            }

            if (downloadThumbnailRequest.isHttpError)
            {
                HandleError(new Exception(downloadThumbnailRequest.downloadHandler.text));
                yield break;
            }

            try
            {
                var texture = new Texture2D(1, 1);
                texture.LoadImage(downloadThumbnailRequest.downloadHandler.data);
                texture.filterMode = FilterMode.Point;
                onSuccess?.Invoke(texture);
                CacheMap[thumbnailUrl] = texture;
            }
            catch (Exception e)
            {
                HandleError(e);
            }
        }
        IEnumerator PostUploadRequest()
        {
            isProcessing = true;
            var postUploadRequestUrl = $"{Constants.VenueApiBaseUrl}/v1/venues/{venueId.Value}/upload/new";
            var postUploadRequest    = ClusterApiUtil.CreateUnityWebRequest(accessToken, postUploadRequestUrl, UnityWebRequest.kHttpVerbPOST);

            postUploadRequest.downloadHandler = new DownloadHandlerBuffer();

            postUploadRequest.SendWebRequest();

            while (!postUploadRequest.isDone)
            {
                yield return(null);
            }

            if (postUploadRequest.isNetworkError)
            {
                HandleError(new Exception(postUploadRequest.error));
                yield break;
            }
            if (postUploadRequest.isHttpError)
            {
                HandleError(new Exception(postUploadRequest.downloadHandler.text));
                yield break;
            }

            try
            {
                Debug.Log(postUploadRequest.downloadHandler.text);
                var uploadId = JsonUtility.FromJson <UploadRequest>(postUploadRequest.downloadHandler.text);
                onSuccess?.Invoke(uploadId);
            }
            catch (Exception e)
            {
                HandleError(e);
            }
            finally
            {
                isProcessing = false;
            }
        }
        IEnumerator PostVenue()
        {
            isProcessing = true;
            var getTeamsUrl      = $"{Constants.VenueApiBaseUrl}/v1/venues";
            var postVenueRequest = ClusterApiUtil.CreateUnityWebRequest(accessToken, getTeamsUrl, UnityWebRequest.kHttpVerbPOST);

            postVenueRequest.downloadHandler = new DownloadHandlerBuffer();
            postVenueRequest.uploadHandler   = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload)));

            postVenueRequest.SendWebRequest();

            while (!postVenueRequest.isDone)
            {
                yield return(null);
            }

            if (postVenueRequest.isNetworkError)
            {
                HandleError(new Exception(postVenueRequest.error));
                yield break;
            }
            if (postVenueRequest.isHttpError)
            {
                HandleError(new Exception(postVenueRequest.downloadHandler.text));
                yield break;
            }

            try
            {
                Debug.Log(postVenueRequest.downloadHandler.text);
                var venue = JsonUtility.FromJson <Json.Venue>(postVenueRequest.downloadHandler.text);
                onSuccess?.Invoke(venue);
            }
            catch (Exception e)
            {
                HandleError(e);
            }

            isProcessing = false;
        }
        IEnumerator Upload()
        {
            isProcessing = true;

            var getPolicyUrl        = $"{Constants.VenueApiBaseUrl}/v1/upload/venue/{uploadRequestId.Value}/policies";
            var getPolicyWebRequest =
                ClusterApiUtil.CreateUnityWebRequest(accessToken, getPolicyUrl, UnityWebRequest.kHttpVerbPOST);

            getPolicyWebRequest.downloadHandler = new DownloadHandlerBuffer();
            getPolicyWebRequest.uploadHandler   =
                new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload)));

            getPolicyWebRequest.SendWebRequest();
            while (!getPolicyWebRequest.isDone)
            {
                yield return(null);
            }

            if (getPolicyWebRequest.isNetworkError)
            {
                HandleError(new Exception(getPolicyWebRequest.error));
            }
            else if (getPolicyWebRequest.isHttpError)
            {
                HandleError(new Exception(getPolicyWebRequest.downloadHandler.text));
            }
            else
            {
                var json = getPolicyWebRequest.downloadHandler.text;

                AssetUploadPolicy policy = null;
                try
                {
                    policy = JsonConvert.DeserializeObject <AssetUploadPolicy>(json);
                }
                catch (Exception e)
                {
                    HandleError(e);
                    yield break;
                }

                byte[] fileBytes = null;
                try
                {
                    fileBytes = ReadFile(filePath);
                }
                catch (Exception e)
                {
                    HandleError(e);
                    yield break;
                }

                if (policy == null || fileBytes == null)
                {
                    HandleError(new Exception("unknown error"));
                    yield break;
                }

                var form = BuildForm(fileBytes, policy);
                var uploadFileWebRequest = UnityWebRequest.Post(policy.uploadUrl, form);

                uploadFileWebRequest.SendWebRequest();
                while (!uploadFileWebRequest.isDone)
                {
                    yield return(null);
                }

                if (uploadFileWebRequest.isNetworkError)
                {
                    HandleError(new Exception(uploadFileWebRequest.error));
                }
                else if (uploadFileWebRequest.isHttpError)
                {
                    HandleError(new Exception(uploadFileWebRequest.downloadHandler.text));
                }
                else
                {
                    Debug.Log($"Success Upload {policy.fileType}");
                    onSuccess?.Invoke(policy);
                    isProcessing = false;
                }
            }
        }
Exemplo n.º 7
0
        static void AssetUpload(
            string accessToken,
            string fileType,
            string filePath,
            UploadRequestID uploadRequestId)
        {
            var fileInfo = new FileInfo(filePath);
            var payload  = new PostUploadAssetPolicyPayload
            {
                fileType = fileType,
                fileName = fileInfo.Name,
                fileSize = fileInfo.Length
            };

            var url = $"{Constants.VenueApiBaseUrl}/v1/upload/venue/{uploadRequestId.Value}/policies";

            Debug.LogFormat("Calling RPC: {0}", url);

            var webRequest = ClusterApiUtil.CreateUnityWebRequest(accessToken, url, UnityWebRequest.kHttpVerbPOST);

            webRequest.downloadHandler = new DownloadHandlerBuffer();
            webRequest.uploadHandler   = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload)));
            webRequest.SendWebRequest();

            while (!webRequest.isDone)
            {
                Thread.Sleep(50);
            }

            if (webRequest.isNetworkError)
            {
                throw new Exception(webRequest.error);
            }

            var responseText = webRequest.downloadHandler.text;

            Debug.LogFormat("Calling RPC ResponseText: {0}", responseText);
            var policy = JsonConvert.DeserializeObject <AssetUploadPolicy>(responseText);

            var fileBytes = ReadFile(filePath);

            if (policy == null || fileBytes == null)
            {
                throw new Exception("unknown error");
            }

            var form = BuildForm(fileBytes, policy);
            var uploadFileWebRequest = UnityWebRequest.Post(policy.uploadUrl, form);

            uploadFileWebRequest.chunkedTransfer = false;

            uploadFileWebRequest.SendWebRequest();
            while (!uploadFileWebRequest.isDone)
            {
                Thread.Sleep(50);
            }

            if (uploadFileWebRequest.isNetworkError)
            {
                throw new Exception(uploadFileWebRequest.error);
            }

            if (uploadFileWebRequest.isHttpError)
            {
                throw new Exception(uploadFileWebRequest.downloadHandler.text);
            }

            Debug.Log($"Success Upload {policy.fileType}");
        }