Example #1
0
        public static int GetSubscriptions(string username)
        {
            GitHubAPIRequestModel gitHubAPIRequestModel = new GitHubAPIRequestModel();

            string cUrl = gitHubAPIRequestModel.cUrlGetSubscriptions + username + "/subscriptions?client_id=" + gitHubAPIRequestModel.OAuthApplicationClient_Id + "&client_secret=" + gitHubAPIRequestModel.OAuthApplicationClient_Secret;
            int    total_number_of_subscriptions = 0;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cUrl);

            request.Accept    = "application/vnd.github.v3+json";
            request.UserAgent = "Coder's Footprint";

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        JArray subscriptionsArray = JArray.Parse(reader.ReadToEnd());

                        foreach (JObject subscription in subscriptionsArray.Children <JObject>())
                        {
                            foreach (JProperty subscriptionProperties in subscription.Properties())
                            {
                                if (subscriptionProperties.Name == "id")
                                {
                                    total_number_of_subscriptions++;
                                }
                            }
                        }
                    }

            return(total_number_of_subscriptions);
        }
Example #2
0
        public static List <GitHubAPIUserRepository> GetUserRepositories(string username)
        {
            GitHubAPIRequestModel gitHubAPIRequestModel = new GitHubAPIRequestModel();

            List <GitHubAPIUserRepository> gitHubAPIUserRepositories = new List <GitHubAPIUserRepository>();
            List <int>    repositoryId   = new List <int>();
            List <string> repositoryName = new List <string>();

            int    totalRepositories = 0;
            string cUrl = gitHubAPIRequestModel.cUrlGetRepositories + username + "/repos?client_id=" + gitHubAPIRequestModel.OAuthApplicationClient_Id + "&client_secret=" + gitHubAPIRequestModel.OAuthApplicationClient_Secret;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cUrl);

            request.Accept    = "application/vnd.github.v3+json";
            request.UserAgent = "Coder's Footprint";

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        JArray repositoriesArray = JArray.Parse(reader.ReadToEnd());

                        foreach (JObject repository in repositoriesArray.Children <JObject>())
                        {
                            foreach (JProperty repositoryProperties in repository.Properties())
                            {
                                if (repositoryProperties.Name == "id")
                                {
                                    repositoryId.Add((int)repositoryProperties.Value);
                                    totalRepositories++;
                                }
                                if (repositoryProperties.Name == "name")
                                {
                                    repositoryName.Add((string)repositoryProperties.Value);
                                }
                            }
                        }
                    }

            for (int i = 0; i < repositoryId.Count; i++)
            {
                GitHubAPIUserRepository newGitHubAPIUserRepository = new GitHubAPIUserRepository()
                {
                    Id   = repositoryId[i],
                    Name = repositoryName[i]
                };

                gitHubAPIUserRepositories.Add(newGitHubAPIUserRepository);
            }

            return(gitHubAPIUserRepositories);
        }
Example #3
0
        public GitHubAPIUserProfile GetUserProfile([FromBody] EmailRequestModel emailRequest)
        {
            GitHubAPIRequestModel gitHubAPIRequestModel = new GitHubAPIRequestModel();
            DateTime TestedAt = DateTime.Now;

            string user_email = emailRequest.Email;

            if (user_email == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, String.Format("Request Body Error! Empty key/value pair or wrong content type, please use x-www-form-urlencoded.")));
            }

            string cURL = gitHubAPIRequestModel.cUrlSearchUserByEmail + user_email + "&client_id=" + gitHubAPIRequestModel.OAuthApplicationClient_Id + "&client_secret=" + gitHubAPIRequestModel.OAuthApplicationClient_Secret;

            string username = GetUsername(cURL);

            if (username == "0")
            {
                username = "******";

                PlatformsController.CalculateTotalPoints(false, user_email, "GitHub", PlatformsController.GetPoints(false, 1));

                List <GitHubAPIUserRepository> gitHubAPINoUserRepositories = new List <GitHubAPIUserRepository>();
                gitHubAPINoUserRepositories.Clear();

                GitHubAPIUserProfile gitHubAPINoUserProfile = new GitHubAPIUserProfile
                {
                    Username          = username,
                    Organizations     = 0,
                    Followers         = 0,
                    Subscriptions     = 0,
                    TotalRepositories = 0,
                    Repositories      = gitHubAPINoUserRepositories,
                    Tested_At         = TestedAt.ToString("dd/MM/yyyy HH:mm")
                };

                return(gitHubAPINoUserProfile);
            }
            else
            {
                PlatformsController.CalculateTotalPoints(true, user_email, "GitHub", PlatformsController.GetPoints(true, 1));

                List <GitHubAPIUserRepository> gitHubAPIUserRepositoriesCollection = GetUserRepositories(username);

                int total_number_of_repositories = gitHubAPIUserRepositoriesCollection.Count;

                int followers = GetFollowers(username);

                int subscriptions = GetSubscriptions(username);

                int organizations = GetOrganizations(username);

                GitHubAPIUserProfile gitHubAPIUserProfile = new GitHubAPIUserProfile
                {
                    Username          = username,
                    Organizations     = organizations,
                    Followers         = followers,
                    Subscriptions     = subscriptions,
                    TotalRepositories = total_number_of_repositories,
                    Repositories      = gitHubAPIUserRepositoriesCollection,
                    Tested_At         = TestedAt.ToString("dd/MM/yyyy HH:mm")
                };

                return(gitHubAPIUserProfile);
            }
        }