Beispiel #1
0
        public static BsonArray GetBsonArray(string uri)
        {
            using (HttpClient http = new HttpClient())
            {
                string data = http.GetStringAsync(uri).Result;
                if (data == "unauthorized permission requested")
                {
                    throw new Exceptions.TrelloDriverRequestException("Key or token invalid");
                }
                else if (data == "The requested resource was not found.")
                {
                    throw new Exceptions.TrelloDriverRequestException("Invalid Action id");
                }

                using (var jsonReader = new JsonReader(data))
                {
                    var serializer = new BsonArraySerializer();
                    try
                    {
                        BsonArray array = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));
                        return(array);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        throw;
                    }
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var client   = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("mygamers");
            var collect  = database.GetCollection <BsonDocument>("gamers");
            var fileName = "ornekdata1.json";

            using (var streamReader = new StreamReader(fileName))
            {
                string line;
                string jsonLine = "";
                while ((line = streamReader.ReadLine()) != null)
                {
                    if (line.Trim() != "},")
                    {
                        jsonLine += line.Trim();
                    }
                    else if (line.Trim() == "},")
                    {
                        jsonLine += "}";
                        using (var jsonReader = new JsonReader(jsonLine))
                        {
                            var serializer = new BsonArraySerializer();
                            var document   = BsonDocument.Parse(jsonLine);
                            collect.InsertOne(document);
                            jsonLine = "";
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public async Task <List <BsonDocument> > GetStatistics()
        {
            // retrieve stats
            var results     = new List <BsonDocument>();
            var nextPageUrl = $"repos/{_repositoryUrl}/stats/contributors";

            // retrieve results
            var response = await GitClient.ExecuteAsGitGetTaskAsync(_token, nextPageUrl);

            // make sure the calculation has been completed
            // https://developer.github.com/v3/repos/statistics/#a-word-about-caching
            while (response.StatusCode == HttpStatusCode.Accepted)
            {
                Thread.Sleep(5000);
                response = await GitClient.ExecuteAsGitGetTaskAsync(_token, nextPageUrl);
            }

            using (var jsonReader = new MongoDB.Bson.IO.JsonReader(response.Content))
            {
                var serializer = new BsonArraySerializer();
                var bsonArray  = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));

                results.AddRange(bsonArray.Where(o => o.IsBsonDocument).Select(o => o.AsBsonDocument));
            }

            return(results);
        }
Beispiel #4
0
        public async Task <List <BsonDocument> > GetReleases()
        {
            // retrieve all releases
            var results     = new List <BsonDocument>();
            var nextPageUrl = $"repos/{_repositoryUrl}/releases?page=1";

            do
            {
                // retrieve pages results
                var response = await GitClient.ExecuteAsGitGetTaskAsync(_token, nextPageUrl);

                using (var jsonReader = new MongoDB.Bson.IO.JsonReader(response.Content))
                {
                    var serializer = new BsonArraySerializer();
                    var bsonArray  = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));

                    results.AddRange(bsonArray.Where(o => o.IsBsonDocument).Select(o => o.AsBsonDocument));
                }

                // make sure the response came with a header, otherwise we're done
                var linkHeader = response.Headers.FirstOrDefault(o => o.Name == "Link")?.Value.ToString();
                if (linkHeader == null)
                {
                    break;
                }

                // make sure there's a next page, otherwise we're done
                var nextLinkMatch = Regex.Match(linkHeader, "<https:\\/\\/api\\.github\\.com\\/(.+?)>; rel=\"next\"");
                if (!nextLinkMatch.Success)
                {
                    break;
                }

                // create new request and keep going
                nextPageUrl = nextLinkMatch.Groups[1].Value;
            } while (true);

            return(results);
        }
Beispiel #5
0
        public void Refresh()
        {
            if (Lists != null)
            {
                Lists.Clear();
            }
            if (Members != null)
            {
                Members.Clear();
            }
            if (Cards != null)
            {
                Cards.Clear();
            }
            Lists   = new Dictionary <string, List>();
            Members = new Dictionary <string, Member>();
            Cards   = new Dictionary <string, Card>();

            using (HttpClient http = new HttpClient())
            {
                //Get all members
                string data = http.GetStringAsync(String.Format("https://api.trello.com/1/boards/{0}/members/?key={1}&token={2}", Id, ConnectionInfo.Key, ConnectionInfo.Token)).Result;
                using (var jsonReader = new JsonReader(data))
                {
                    var serializer = new BsonArraySerializer();
                    try
                    {
                        BsonArray lists = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));
                        foreach (var it in lists)
                        {
                            BsonDocument item     = it.AsBsonDocument;
                            string       username = item.GetValue("username").AsString;
                            string       id       = item.GetValue("id").AsString;
                            Member       member   = new Member(username, ConnectionInfo.Key);
                            Members.Add(id, member);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        throw;
                    }
                }

                //Get all lists
                // TODO: Make asyncronous and have events
                data = http.GetStringAsync(String.Format("https://api.trello.com/1/boards/{0}/lists?key={1}&token={2}", Id, ConnectionInfo.Key, ConnectionInfo.Token)).Result;

                using (var jsonReader = new JsonReader(data))
                {
                    var serializer = new BsonArraySerializer();
                    try
                    {
                        BsonArray lists = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));
                        foreach (var it in lists)
                        {
                            BsonDocument item = it.AsBsonDocument;
                            var          name = item.GetValue("name").AsString;
                            var          id   = item.GetValue("id").AsString;
                            var          list = new List(name, id, this);
                            Lists.Add(name, list);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        throw;
                    }
                }

                //Sort cards into lists
                // TODO: Make asyncronous and have events
                string uri = String.Format("https://api.trello.com/1/search?query=member:{0}%20board:{1}%20is:open%20sort:edited&card_fields=name,shortLink,idList&cards_limit=100&key={3}&token={4}", m_member.Id, Id, m_name, ConnectionInfo.Key, ConnectionInfo.Token);
                data = http.GetStringAsync(uri).Result;

                try
                {
                    BsonArray cards = BsonDocument.Parse(data).GetValue("cards").AsBsonArray;
                    foreach (var it in cards)
                    {
                        BsonDocument item        = it.AsBsonDocument;
                        var          name        = item.GetValue("name").AsString;
                        var          id          = item.GetValue("id").AsString;
                        var          idList      = item.GetValue("idList").AsString;
                        List         currentList = Lists.Where(z => z.Value.Id == idList).FirstOrDefault().Value;
                        Card         newCard     = new Card(id, name, currentList, this);
                        Cards.Add(id, newCard);
                        currentList.Cards.Add(newCard);
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
Beispiel #6
0
        public async Task GetCommits()
        {
            // retrieve all commits
            var           pageCount   = 1;
            var           totalPages  = 0;
            var           nextPageUrl = $"repos/{_repositoryUrl}/commits?page=1";
            IRestResponse response;

            do
            {
                // retrieve page results
                response = await GitClient.ExecuteAsGitGetTaskAsync(_token, nextPageUrl);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    _logger.LogError(response.Content);
                    throw new HttpRequestException("Couldn't retrieve Git data, check error message for details.");
                }

                var results = new List <BsonDocument>();

                using (var jsonReader = new MongoDB.Bson.IO.JsonReader(response.Content))
                {
                    var serializer = new BsonArraySerializer();
                    var bsonArray  = serializer.Deserialize(BsonDeserializationContext.CreateRoot(jsonReader));

                    results.AddRange(bsonArray.Where(o => o.IsBsonDocument).Select(o => o.AsBsonDocument));
                }

                // the above request only provides partial commit information, go through all of them
                //  and retrieve their full details
                var fullCommitTasks = results
                                      .Select(async commit =>
                {
                    // build commit request with the SHA as commit ID
                    var sha          = commit["sha"].AsString;
                    var fullResponse = await GitClient.ExecuteAsGitGetTaskAsync(_token, $"repos/{_repositoryUrl}/commits/{sha}");

                    // parse and return document
                    var document = BsonDocument.Parse(fullResponse.Content);
                    return(new Commit(document));
                })
                                      .ToList();

                // wait for task completion
                var fullCommits = await Task.WhenAll(fullCommitTasks);

                OnRaiseCommitQueryNext(fullCommits);

                // make sure the response came with a header, otherwise we're done
                var linkHeader = GetHeader(response, "Link");
                if (linkHeader == null)
                {
                    break;
                }

                // make sure there's a next page, otherwise we're done
                var nextLinkMatch = Regex.Match(linkHeader, "<https:\\/\\/api\\.github\\.com\\/(.+?)>; rel=\"next\"");
                if (!nextLinkMatch.Success)
                {
                    break;
                }

                // get number of total pages
                if (totalPages == 0)
                {
                    var totalPagesMatch = Regex.Match(linkHeader, "page=(\\d+)>; rel=\"last");
                    if (totalPagesMatch.Success)
                    {
                        totalPages = int.Parse(totalPagesMatch.Groups[1].Value);
                    }
                }

                // log
                _logger.LogInformation(
                    "Got page {0} of {1}, " +
                    "X-RateLimit-Remaining: {2}, " +
                    "X-RateLimit-Reset: {3}",
                    pageCount,
                    totalPages,
                    GetHeader(response, "X-RateLimit-Remaining"),
                    GetRateLimitReset(response));

                // create new request and keep going
                nextPageUrl = nextLinkMatch.Groups[1].Value;
                pageCount++;
            } while (true);

            // log
            _logger.LogInformation(
                "Got page {0} of {1}\n" +
                "\tX-RateLimit-Remaining: {2}\n" +
                "\tX-RateLimit-Reset: {3}",
                totalPages,
                totalPages,
                GetHeader(response, "X-RateLimit-Remaining"),
                GetRateLimitReset(response));
        }