示例#1
0
        public async Task DownloadDiagnosisKeysAsync(DiagnosisKeyEntry diagnosisKeyEntry, string path)
        {
            Uri uri = new Uri(diagnosisKeyEntry.Url);
            HttpResponseMessage response = await _client.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                string fileName   = uri.Segments[uri.Segments.Length - 1];
                string outputPath = Path.Combine(path, fileName);

                byte[] buffer = new byte[BUFFER_LENGTH];

                using BufferedStream bs = new BufferedStream(await response.Content.ReadAsStreamAsync());
                using FileStream fs     = File.OpenWrite(outputPath);

                int len = 0;
                while ((len = await bs.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    await fs.WriteAsync(buffer, 0, len);
                }
            }
            else
            {
                Debug.Print($"DownloadDiagnosisKeysAsync {response.StatusCode}");
            }
        }
        public async void DownloadDiagnosisKeysAsyncTests_HttpError(System.Net.HttpStatusCode statusCode)
        {
            var content = new ByteArrayContent(Encoding.UTF8.GetBytes(""));

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(@"application/zip");

            var client = HttpClientUtils.CreateHttpClient(
                statusCode,
                content
                );

            mockClientService.Setup(x => x.Create()).Returns(client);

            var entry = new DiagnosisKeyEntry()
            {
                Region  = 1,
                Url     = "https://example.com/1.zip",
                Created = 12345678
            };

            var tmpPath = Path.GetTempPath();

            await Assert.ThrowsAsync <HttpRequestException>(async() =>
            {
                var unitUnderTest = CreateRepository();
                string result     = await unitUnderTest.DownloadDiagnosisKeysAsync(entry, tmpPath, default);
            });

            var outputPath = Path.Combine(tmpPath, "1.zip");

            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }
        }
        public async void DownloadDiagnosisKeysAsyncTests_Success()
        {
            var content = new ByteArrayContent(Encoding.UTF8.GetBytes(""));

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(@"application/zip");

            var client = HttpClientUtils.CreateHttpClient(
                HttpStatusCode.OK,
                content
                );

            mockClientService.Setup(x => x.Create()).Returns(client);

            var entry = new DiagnosisKeyEntry()
            {
                Region  = 1,
                Url     = "https://example.com/1.zip",
                Created = 12345678
            };

            var tmpPath       = Path.GetTempPath();
            var unitUnderTest = CreateRepository();
            var result        = await unitUnderTest.DownloadDiagnosisKeysAsync(entry, tmpPath, default);

            Assert.Equal(Path.Combine(tmpPath, "1.zip"), result);

            if (File.Exists(result))
            {
                File.Delete(result);
            }
        }
        public async Task <string> DownloadDiagnosisKeysAsync(DiagnosisKeyEntry diagnosisKeyEntry, string outputDir, CancellationToken cancellationToken)
        {
            Uri uri = new Uri(diagnosisKeyEntry.Url);

            HttpResponseMessage response = await _httpClient.GetAsync(uri, cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                string fileName   = uri.Segments[uri.Segments.Length - 1];
                string outputPath = Path.Combine(outputDir, fileName);

                byte[] buffer = new byte[BUFFER_LENGTH];

                using BufferedStream bs = new BufferedStream(await response.Content.ReadAsStreamAsync());
                using FileStream fs     = File.OpenWrite(outputPath);

                int len = 0;
                while ((len = await bs.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    await fs.WriteAsync(buffer, 0, len);
                }

                return(outputPath);
            }
            else
            {
                _loggerService.Debug($"DownloadDiagnosisKeysAsync {response.StatusCode}");
                throw new HttpRequestException($"Request failed with status code {response.StatusCode}");
            }
        }