Exemple #1
0
        // Creates a HTTP request to access the specified URI.
        private static HttpWebRequest CreateRequest(ListParameters p, Uri uri)
        {
            HttpWebRequest request = GitHubApi.CreateRequest(uri, p.UserName, p.Password);

            request.Accept = "application/vnd.github-commitcomment.html+json";
            return(request);
        }
        private Task <HttpWebResponse> MakeApiRequest(AboutViewModel model)
        {
            // get the root API URI, which will return the X-RateLimit-Remaining HTTP header
            Uri            uri     = new Uri("https://api.github.com");
            HttpWebRequest request = GitHubApi.CreateRequest(uri);

            return(request.GetHttpResponseAsync());
        }
Exemple #3
0
        private Task <List <GitHubComment> > GetCommits(ListParameters p, List <GitHubComment> comments)
        {
            List <Task> tasks = new List <Task>();

            if (p.Version == 2)
            {
                object lockObject = new object();
                m_commits = new Dictionary <string, GitHubCommit>();

                foreach (var commitId in comments.Select(c => c.commit_id).Distinct())
                {
                    // look up commit in cache
                    var commit = (GitHubCommit)HttpContext.Cache.Get("commit:" + commitId);

                    if (commit != null)
                    {
                        // if found, store it locally (in case it gets evicted from cache)
                        lock (lockObject)
                            m_commits.Add(commitId, commit);
                    }
                    else
                    {
                        // if not found, request it
                        string baseUri     = p.Server == "api.github.com" ? @"https://{0}/repos/" : @"http://{0}/api/v3/repos/";
                        string uriTemplate = baseUri + @"{1}/{2}/commits/{3}";
                        Uri    uri         = new Uri(string.Format(CultureInfo.InvariantCulture, uriTemplate, Uri.EscapeDataString(p.Server),
                                                                   Uri.EscapeDataString(p.User), Uri.EscapeDataString(p.Repo), Uri.EscapeDataString(commitId)));

                        var request = GitHubApi.CreateRequest(uri, p.UserName, p.Password);
                        tasks.Add(request.GetHttpResponseAsync().ContinueWith(t =>
                        {
                            // parse the commit JSON
                            GitHubCommit downloadedCommit;
                            using (HttpWebResponse response = t.Result)
                                using (Stream stream = response.GetResponseStream())
                                    using (TextReader reader = new StreamReader(stream, Encoding.UTF8))
                                        downloadedCommit = JsonSerializer.DeserializeFromReader <GitHubCommit>(reader);

                            // store in cache
                            string downloadedCommitId = downloadedCommit.sha;
                            HttpContext.Cache.Insert("commit:" + downloadedCommitId, downloadedCommit);

                            // also store it locally (in case it gets evicted from cache)
                            lock (lockObject)
                                m_commits.Add(downloadedCommitId, downloadedCommit);
                        }));
                    }
                }
            }

            return(TaskUtility.ContinueWhenAll(tasks.ToArray(), t => comments));
        }