Esempio n. 1
0
        public IHttpActionResult GetUserByName(string name)
        {
            GitUser user = null;
            IEnumerable <GitUserRepoList> userRepo = null;

            try
            {
                user = _repo.GetGitUserAsync(name);

                if (user == null)
                {
                    return(NotFound());
                }

                userRepo = _repo.GetUserRepoListAsync(name);

                user.UserRepoList = userRepo
                                    .OrderByDescending(s => s.stargazers_count)
                                    .Take(5)
                                    .ToList();
            }
            catch (Exception ex)
            {
                _log.ExceptionLogger(ex.Message.ToString());
                return(BadRequest());
            }
            return(Ok(user));
        }
Esempio n. 2
0
        public GitUser GetGitUserAsync(string name)
        {
            var gituser = new GitUser();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);
                HttpResponseMessage Res = null;
                try
                {
                    if (!String.IsNullOrEmpty(name))
                    {
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        client.DefaultRequestHeaders.Add("User-Agent", "C# App");
                        Res = client.GetAsync($"{name}").Result;
                    }
                }
                catch (Exception ex)
                {
                    log.ExceptionLogger(ex.Message.ToString());
                }

                if (Res.IsSuccessStatusCode)
                {
                    var UserResponse = Res.Content.ReadAsStringAsync().Result;
                    gituser = JsonConvert.DeserializeObject <GitUser>(UserResponse);
                }
                else
                {
                    return(null);
                }
            }
            return(gituser);
        }