Exemplo n.º 1
0
            public async Task StartAsync()
            {
                pauseRequest = false;
                while (true)
                {
                    if (pauseRequest)
                    {
                        MessageAppended?.Invoke("Paused.");
                        Status = Networker.NetworkStatus.Paused;
                        return;
                    }
                    var ans = await GetNextPageAsync();

                    if (ans == null)
                    {
                        break;
                    }
                    NewFileListGot?.Invoke(ans);
                }
                MessageAppended?.Invoke("Done.");
            }
            public async Task GetNextPageAsync(string fields = "nextPageToken,incompleteSearch,files(id,name,mimeType)")
            {
                string url = $"https://www.googleapis.com/drive/v3/files?corpora=user&fields={fields}";

                url += $"&pageSize={pageSize}";
                if (pageToken != null)
                {
                    url += $"&pageToken={pageToken}";
                }
                url += $"&q={System.Net.WebUtility.UrlEncode(searchPattern)}";
                HttpWebRequest request = WebRequest.CreateHttp(url);

                //MessageAppended?.Invoke($"url: {url}");
                request.Headers["Authorization"] = "Bearer " + (await Drive.GetAccessTokenAsync());
                request.Method = "GET";
                using (var response = await GetHttpResponseAsync(request))
                {
                    if (response == null)
                    {
                        MessageAppended?.Invoke("Null response");
                        Status = SearchStatus.ErrorNeedResume;
                        return;
                    }
                    MessageAppended?.Invoke($"Http response: {response.StatusCode} ({(int)response.StatusCode})");
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string resultText;
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (var reader = new System.IO.StreamReader(responseStream))
                            {
                                resultText = await reader.ReadToEndAsync();
                            }
                        }
                        NextPageString = resultText;
                        var result = JsonConvert.DeserializeObject <temporaryClassForResponseBody>(resultText);
                        if (result.incompleteSearch)
                        {
                            MessageAppended?.Invoke("Warning! This is an Incomplete Search");
                            MyLogger.Log("Warning! This is an Incomplete Search");
                        }
                        FileListGot.Clear();
                        if (result.files != null)
                        {
                            foreach (var file in result.files)
                            {
                                FileListGot.Add(new Tuple <string, string, string>(file.id, file.name, file.mimeType));
                            }
                        }
                        NewFileListGot?.Invoke(FileListGot);
                        if (result.nextPageToken == null)
                        {
                            Status = SearchStatus.Completed;
                        }
                        else
                        {
                            pageToken = result.nextPageToken;
                            Status    = SearchStatus.Paused;
                        }
                        return;
                    }
                    else
                    {
                        MessageAppended?.Invoke("Http response isn't OK!");
                        MessageAppended?.Invoke(await LogHttpWebResponse(response, true));
                        Status = SearchStatus.ErrorNeedResume;
                        return;
                    }
                }
            }