示例#1
0
        public async Task <GoogleEvent.UpdateResponse> UploadData()
        {
            IsLoadingData = true;
            ObjEvent.FixDates(false);

            var request = new GoogleEventUpdateRequest {
                ObjGoogleEvent   = ObjEvent,
                RelatedOwnerGuid = App.VM.user.Guid,
            };

            try
            {
                var ws     = new WebServiceV2();
                var config = new WebServiceV2.UrlToStringConfiguration
                {
                    url        = Globals.RestApiV1 + "loft/event/" + ObjEvent.ID + "/update",
                    Type       = WebServiceV2.UrlToStringConfiguration.RequestType.JsonRaw,
                    Verb       = WebServiceV2.UrlToStringConfiguration.RequestVerb.POST,
                    RawContent = JsonConvert.SerializeObject(request),
                    timeout    = TimeSpan.FromSeconds(60)
                };
                if (string.IsNullOrWhiteSpace(ObjEvent.ID))
                {
                    config.url = Globals.RestApiV1 + "loft/event/null/update";
                }
                var res = await ws.UrlToString(config);

                if (res == null)
                {
                    Visual1993.Console.WriteLine("bad response"); return(new GoogleEvent.UpdateResponse());
                }
                var resObj = JsonConvert.DeserializeObject <GoogleEvent.UpdateResponse>(res);
                if (resObj != null)
                {
                    this.ObjEvent = resObj.item;
                    if (this.ObjEvent == null)
                    {
                        return(resObj);
                    }
                    this.ObjInternalEvent.data.RelatedGoogleEventID = this.ObjEvent.ID;
                    if (IsNew == false)
                    {
                        var resInterno = await this.ObjInternalEvent.update();
                    }
                    if (IsNew)
                    {
                        var resInterno = await this.ObjInternalEvent.insert();

                        IsNew = false;
                    }                    //qui non ci posso mettere l'else
                }
                return(resObj);
            }
            finally {
                IsLoadingData = false;
            }
        }
示例#2
0
        public static async Task <ResponseList <AccessToken> > getFromSocial(string SocialID, string SocialLastAccessToken, UserData.SocialProvider SocialProvider)
        {
            //TODO: controlla che restituisca solo gli utenti con SocialProvider<>0
            if (Constants.IsDownloadingToken)
            {
                return(new Gateway <PersonalizedData, AccessToken> .ResponseList <AccessToken>());
            }
            else
            {
                Constants.IsDownloadingToken = true;
            }

            var parameters = new GetTokenQuery
            {
                SocialID = SocialID,
                SocialLastAccessToken = SocialLastAccessToken,
                SocialProvider        = SocialProvider,
            };
            string url   = Constants.GatewayUrl + "?action=getUserToken&AppToken=" + Constants.RandomAppToken;
            var    pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("parameters", JsonConvert.SerializeObject(parameters)));

            WebServiceV2 webRequest = new WebServiceV2();
            var          result     = await webRequest.UrlToString(url, pairs, false, Constants.HTTPTimeoutMini);

            Constants.IsDownloadingToken = false;
            if (result == null || result.Length < 1)
            {
                return(new ResponseList <AccessToken> {
                    errorMessage = "no response", state = WebServiceV2.WebRequestState.TimeOut
                });
            }
            ResponseList <AccessToken> obj = new ResponseList <AccessToken>();

            try
            {
                obj = JsonConvert.DeserializeObject <ResponseList <AccessToken> >(result);
                if (obj.errorMessage != null)
                {
                    Debug.WriteLine(obj.errorMessage + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(new ResponseList <AccessToken> {
                    state = WebServiceV2.WebRequestState.GenericError
                });
            }
            return(obj);
        }
示例#3
0
        public async Task <AccessTokenExistsResponse> esisteUserFromSocialToken(string SocialToken, UserData.SocialProvider SocialProvider)
        {
            var parameters = new GetTokenQuery
            {
                SocialLastAccessToken = SocialToken,
                SocialProvider        = SocialProvider,
            };
            string url   = Constants.GatewayUrl + "?action=esisteUserFromSocialToken&AppToken=" + Constants.RandomAppToken;
            var    pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("parameters", JsonConvert.SerializeObject(parameters)));

            WebServiceV2 webRequest = new WebServiceV2();
            var          result     = await webRequest.UrlToString(url, pairs, false, Constants.HTTPTimeoutMini);

            Constants.IsDownloadingToken = false;
            if (result == null || result.Length < 1)
            {
                return(new AccessTokenExistsResponse {
                    errorMessage = "no response", state = WebServiceV2.WebRequestState.TimeOut
                });
            }
            AccessTokenExistsResponse obj = new AccessTokenExistsResponse();

            try
            {
                obj = JsonConvert.DeserializeObject <AccessTokenExistsResponse>(result);
                if (obj.errorMessage != null)
                {
                    Debug.WriteLine(obj.errorMessage + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(new AccessTokenExistsResponse {
                    state = WebServiceV2.WebRequestState.GenericError
                });
            }
            return(obj);
        }
示例#4
0
        public static async Task <ResponseList <T2> > getAdvanced(IEnumerable <Guid> guids, List <string> fields = null, string filterQuery = "", List <ListQuery> listsQuery = null, string remoteEndpoint = null, string remoteClassName = "", string customAction = "getAdvanced", List <AdditionalParametersQuery> additionalParameters = null)
        {
            if (remoteEndpoint == null)
            {
                remoteEndpoint = Constants.RestAPI;
            }
            try
            {
                var secureToken = GatewaySecure.GatewaySecure.GetToken(Constants.GatewaySecureBlowfish);
                if (TextHelper.IsNullOrWhiteSpace(remoteEndpoint) || TextHelper.IsNullOrWhiteSpace(remoteClassName))
                {
                    return(new ResponseList <T2> {
                        state = WebServiceV2.WebRequestState.GenericError, errorMessage = "endpoint or remote class name not set"
                    });
                }
                var fieldsStr = ""; if (fields == null)
                {
                    fieldsStr = "*";
                }
                else
                {
                    fieldsStr = string.Join(",", fields);
                }
                var guidsListStr = new List <string>();
                //string url = remoteEndpoint + "?action=get&remoteClassName="+remoteClassName+"&guid="+guidStr+"&filterQuery="+filterQuery;
                var parameters = new AdvancedGetQuery {
                    filter = filterQuery, additionalParameters = additionalParameters
                };
                parameters.lists = listsQuery;
                if (guids == null)
                {
                    parameters.isAnyGuid = true;
                }
                else
                {
                    foreach (Guid val in guids)
                    {
                        guidsListStr.Add(val.ToString());
                    }
                }
                parameters.guids = guidsListStr;

                string url = remoteEndpoint + "?action=" + customAction + "&remoteClassName=" + remoteClassName + "&UserToken=" + Constants.CurrentUserToken + "&AppToken=" + Constants.RandomAppToken + "&secureToken=" + secureToken;

                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
                }
                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
                }
                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.HTTPTimeoutMini).Wait();
                }

                WebServiceV2 webRequest = new WebServiceV2();
                var          pairs      = new List <KeyValuePair <string, string> >();
                pairs.Add(new KeyValuePair <string, string>("parameters", JsonConvert.SerializeObject(parameters)));
                var result = await webRequest.UrlToString(url, pairs);

                if (result == null || result.Length < 1)
                {
                    return(new ResponseList <T2> {
                        errorMessage = "no response", state = WebServiceV2.WebRequestState.TimeOut
                    });
                }
                ResponseList <T2> obj = JsonConvert.DeserializeObject <ResponseList <T2> >(result);
                if (obj.errorMessage != null)
                {
                    Debug.WriteLine(obj.errorMessage + Environment.NewLine);
                }
                if (obj.state == WebServiceV2.WebRequestState.InvalidUserToken)
                {
                    if ((DateTime.Now - Constants.LastTokenRequestDateTime) > Constants.TokenCheckPeriod)
                    {                     //richiedi nuovo token e poi fai insert
                        Constants.LastTokenRequestDateTime = DateTime.Now;
                        var resToken = await AccessToken.getFromGeneric(Constants.CurrentUserSocialProvider, Constants.CurrentUserUsername, Constants.CurrentUserPassword, Constants.CurrentUserSocialID, Constants.CurrentUserLastAccessToken);

                        if (resToken.items.Count == 0)
                        {
                            return(new ResponseList <T2> {
                                state = WebServiceV2.WebRequestState.NotAuthorized, errorMessage = "unable to obtain access token"
                            });
                        }
                        return(await getAdvanced(guids, fields, filterQuery, listsQuery, remoteEndpoint, remoteClassName, customAction, additionalParameters));
                    }
                    else
                    {
                        return(obj);                        //che sarà invalido
                    }
                }
                else
                {
                    return(obj);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("exception in getAdvanced: " + ex.Message);
                if (ex.GetType() == typeof(TimeoutException))
                {
                    return(new ResponseList <T2> {
                        errorMessage = "timeout", state = WebServiceV2.WebRequestState.TimeOut
                    });
                }
                if (ex.GetType() == typeof(TaskCanceledException))
                {
                    return(new ResponseList <T2> {
                        errorMessage = "task cancelled", state = WebServiceV2.WebRequestState.TaskCancelled
                    });
                }
                return(new ResponseList <T2> {
                    errorMessage = "exception", state = WebServiceV2.WebRequestState.GenericNetworkError
                });
            }
        }
示例#5
0
        public static async Task <ResponseList <T2> > getBase(Guid guid, string remoteEndpoint, string remoteClassName, string filterQuery, List <ListQuery> listsQuery)
        {
            try
            {
                var secureToken = GatewaySecure.GatewaySecure.GetToken(Constants.GatewaySecureBlowfish);
                if (TextHelper.IsNullOrWhiteSpace(remoteEndpoint) || TextHelper.IsNullOrWhiteSpace(remoteClassName))
                {
                    return(new ResponseList <T2> {
                        state = WebServiceV2.WebRequestState.GenericError, errorMessage = "endpoint or remote class name not set"
                    });
                }
                var guidStr = "";
                if (guid == Guid.Empty)
                {
                    guidStr = "any";
                }
                else
                {
                    guidStr = guid.ToString();
                }
                //string url = remoteEndpoint + "?action=get&remoteClassName="+remoteClassName+"&guid="+guidStr+"&filterQuery="+filterQuery;
                var parameters = new GetQuery {
                    guid = guidStr, filter = filterQuery
                };
                parameters.lists = listsQuery;

                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
                }
                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
                }
                if (Constants.IsDownloadingToken)
                {                 //delay
                    System.Threading.Tasks.Task.Delay(Constants.HTTPTimeoutMini).Wait();
                }
                var    UserToken = Constants.CurrentUserToken;
                string url       = remoteEndpoint + "?action=get&remoteClassName=" + remoteClassName + "&UserToken=" + UserToken + "&AppToken=" + Constants.RandomAppToken + "&secureToken=" + secureToken;
                var    pairs     = new List <KeyValuePair <string, string> >();
                pairs.Add(new KeyValuePair <string, string>("parameters", JsonConvert.SerializeObject(parameters)));

                WebServiceV2 webRequest = new WebServiceV2();
                var          result     = await webRequest.UrlToString(url, pairs);

                if (result == null || result.Length < 1)
                {
                    return(new ResponseList <T2> {
                        errorMessage = "no response", state = WebServiceV2.WebRequestState.TimeOut
                    });
                }
                ResponseList <T2> obj = new ResponseList <T2>();
                try
                {
                    obj = JsonConvert.DeserializeObject <ResponseList <T2> >(result);
                    if (obj.errorMessage != null)
                    {
                        Debug.WriteLine(obj.errorMessage + Environment.NewLine);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(new ResponseList <T2> {
                        state = WebServiceV2.WebRequestState.GenericError
                    });
                }

                if (obj.state == WebServiceV2.WebRequestState.InvalidUserToken)
                {
                    Debug.WriteLine("This token is invalid: " + UserToken);
                    if ((DateTime.Now - Constants.LastTokenRequestDateTime) > Constants.TokenCheckPeriod)
                    {                     //richiedi nuovo token e poi fai insert
                        Constants.LastTokenRequestDateTime = DateTime.Now;
                        var resToken = await AccessToken.getFromGeneric(Constants.CurrentUserSocialProvider, Constants.CurrentUserUsername, Constants.CurrentUserPassword, Constants.CurrentUserSocialID, Constants.CurrentUserLastAccessToken);

                        if (resToken.items.Count == 0)
                        {
                            return(new ResponseList <T2> {
                                state = WebServiceV2.WebRequestState.NotAuthorized, errorMessage = "unable to obtain access token"
                            });
                        }
                        //Constants.CurrentUserToken = resToken.items.FirstOrDefault().data.AccessTokenValue;
                        return(await getBase(guid, remoteEndpoint, remoteClassName, filterQuery, listsQuery));
                    }
                    else
                    {
                        return(obj);                        //che sarà invalido
                    }
                }
                else
                {
                    return(obj);
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(TimeoutException))
                {
                    return(new ResponseList <T2> {
                        errorMessage = "timeout", state = WebServiceV2.WebRequestState.TimeOut
                    });
                }
                if (ex.GetType() == typeof(TaskCanceledException))
                {
                    return(new ResponseList <T2> {
                        errorMessage = "task cancelled", state = WebServiceV2.WebRequestState.TaskCancelled
                    });
                }
                return(new ResponseList <T2> {
                    errorMessage = "exception", state = WebServiceV2.WebRequestState.GenericNetworkError
                });
            }
        }
示例#6
0
        public virtual async Task <Response> remove()
        {
            var secureToken = GatewaySecure.GatewaySecure.GetToken(Constants.GatewaySecureBlowfish);

            if (TextHelper.IsNullOrWhiteSpace(remoteEndpoint) || TextHelper.IsNullOrWhiteSpace(remoteClassName))
            {
                return(new Response {
                    state = WebServiceV2.WebRequestState.GenericError, errorMessage = "endpoint or remote class name not set"
                });
            }
            if (checkProprieta() == false)
            {
                return(new Response {
                    errorMessage = "no guid"
                });
            }
            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
            }
            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
            }
            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.HTTPTimeoutMini).Wait();
            }
            string url = remoteEndpoint + "?action=remove" + "&UserToken=" + Constants.CurrentUserToken + "&AppToken=" + Constants.RandomAppToken + "&secureToken=" + secureToken;;
            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("guid", this.guid),
                new KeyValuePair <string, string>("remoteClassName", remoteClassName),
            };

            WebServiceV2 webRequest = new WebServiceV2();
            var          result     = await webRequest.UrlToString(url, pairs);

            if (result == null || result.Length < 1)
            {
                return(new Response {
                    errorMessage = "no response"
                });
            }
            Response obj = JsonConvert.DeserializeObject <Response>(result);

            if (obj.errorMessage != null)
            {
                Debug.WriteLine(obj.errorMessage + Environment.NewLine);
            }
            if (obj.state == WebServiceV2.WebRequestState.InvalidUserToken)
            {
                if ((DateTime.Now - Constants.LastTokenRequestDateTime) > Constants.TokenCheckPeriod)
                {                 //richiedi nuovo token e poi fai insert
                    Constants.LastTokenRequestDateTime = DateTime.Now;
                    var resToken = await AccessToken.getFromGeneric(Constants.CurrentUserSocialProvider, Constants.CurrentUserUsername, Constants.CurrentUserPassword, Constants.CurrentUserSocialID, Constants.CurrentUserLastAccessToken);

                    if (resToken.items.Count == 0)
                    {
                        return(new Response {
                            state = WebServiceV2.WebRequestState.NotAuthorized, errorMessage = "unable to obtain access token"
                        });
                    }
                    Constants.CurrentUserToken = resToken.items.FirstOrDefault().data.AccessTokenValue;
                    return(await remove());
                }
                else
                {
                    return(obj);                    //che sarà invalido
                }
            }
            else
            {
                return(obj);
            }
        }
示例#7
0
        public virtual async Task <Response> insert(object fullData)
        {
            var secureToken = GatewaySecure.GatewaySecure.GetToken(Constants.GatewaySecureBlowfish);

            if (TextHelper.IsNullOrWhiteSpace(remoteEndpoint) || TextHelper.IsNullOrWhiteSpace(remoteClassName))
            {
                return(new Response {
                    state = WebServiceV2.WebRequestState.GenericError, errorMessage = "endpoint or remote class name not set"
                });
            }
            if (this.guid == null || this.guid.Length < 1 || this.guid == Guid.Empty.ToString())
            {
                this.Guid = Guid.NewGuid();
            }
            if (checkProprieta() == false)
            {
                return(new Response {
                    errorMessage = "no guid"
                });
            }

            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
            }
            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.TimeoutMicroPerToken).Wait();
            }
            if (Constants.IsDownloadingToken)
            {             //delay
                System.Threading.Tasks.Task.Delay(Constants.HTTPTimeoutMini).Wait();
            }

            string url = remoteEndpoint + "?action=insert" + "&UserToken=" + Constants.CurrentUserToken + "&AppToken=" + Constants.RandomAppToken + "&secureToken=" + secureToken;
            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("guid", this.guid),
                new KeyValuePair <string, string>("remoteClassName", remoteClassName),
                new KeyValuePair <string, string>("data", JsonConvert.SerializeObject(fullData)),
            };

            WebServiceV2 webRequest = new WebServiceV2();
            var          result     = await webRequest.UrlToString(url, pairs);

            if (result == null || result.Length < 1)
            {
                return(new Response {
                    errorMessage = "no response"
                });
            }
            Response obj;

            try
            {
                obj = JsonConvert.DeserializeObject <Response>(result);
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(TimeoutException))
                {
                    return(new Response {
                        errorMessage = "timeout", state = WebServiceV2.WebRequestState.TimeOut
                    });
                }
                if (ex.GetType() == typeof(TaskCanceledException))
                {
                    return(new Response {
                        errorMessage = "task cancelled", state = WebServiceV2.WebRequestState.TaskCancelled
                    });
                }
                Debug.WriteLine("Parsing exception. Result was:" + Environment.NewLine + result);
                return(new Response {
                    errorMessage = "unable to parse json"
                });
            }
            if (obj.errorMessage != null)
            {
                Debug.WriteLine(obj.errorMessage + Environment.NewLine);
            }
            if (obj.state == WebServiceV2.WebRequestState.InvalidUserToken)
            {
                if ((DateTime.Now - Constants.LastTokenRequestDateTime) > Constants.TokenCheckPeriod)
                {                 //richiedi nuovo token e poi fai insert
                    Constants.LastTokenRequestDateTime = DateTime.Now;
                    var resToken = await AccessToken.getFromGeneric(Constants.CurrentUserSocialProvider, Constants.CurrentUserUsername, Constants.CurrentUserPassword, Constants.CurrentUserSocialID, Constants.CurrentUserLastAccessToken);

                    if (resToken.items.Count == 0)
                    {
                        return(new Response {
                            state = WebServiceV2.WebRequestState.NotAuthorized, errorMessage = "unable to obtain access token"
                        });
                    }
                    return(await insert(fullData));
                }
                else
                {
                    return(obj);                    //che sarà invalido
                }
            }
            else
            {
                return(obj);
            }
        }