Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit ad927a6

Browse files
Adding a task to download the contents of a text file
1 parent 7c94cd0 commit ad927a6

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Net;
5+
using System.Text;
56
using System.Threading;
67

78
namespace GitHub.Unity
@@ -100,7 +101,7 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
100101
}
101102
}
102103

103-
class DownloadTask: TaskBase<DownloadResult>
104+
class DownloadTask : TaskBase<DownloadResult>
104105
{
105106
private IFileSystem fileSystem;
106107
private long bytes;
@@ -121,10 +122,11 @@ public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url,
121122
protected override DownloadResult RunWithReturn(bool success)
122123
{
123124
base.RunWithReturn(success);
124-
125+
125126
RaiseOnStart();
126127

127-
var downloadResult = new DownloadResult {
128+
var downloadResult = new DownloadResult
129+
{
128130
Url = Url,
129131
Destination = Destination
130132
};
@@ -213,10 +215,10 @@ public bool Download()
213215
}
214216
}
215217

216-
var responseLength = webResponse.ContentLength;
218+
var responseLength = webResponse.ContentLength;
217219
if (restarted && bytes > 0)
218220
{
219-
UpdateProgress(bytes / (float) responseLength);
221+
UpdateProgress(bytes / (float)responseLength);
220222
}
221223

222224
using (var responseStream = webResponse.GetResponseStream())
@@ -236,4 +238,44 @@ public bool Download()
236238

237239
protected string Destination { get; }
238240
}
241+
242+
class DownloadTextTask : TaskBase<string>
243+
{
244+
public float Progress { get; set; }
245+
246+
public DownloadTextTask(CancellationToken token, string url)
247+
: base(token)
248+
{
249+
Url = url;
250+
Name = "DownloadTask";
251+
}
252+
253+
protected override string RunWithReturn(bool success)
254+
{
255+
base.RunWithReturn(success);
256+
257+
RaiseOnStart();
258+
259+
var webRequest = WebRequest.Create(Url);
260+
webRequest.Method = "GET";
261+
webRequest.Timeout = 3000;
262+
263+
using (var webResponse = (HttpWebResponse) webRequest.GetResponseWithoutException())
264+
{
265+
var webResponseCharacterSet = webResponse.CharacterSet ?? Encoding.UTF8.BodyName;
266+
var encoding = Encoding.GetEncoding(webResponseCharacterSet);
267+
268+
using (var responseStream = webResponse.GetResponseStream())
269+
using (var reader = new StreamReader(responseStream, encoding))
270+
return reader.ReadToEnd();
271+
}
272+
}
273+
274+
protected virtual void UpdateProgress(float progress)
275+
{
276+
Progress = progress;
277+
}
278+
279+
protected string Url { get; }
280+
}
239281
}

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@ public async Task TestDownloadTask()
4949

5050
downloadResult.MD5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
5151
}
52+
53+
[Test]
54+
public void TestDownloadTextTask()
55+
{
56+
InitializeTaskManager();
57+
58+
var downloadTask = new DownloadTextTask(CancellationToken.None, "https://github.com/robots.txt");
59+
var result = downloadTask.Start().Result;
60+
var resultLines = result.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
61+
resultLines[0].Should().Be("# If you would like to crawl GitHub contact us at [email protected].");
62+
}
5263
}
5364
}

0 commit comments

Comments
 (0)