Esempio n. 1
0
        IEnumerator Request(string api_path)
        {
            if (token != null)
            {
                UnityWebRequest request = UnityWebRequest.Get(API_URL + api_path);
                PrepareHeaders(request);
                yield return(VimeoApi.SendRequest(request));

                if (request.responseCode != 200)
                {
                    if (request.responseCode == 401)
                    {
                        Debug.LogError("[VimeoApi] 401 Unauthorized request.");
                    }

                    if (OnNetworkError != null && IsNetworkError(request))
                    {
                        OnNetworkError(request.error);
                    }

                    if (OnError != null && !IsNetworkError(request))
                    {
                        OnError(request.downloadHandler.text);
                    }
                }
                else if (OnRequestComplete != null)
                {
                    OnRequestComplete(request.downloadHandler.text);
                }
            }
        }
Esempio n. 2
0
        private IEnumerator SendTusRequest()
        {
            ReadBytes();

            m_uploadRequest = UnityWebRequest.Put(m_url, bytes);
            SetupTusRequest(m_uploadRequest);
            m_uploadRequest.method = "PATCH";
            m_uploadRequest.SetRequestHeader("Upload-Offset", m_lastByteUploaded.ToString());
            m_uploadRequest.SetRequestHeader("Content-Type", "application/offset+octet-stream");

            m_isUploading = true;

            yield return(VimeoApi.SendRequest(m_uploadRequest));

            if (m_uploadRequest.isNetworkError || m_uploadRequest.isHttpError)
            {
                UploadError(m_uploadRequest.responseCode + ": " + m_uploadRequest.error);
            }
            else
            {
                m_isUploading         = false;
                m_isFinishedUploading = true;
                if (OnChunkUploadComplete != null)
                {
                    OnChunkUploadComplete(this, m_uploadRequest.GetResponseHeader("Upload-Offset"));
                }
            }

            DisposeBytes();
        }
Esempio n. 3
0
        IEnumerator GetTicket()
        {
            if (OnUploadProgress != null)
            {
                OnUploadProgress("Authorizing", 0);
            }

            WWWForm form = new WWWForm();

            form.AddField("type", "streaming");

            using (UnityWebRequest request = UnityWebRequest.Post(API_URL + "/me/videos", form)) {
                PrepareHeaders(request, "3.2");
                yield return(VimeoApi.SendRequest(request));

                if (IsNetworkError(request))
                {
                    Debug.LogError(request.error);
                }
                else
                {
                    VimeoTicket ticket = VimeoTicket.CreateFromJSON(request.downloadHandler.text);

                    if (ticket.error == null)
                    {
                        StartCoroutine(UploadVideo(ticket));
                    }
                    else
                    {
                        Debug.LogError(ticket.error + " " + ticket.developer_message);
                    }
                }
            }
        }
Esempio n. 4
0
 public IEnumerator Request(string api_path)
 {
     if (token != null) {
         UnityWebRequest request = UnityWebRequest.Get(API_URL + api_path);
         PrepareHeaders(request);
         yield return VimeoApi.SendRequest(request);
         ResponseHandler(request);
     }
 }
Esempio n. 5
0
        public IEnumerator Patch(string url)
        {
            using (UnityWebRequest request = UnityWebRequest.Post(url, form)) {
                PrepareHeaders(request);
                request.SetRequestHeader("X-HTTP-Method-Override", "PATCH");
                yield return(VimeoApi.SendRequest(request));

                ResetForm();
                ResponseHandler(request);
            }
        }
Esempio n. 6
0
 IEnumerator Put(string api_path)
 {
     if (token != null) {
         byte[] data = new byte[] { 0x00 };
         using (UnityWebRequest request = UnityWebRequest.Put(API_URL + api_path, data)) {
             PrepareHeaders(request);
             yield return VimeoApi.SendRequest(request);
             ResponseHandler(request);
         }
     }
 }
Esempio n. 7
0
        public IEnumerator RequestTusResource(string api_path, long fileByteCount)
        {
            string tusResourceRequestBody = "{ \"upload\": { \"approach\": \"tus\", \"size\": \"" + fileByteCount.ToString() + "\" } }";

            if (token != null) {
                using (UnityWebRequest request = UnityWebRequest.Put("https://api.vimeo.com/me/videos", tusResourceRequestBody)) {
                    PrepareTusHeaders(request);
                    yield return VimeoApi.SendRequest(request);
                    ResponseHandler(request);
                }
            }
        }
Esempio n. 8
0
        public IEnumerator Put(string api_path, string body)
        {
            if (token != null)
            {
                using (UnityWebRequest request = UnityWebRequest.Put(API_URL + api_path, body))
                {
                    PrepareHeaders(request);
                    yield return(VimeoApi.SendRequest(request));

                    ResponseHandler(request);
                }
            }
        }
Esempio n. 9
0
        IEnumerator UploadVideo(VimeoTicket ticket)
        {
            if (OnUploadProgress != null)
            {
                OnUploadProgress("Uploading", 0);
            }

            byte[] data    = new byte[0];
            bool   success = false;


            // Using try/catch to wait for video to finish being
            while (success == false)
            {
                try {
                    // Get local video file and store it in a byte array for uploading
                    data    = File.ReadAllBytes(video_file_path);
                    success = true;
                }
                catch (IOException e) {
                    // TODO: fix this ugly code!
                    Debug.Log("File is being accessed by another process. " + e.Message);
                }
            }

            FileInfo video_file = new FileInfo(video_file_path);

            // Upload to the Vimeo server
            using (UnityWebRequest request = UnityWebRequest.Put(ticket.upload_link_secure, data)) {
                uploader = request;
                request.chunkedTransfer = false;
                request.SetRequestHeader("Content-Type", "video/" + video_file.Extension);
                yield return(VimeoApi.SendRequest(request));

                uploader = null;

                if (IsNetworkError(request))
                {
                    if (OnNetworkError != null)
                    {
                        OnNetworkError(request.error);
                    }
                }
                else
                {
                    StartCoroutine(VerifyUpload(ticket));
                }
            }
        }
Esempio n. 10
0
        public IEnumerator TusUploadReplace(int videoId, string file_name, long fileByteCount)
        {
            string tusResourceRequestBody = "{ \"file_name\": \"" + file_name + "\", \"upload\": { \"status\": \"in_progress\", \"size\": \"" + fileByteCount.ToString() + "\", \"approach\": \"tus\" } }";

            if (token != null)
            {
                using (UnityWebRequest request = UnityWebRequest.Put("https://api.vimeo.com/videos/" + videoId + "/versions", tusResourceRequestBody))
                {
                    PrepareTusHeaders(request, true);
                    yield return(VimeoApi.SendRequest(request));

                    ResponseHandler(request);
                }
            }
        }
Esempio n. 11
0
        IEnumerator CompleteUpload(VimeoTicket ticket)
        {
            if (OnUploadProgress != null)
            {
                OnUploadProgress("Complete", 1f);
            }

            using (UnityWebRequest request = UnityWebRequest.Delete(API_URL + ticket.complete_uri)) {
                PrepareHeaders(request);
                yield return(VimeoApi.SendRequest(request));

                if (OnUploadComplete != null)
                {
                    OnUploadComplete(request.GetResponseHeader("Location"));
                }
            }
        }
Esempio n. 12
0
        private IEnumerator ResumeUpload()
        {
            yield return(new WaitForSeconds(5)); // Wait a bit before trying again so there is time to reconnect

            // Make a request to check what the last upload offset is...
            UnityWebRequest uploadCheck = UnityWebRequest.Get(m_url);

            uploadCheck.method = "HEAD";
            SetupTusRequest(uploadCheck);

            yield return(VimeoApi.SendRequest(uploadCheck));

            if (uploadCheck.GetResponseHeader("Upload-Offset") != null)
            {
                m_lastByteUploaded = long.Parse(uploadCheck.GetResponseHeader("Upload-Offset"));
            }

            uploadCheck.Dispose();
            m_uploadRequest.Dispose();
            m_uploadRequest = null;
            DisposeBytes();
            Upload();
        }
Esempio n. 13
0
        IEnumerator Put(string api_path)
        {
            if (token != null)
            {
                byte[] data = new byte[] { 0x00 };
                using (UnityWebRequest request = UnityWebRequest.Put(API_URL + api_path, data)) {
                    PrepareHeaders(request);
                    yield return(VimeoApi.SendRequest(request));

                    if (request.error != null)
                    {
                        Debug.LogError(request.downloadHandler.text);
                        if (OnError != null)
                        {
                            OnError(request.downloadHandler.text);
                        }
                    }
                    else
                    {
                        // TODO create event hook
                    }
                }
            }
        }
Esempio n. 14
0
        IEnumerator Patch(string url)
        {
            using (UnityWebRequest request = UnityWebRequest.Post(url, form)) {
                PrepareHeaders(request);
                request.SetRequestHeader("X-HTTP-Method-Override", "PATCH");
                yield return(VimeoApi.SendRequest(request));

                // Reset the form
                form = new WWWForm();

                if (request.responseCode != 200)
                {
                    Debug.LogError(request.downloadHandler.text);
                    if (OnError != null)
                    {
                        OnError(request.downloadHandler.text);
                    }
                }
                else if (OnPatchComplete != null)
                {
                    OnPatchComplete(request.downloadHandler.text);
                }
            }
        }
Esempio n. 15
0
        IEnumerator VerifyUpload(VimeoTicket ticket)
        {
            if (OnUploadProgress != null)
            {
                OnUploadProgress("Verifying", 0.9999999f);
            }

            byte[] data = new byte[] { 0x00 };

            using (UnityWebRequest request = UnityWebRequest.Put(ticket.upload_link_secure, data)) {
                request.chunkedTransfer = false;
                request.SetRequestHeader("Content-Range", "bytes */*");
                yield return(VimeoApi.SendRequest(request));

                if (request.responseCode == 308)
                {
                    StartCoroutine(CompleteUpload(ticket));
                }
                else
                {
                    Debug.Log(request.responseCode);
                }
            }
        }
Esempio n. 16
0
        public IEnumerator PagedRequest(int per_page, string api_path)
        {
            int      page         = 1;
            JSONNode data         = JSONNode.Parse("[]");
            string   response     = null;
            JSONNode responseJSON = null;

            do
            {
                UnityWebRequest request = UnityWebRequest.Get(API_URL + api_path + "&page=" + page + "&per_page=" + per_page);
                PrepareHeaders(request);
                yield return(VimeoApi.SendRequest(request));

                if (request.error != null)
                {
                    if (request.responseCode == 401)
                    {
                        SendError("401 Unauthorized request. Are you using a valid token?", request.downloadHandler.text);
                    }
                    else if (IsNetworkError(request))
                    {
                        Debug.LogError("[VimeoApi] It seems like you are not connected to the internet or are having connection problems.");
                        OnNetworkError?.Invoke(request.error);
                    }
                    else
                    {
                        if (page > 1)
                        {
                            // It's OK, make it the end of pages
                            Debug.LogWarning("[VimeoApi] Paged request error for url " + request.url + ", at page #" + page + ", error: " + request.error + ", " + request.downloadHandler.text);
                            break;
                        }
                        else
                        {
                            SendError(request.url + " - " + request.downloadHandler.text, request.downloadHandler.text);
                        }
                    }
                    yield break;
                }

                response     = request.downloadHandler.text;
                responseJSON = JSONNode.Parse(response);
                JSONNode pageData = responseJSON["data"];
                for (int i = 0; i < pageData.Count; i++)
                {
                    JSONNode element = pageData[i];
                    data.Add(element);
                }
                if (pageData.Count < per_page)
                {
                    break;
                }
                page++;
            }while (true);

            if (responseJSON != null)
            {
                responseJSON.Remove("data");
                responseJSON.Add("data", data);
            }
            OnRequestComplete?.Invoke(responseJSON.ToString());
        }