Esempio n. 1
0
        private void settingsButton_Click(object sender, RoutedEventArgs e)
        {
            Settings settingsWindow = new Settings();

            settingsWindow.Top  = this.Top;
            settingsWindow.Left = this.Left;
            settingsWindow.Clyp = Clyp;
            settingsWindow.ShowDialog();

            if (Clyp.login_attempt)
            {
                Clyp = JsonConvert.DeserializeObject <ClypSession>(ClypQuery("https://api.clyp.it/oauth2/token",
                                                                             HttpMethod.Post,
                                                                             new Dictionary <string, string>()
                {
                    { "Authorization", "Basic MjkzMTE5Og==" }, { "Content-Type", "application/x-www-form-urlencoded" }
                },
                                                                             new Dictionary <string, string>()
                {
                    { "grant_type", "password" }, { "username", settingsWindow.usernameTextbox.Text }, { "password", settingsWindow.passwordTextbox.Password.ToString() }
                },
                                                                             this.Clyp));
                Clyp.user = JsonConvert.DeserializeObject <ClypUser>(ClypQuery("https://api.clyp.it/me",
                                                                               HttpMethod.Get,
                                                                               new Dictionary <string, string>()
                {
                    { "authorization", "Bearer " + Clyp.access_token }, { "Content-Type", "application/x-www-form-urlencoded" }, { "x-client-type", "WebAlfa" }
                },
                                                                               new Dictionary <string, string>(),
                                                                               this.Clyp));
                username.Content = "Logged in as: " + Clyp.user.FirstName;

                if (Clyp.user.NotificationsSummary.Count > 0)
                {
                    BitmapImage unreadNotificationsBell = new BitmapImage();
                    unreadNotificationsBell.BeginInit();
                    unreadNotificationsBell.UriSource = new Uri("pack://application:,,,/ClypItWin;component/assets/notification_unread.png");
                    unreadNotificationsBell.EndInit();
                    notificationBell.Source          = unreadNotificationsBell;
                    notificationBell.IsEnabled       = true;
                    notificationsCircle.Visibility   = Visibility.Visible;
                    numberOfNotifications.Visibility = Visibility.Visible;
                    numberOfNotifications.Content    = Clyp.user.NotificationsSummary.Count;
                }

                while (Clyp.access_token == null && Clyp.login_attempt)
                {
                    settingsButton = null;
                    settingsButton_Click(new object(), new RoutedEventArgs());
                }
            }
        }
Esempio n. 2
0
        public static string ClypQuery(string Url, HttpMethod Method, Dictionary <string, string> Headers, Dictionary <string, string> Parameters, ClypSession Clyp)
        {
            using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage();
                request.RequestUri = new Uri(Url);
                request.Method     = Method;

                request.Content = new FormUrlEncodedContent(Parameters);

                foreach (var header in Headers)
                {
                    request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }

                var response = new HttpResponseMessage();

                if (Method == HttpMethod.Get)
                {
                    foreach (var header in Headers)
                    {
                        client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
                        return(client.GetAsync(Url).Result.Content.ReadAsStringAsync().Result);
                    }
                }
                else
                {
                    var task = client.SendAsync(request)
                               .ContinueWith((taskWithMessage) =>
                    {
                        response = taskWithMessage.Result;
                    });
                    task.Wait();
                }

                if (response == new HttpResponseMessage())
                {
                    return("Failed");
                }
                else
                {
                    return(response.Content.ReadAsStringAsync().Result);
                }
            }
        }