Exemplo n.º 1
0
        public async Task <OneDriveChangeCollection> GetChanges(string accessToken, string path, string cursor)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("path", path);

            var requestUri = GetRequestUri(path);

            requestUri = await GetItemUri(accessToken, requestUri) + "/view.changes";

            var result = new OneDriveChangeCollection();

            using (var client = CreateHttpClient(accessToken))
            {
                var next = cursor;
                var ids  = new Dictionary <string, ItemInfo>();
                Dictionary <string, object> changes = null;
                var serializer = JsonUtils.CreateJsonSerializer();
                do
                {
                    var uri = requestUri;
                    if (!String.IsNullOrEmpty(next))
                    {
                        uri = String.Format("{0}?token={1}", requestUri, next);
                    }

                    using (var response = await client.GetAsync(uri))
                    {
                        changes = await ProcessResponse <Dictionary <string, object> >("GetChanges", response);
                    }

                    if (changes.ContainsKey("@changes.resync"))
                    {
                        if (String.IsNullOrEmpty(next))
                        {
                            throw new InvalidOperationException("Unable to sync OneDrive @changes.resync is " + changes["@changes.resync"]);
                        }

                        // resync
                        next = null;
                        changes["@changes.hasMoreChanges"] = true;
                        result = new OneDriveChangeCollection();
                        continue;
                    }

                    var items = serializer.ConvertToType <OneDriveItemCollection>(changes);

                    // changes
                    result.AddRange(GetChanges(items, ids));

                    // set next token
                    next = (string)changes["@changes.token"];
                } while ((bool)changes["@changes.hasMoreChanges"]);

                result.Cursor = next;
                return(result);
            }
        }