Ejemplo n.º 1
0
        private IEnumerator CreateAutoApprovedTransferCall(TransferAPIRequest request)
        {
            Debug.Log("CreateAutoApprovedTransfer");

            Dictionary <string, string> headers = new Dictionary <string, string>(AuthorizationHeader);

            headers.Add("Authorization", "Bearer " + session.AccessToken);


            Dictionary <string, object> data = new Dictionary <string, object>();

            data.Add("tokens", request.tokens);
            if (!string.IsNullOrEmpty(request.description))
            {
                data.Add("description", request.description);
            }
            data.Add("app_id", QuartersInit.Instance.APP_ID);


            string dataJson = JsonConvert.SerializeObject(data);

            Debug.Log(dataJson);
            byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(dataJson);


            WWW www = new WWW(API_URL + "/requests", dataBytes, headers);

            Debug.Log(www.url);

            while (!www.isDone)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);

                //failed, fallback to normal request
                StartCoroutine(CreateTransferRequestCall(request));
            }
            else
            {
                Debug.Log(www.text);

                string response = www.text;
                Debug.Log("Response: " + response);

                TransferRequest transferRequest = new TransferRequest(response);

                request.requestId = transferRequest.id;
                Debug.Log("request id is: " + transferRequest.id);
                currentTransferAPIRequests.Add(request);

                StartCoroutine(AutoApproveTransfer(request));
            }
        }
Ejemplo n.º 2
0
 public void CreateTransfer(TransferAPIRequest request)
 {
     if (QuartersInit.Instance.useAutoapproval)
     {
         StartCoroutine(CreateAutoApprovedTransferCall(request));
     }
     else
     {
         //normal transfer
         StartCoroutine(CreateTransferRequestCall(request));
     }
 }
Ejemplo n.º 3
0
        private void ProcessDeepLink(bool isExternalBrowser, string url = "")
        {
            string linkUrl = url;

            #if UNITY_ANDROID
            //overriden linkUrl if deep link comes from external browser, due to limitations of Android plugins implementation
            if (isExternalBrowser)
            {
                linkUrl = CustomUrlSchemeAndroid.GetLaunchedUrl(true);
                CustomUrlSchemeAndroid.ClearSavedData();
            }
            #endif


            #if UNITY_IOS
            //there is no link override on iOS
            #endif


            if (!string.IsNullOrEmpty(linkUrl))
            {
                Debug.Log("Unity URL returned: " + linkUrl);

                Dictionary <string, string> urlParams = linkUrl.ParseURI();

                //blindcode as unable to test this without API update
                if (urlParams.ContainsKey("code"))
                {
                    //string code = split[1];
                    AuthorizationCodeReceived(urlParams["code"]);
                }
                else if (linkUrl.Contains("requestId="))
                {
                    string transferId = urlParams["requestId"];

                    foreach (TransferAPIRequest r in currentTransferAPIRequests)
                    {
                        Debug.Log("Current requests id: " + r.requestId);
                    }

                    //get request from ongoing
                    TransferAPIRequest transferRequest = currentTransferAPIRequests.Find(t => t.requestId == transferId);
                    if (transferRequest == null)
                    {
                        Debug.LogError("Transfer id is invalid: " + transferId);
                        transferRequest.failedDelegate("Invalid transfer id: " + transferId);
                    }

                    if (urlParams.ContainsKey("error"))
                    {
                        //all requests are validated positivelly currently
                        transferRequest.failedDelegate(urlParams["error"]);
                    }
                    else
                    {
                        transferRequest.txId = urlParams["txId"];
                        Debug.Log("tx id:" + transferRequest.txId);

                        transferRequest.successDelegate(transferRequest.txId);
                    }

                    currentTransferAPIRequests.Remove(transferRequest);
                }
                else
                {
                    Debug.Log("NOT IMPLEMENTED URL: " + linkUrl);
                }
            }
        }
Ejemplo n.º 4
0
        private IEnumerator CreateTransferRequestCall(TransferAPIRequest request, bool forceExternalBrowser = false)
        {
            if (Application.isEditor && forceExternalBrowser)
            {
                Debug.LogWarning("Quarters: Transfers with external browser arent supported in Unity editor");
            }

            Debug.Log("CreateTransferRequestCall");

            Dictionary <string, string> headers = new Dictionary <string, string>(AuthorizationHeader);

            headers.Add("Authorization", "Bearer " + session.AccessToken);


            Dictionary <string, object> data = new Dictionary <string, object>();

            data.Add("tokens", request.tokens);
            if (!string.IsNullOrEmpty(request.description))
            {
                data.Add("description", request.description);
            }
            data.Add("app_id", QuartersInit.Instance.APP_ID);


            string dataJson = JsonConvert.SerializeObject(data);

            Debug.Log(dataJson);
            byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(dataJson);


            WWW www = new WWW(API_URL + "/requests", dataBytes, headers);

            Debug.Log(www.url);

            while (!www.isDone)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);

                request.failedDelegate("Creating transfer failed: " + www.error);
            }
            else
            {
                Debug.Log(www.text);

                string response = www.text;
                Debug.Log("Response: " + response);

                TransferRequest transferRequest = new TransferRequest(response);

                request.requestId = transferRequest.id;
                Debug.Log("request id is: " + transferRequest.id);
                currentTransferAPIRequests.Add(request);

                //continue outh forward
                string url = QUARTERS_URL + "/requests/" + transferRequest.id + "?inline=true" + "&redirect_uri=" + URL_SCHEME;

                if (!forceExternalBrowser)
                {
                    //web view authentication
                    QuartersWebView.OpenURL(url);
                    QuartersWebView.OnDeepLink = DeepLink;
                }
                else
                {
                    //external authentication
                    Application.OpenURL(url);
                }
            }
        }
Ejemplo n.º 5
0
 public void CreateTransfer(TransferAPIRequest request)
 {
     StartCoroutine(CreateTransferRequestCall(request));
 }
Ejemplo n.º 6
0
        private IEnumerator AutoApproveTransfer(TransferAPIRequest request)
        {
            Debug.Log("AutoApproveTransfer");

            Dictionary <string, string> headers = new Dictionary <string, string>(AuthorizationHeader);

            headers.Add("Authorization", "Bearer " + QuartersInit.Instance.SERVER_API_TOKEN);


            Dictionary <string, object> data = new Dictionary <string, object>();

            data.Add("clientId", QuartersInit.Instance.APP_ID);
            data.Add("userId", CurrentUser.userId);
            data.Add("address", CurrentUser.accounts[0].address);


            string dataJson = JsonConvert.SerializeObject(data);

            byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(dataJson);


            WWW www = new WWW(API_URL + "/requests/" + request.requestId + "/autoApprove", dataBytes, headers);

            Debug.Log(www.url);
            Debug.Log(dataJson);

            while (!www.isDone)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);
                Debug.Log(www.text);
                if (www.error.StartsWith("40"))
                {
                    //bad request check for reason
                    if (www.error.StartsWith("400") && !string.IsNullOrEmpty(www.text))
                    {
                        //bad request, possibly out of quarters
                        Dictionary <string, string> responseData = JsonConvert.DeserializeObject <Dictionary <string, string> >(www.text);
                        if (responseData.ContainsKey("message"))
                        {
                            request.failedDelegate(responseData["message"]);
                        }
                        else
                        {
                            StartCoroutine(CreateTransferRequestCall(request));
                        }
                    }
                    else
                    {
                        //fallback to normal transfer automatically
                        StartCoroutine(CreateTransferRequestCall(request));
                    }
                }
                else
                {
                    Debug.LogError(www.error);
                    request.failedDelegate(www.error);
                }
            }
            else
            {
                Debug.Log(www.text);

                string response = www.text;
                Debug.Log("Autoapprove response: " + response);

                Dictionary <string, string> responseData = JsonConvert.DeserializeObject <Dictionary <string, string> >(response);

                request.txId = responseData["txId"];

                Debug.Log("Autoapproved request txId is: " + request.txId);

                request.successDelegate(request.txId);
            }
        }