Exemple #1
0
        static ApiService()
        {
            client = new HttpClient()
            {
                BaseAddress = new Uri(Routes.Base)
            };

            client.DefaultRequestHeaders.Clear();

            if (Settings.Contains("token"))
            {
                string tokenString = (string)Settings.GetValue("token");

                Token token = JsonService.Deserialize <Token>(tokenString);

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Auth_Token);
            }
            else
            {
                if (Settings.Contains("user"))
                {
                    string userString = (string)Settings.GetValue("user");
                    User   user       = JsonService.Deserialize <User>(userString);

                    LoginViewModel model = new LoginViewModel
                    {
                        Email    = user.Email,
                        Password = user.Password
                    };

                    var result = Account.Login(model).GetAwaiter().GetResult();
                }
            }
        }
Exemple #2
0
            public static async Task <HttpResult> Login(LoginViewModel model)
            {
                try
                {
                    string route       = Routes.AccountLogin;
                    string modelString = JsonService.Serialize(model);

                    var response = await client.PostAsync(route, GetStringContent(modelString));

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string answer = await response.Content.ReadAsStringAsync();

                        Settings.Save("token", answer);
                        Token token = JsonService.Deserialize <Token>(answer);
                        await RequestUserDetails(token.Id);

                        return(new HttpResult(true, response.StatusCode, answer));
                    }
                    return(new HttpResult(false, response.StatusCode, await response.Content.ReadAsStringAsync()));
                }
                catch (Exception e)
                {
                    return(new HttpResult(false, e.Message));
                }
            }
Exemple #3
0
            public static async Task <HttpResult> Login(SignUpViewModel sender)
            {
                try
                {
                    LoginViewModel model = GetLoginViewModel(sender);

                    string modelString = JsonConvert.SerializeObject(model, Formatting.Indented);

                    HttpResponseMessage response = await client.PostAsync("account/login", new StringContent(modelString, Encoding.UTF8, "application/json"));

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var answer = await response.Content.ReadAsStringAsync();

                        Settings.Save("token", answer);

                        Token token = JsonService.Deserialize <Token>(answer);
                        User  user  = await RequestUserDetails(token.Id);

                        if (user != null)
                        {
                            string userString = JsonService.Serialize(user);
                            Settings.Save("user", userString);
                            return(new HttpResult(true, response.StatusCode));
                        }
                    }

                    return(new HttpResult(false, response.StatusCode, response.Content.ToString()));
                }
                catch (Exception e)
                {
                    return(new HttpResult(false, e.Message));
                }
            }
Exemple #4
0
            public static async Task <HttpResult> SignUp(SignUpViewModel sender)
            {
                try
                {
                    string modelString = JsonConvert.SerializeObject(sender, Formatting.Indented);

                    HttpResponseMessage response = await client.PostAsync(Routes.AccountRegister, GetStringContent(modelString));

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var tokenString = await response.Content.ReadAsStringAsync();

                        Settings.Save("token", tokenString);

                        Token token = JsonService.Deserialize <Token>(tokenString);
                        User  user  = await RequestUserDetails(token.Id);

                        if (user != null)
                        {
                            string userString = JsonService.Serialize(user);
                            Settings.Save("user", userString);
                            return(new HttpResult(true, response.StatusCode));
                        }
                    }

                    string message = await response.Content.ReadAsStringAsync();

                    return(new HttpResult(false, response.StatusCode, await response.Content.ReadAsStringAsync()));
                }
                catch (Exception e)
                {
                    return(new HttpResult(false, e.Message));
                }
            }
Exemple #5
0
            public static async Task <User> RequestUserDetails(string userId)
            {
                try
                {
                    EnsureTokenExists();
                    string route = Routes.AccountGetUserById + userId;
                    HttpResponseMessage response = await client.GetAsync(route);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string userString = await response.Content.ReadAsStringAsync();

                        User user = JsonService.Deserialize <User>(userString);
                        Settings.Save("user", userString);
                        return(user);
                    }

                    return(null);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Alert.Send("Error de connexió");
                    return(null);
                }
            }
Exemple #6
0
            public static async Task <List <Forum> > Subscriptions()
            {
                try
                {
                    EnsureTokenExists();

                    User   user  = Settings.GetLoggedUser();
                    string route = Routes.AccountSubscribedForumsByUserId + user.Id.ToString();

                    HttpResponseMessage response = await client.GetAsync(route);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string forumsString = await response.Content.ReadAsStringAsync();

                        return(JsonService.Deserialize <List <Forum> >(forumsString));
                    }
                    return(new List <Forum>());
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    return(new List <Forum>());
                }
            }
        public static async Task <bool> RequestSignUp(SignUpViewModel sender)
        {
            try
            {
                string modelString = JsonConvert.SerializeObject(sender, Formatting.Indented);

                HttpResponseMessage response = await client.PostAsync("account/register", new StringContent(modelString, Encoding.UTF8, MediaType));

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var answer = await response.Content.ReadAsStringAsync();

                    Settings.Save("token", answer);

                    Token token = JsonService.Deserialize <Token>(answer);
                    User  user  = await RequestLoggedUserDetails(token.Id);

                    if (user != null)
                    {
                        string userString = JsonService.Serialize(user);
                        Settings.Save("user", userString);
                        return(true);
                    }
                }

                return(false);
            }
            catch
            {
                return(false);
            }
        }
Exemple #8
0
        public static async Task <HttpResult> Login(LoginViewModel model)
        {
            string route       = Routes.AccountLogin;
            string modelString = JsonService.Serialize(model);

            HttpResponseMessage response = await client.PostAsync(route, GetStringContent(modelString));

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string tokenString = await response.Content.ReadAsStringAsync();

                Settings.Save("token", tokenString);

                Token token = JsonService.Deserialize <Token>(tokenString);
                Authorize(token);
                var result = await GetUserDetails(token.Id);

                if (result.IsSuccess)
                {
                    return(new HttpResult(true, response.StatusCode));
                }

                return(new HttpResult(false, "Failed to retrieve user details"));
            }
            string message = await response.Content.ReadAsStringAsync();

            return(new HttpResult(false, response.StatusCode, await response.Content.ReadAsStringAsync()));
        }
        public static async Task <User> RequestLoggedUserDetails(string userId)
        {
            HttpResponseMessage response = await client.GetAsync(Routes.AccountGetUser);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string userString = await response.Content.ReadAsStringAsync();

                User user = JsonService.Deserialize <User>(userString);
                return(user);
            }
            else
            {
                for (int i = 0; i < 3; i++)
                {
                    response = await client.PostAsync(Routes.AccountGetUser, GetStringContent(userId));

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string userString = await response.Content.ReadAsStringAsync();

                        User user = JsonService.Deserialize <User>(userString);
                        return(user);
                    }
                }
                return(null);
            }
        }
Exemple #10
0
        private static async Task <List <Forum> > CheckForumListResponse(HttpResponseMessage response)
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                string forumsString = await response.Content.ReadAsStringAsync();

                return(JsonService.Deserialize <List <Forum> >(forumsString));
            }
            return(new List <Forum>());
        }
Exemple #11
0
        private static void EnsureTokenExists()
        {
            client.DefaultRequestHeaders.Clear();

            string tokenString = (string)Settings.GetValue("token");

            Token token = JsonService.Deserialize <Token>(tokenString);

            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Auth_Token);
        }
Exemple #12
0
        private static async Task <User> CheckUserResponse(HttpResponseMessage response)
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                string userString = await response.Content.ReadAsStringAsync();

                User user = JsonService.Deserialize <User>(userString);
                return(user);
            }

            return(null);
        }
Exemple #13
0
            public static async Task <Forum> Select(Guid forumId)
            {
                try
                {
                    string route = Routes.ForumGetById + forumId.ToString();

                    var response = await client.GetAsync(route);

                    string message = await response.Content.ReadAsStringAsync();

                    return(JsonService.Deserialize <Forum>(message));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    return(null);
                }
            }
        static ApiService()
        {
            if (!client.DefaultRequestHeaders.Contains("Authorization"))
            {
                if (Settings.Contains("token"))
                {
                    string tokenString = (string)Settings.GetValue("token");

                    Token token = JsonService.Deserialize <Token>(tokenString);

                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Auth_Token);
                }
                else
                {
                    if (Settings.Contains("user_email"))
                    {
                        string email = (string)Settings.GetValue("user_email");
                        string pass  = (string)Settings.GetValue("user_password");

                        LoginViewModel model = new LoginViewModel
                        {
                            Email    = email,
                            Password = pass
                        };

                        var result = RequestLogin(model).GetAwaiter().GetResult();

                        if (!result)
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                result = RequestLogin(model).GetAwaiter().GetResult();
                                if (result)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #15
0
            public static async Task <List <Forum> > Forums(Guid userId)
            {
                try
                {
                    EnsureTokenExists();
                    string route = Routes.AccountGetSubscribedForums + userId.ToString();
                    HttpResponseMessage response = await client.GetAsync(route);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string forumsString = await response.Content.ReadAsStringAsync();

                        List <Forum> forums = JsonService.Deserialize <List <Forum> >(forumsString);
                        return(forums);
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            response = await client.GetAsync(Routes.ForumSubscribed + userId.ToString());

                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                string forumsString = await response.Content.ReadAsStringAsync();

                                List <Forum> forums = JsonService.Deserialize <List <Forum> >(forumsString);
                                return(forums);
                            }
                        }

                        return(new List <Forum>());
                    }
                }
                catch (Exception)
                {
                    Alert.Send("Error de connexió");
                    return(new List <Forum>());
                }
            }
Exemple #16
0
            public static async Task <List <Forum> > Forums()
            {
                try
                {
                    EnsureTokenExists();
                    HttpResponseMessage response = await client.GetAsync(Routes.AccountPersonalForums);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string forumsString = await response.Content.ReadAsStringAsync();

                        return(JsonService.Deserialize <List <Forum> >(forumsString));
                    }

                    return(new List <Forum>());
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    return(new List <Forum>());
                }
            }
Exemple #17
0
        public static User GetLoggedUser()
        {
            User user = JsonService.Deserialize <User>((string)GetValue("user"));

            return(user);
        }