Пример #1
0
        /// <summary>
        /// Obtiene el token del servidor.
        /// </summary>
        public void GetTokenFromServer()
        {
            // Obtengo datos usuario
            string[] userData = UserDataDefaults.GetLoginData();

            // Preparo conexion
            if (userData != null)
            {
                // URI
                URIData uri = new URIData(uris[(int)URIS.GetToken].Url, uris[(int)URIS.GetToken].Method, uris[(int)URIS.GetToken].Accept, uris[(int)URIS.GetToken].ContentType, uris[(int)URIS.GetToken].Auth);

                // Action
                Action <bool, string> complete = null;

                // Inicializo
                token = "";

                // Añado datos
                uri.AddData(userData);

                // Action
                complete += (arg1, arg2) =>
                {
                    // Compruebo conexion
                    if (arg1)
                    {
                        // Compruebo datos
                        if (!string.IsNullOrWhiteSpace(arg2))
                        {
                            // Guardo token
                            token = arg2;

                            // Guardo token en local
                            UserDataDefaults.SetToken(arg2);
                        }
                    }
                    lock (l)
                    {
                        Monitor.Pulse(l);
                    }
                };

                // Obtengo datos del servidor
                Connection(uri, complete);

                // Espero a que finalice
                lock (l)
                {
                    Monitor.Wait(l);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Obtiene datos del servidor.
        /// </summary>
        /// <param name="id">ID de la URI.</param>
        /// <param name="data">Datos para crear la consulta.</param>
        /// <param name="bodyData">Datos que van en el body de la consulta.</param>
        /// <param name="complete">Conexion completada.</param>
        public void GetData(int id, string[] data, string bodyData, Action <string> complete)
        {
            // Reintento token
            bool flag = true;

            // URI
            URIData uri = new URIData(uris[id].Url, uris[id].Method, uris[id].Accept, uris[id].ContentType, uris[id].Auth);

            // Request
            uri.AddData(data);

            // Body
            if (bodyData != null)
            {
                if (bodyData.Length > 0)
                {
                    uri.Body = bodyData;
                }
            }

            // Action
            Action <bool, string> actionConnection = null;

            actionConnection += (arg1, arg2) =>
            {
                // Compruebo resultado conexion
                if (arg1)
                {
                    // Devuelvo datos obtenidos del servidor
                    complete(arg2);
                }
                else
                {
                    // Compruebo si el token esta caducado
                    if ((arg2 ?? "null").Equals("401") && flag)
                    {
                        // Renuevo token
                        GetTokenFromServer();
                        flag = false;
                        Connection(uri, actionConnection);
                    }
                    else
                    {
                        // Conexion erronea
                        complete(null);
                    }
                }
            };

            // Realizo conexion
            Connection(uri, actionConnection);
        }