/// <summary>Creates a <see cref="System.Uri"/> which is used to request the authorization code.</summary> public Uri Build() { var builder = new RequestBuilder() { BaseUri = AuthorizationServerUrl }; ParameterUtils.InitParameters(builder, this); return builder.BuildUri(); }
public System.Uri Build() { var builder = new RequestBuilder() { BaseUri = new System.Uri("//revokeTokenUrl") }; ParameterUtils.InitParameters(builder, this); return builder.BuildUri(); }
/// <summary>Creates a <see cref="System.Uri"/> which is used to request the authorization code.</summary> public Uri Build() { var builder = new RequestBuilder() { BaseUri = revokeTokenUrl }; ParameterUtils.InitParameters(builder, this); return builder.BuildUri(); }
public void TestBasicWebRequest() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/") }; var request = builder.CreateRequest(); Assert.That(request.Method, Is.EqualTo(HttpMethod.Get)); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/"))); }
public void TestSingleQueryParameter() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), }; builder.AddParameter(RequestParameterType.Query, "testQueryParam", "testValue"); var request = builder.CreateRequest(); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/?testQueryParam=testValue"))); }
public void TestBasePlusPathMath() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), Path = "test/path", Method = "PUT" }; var request = builder.CreateRequest(); Assert.That(request.Method, Is.EqualTo(HttpMethod.Put)); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/test/path"))); }
/// <summary> /// The core download logic. It downloads the media in parts, where each part's size is defined by /// <see cref="ChunkSize"/> (in bytes). /// </summary> /// <param name="url">The URL of the resource to download.</param> /// <param name="stream">The download will download the resource into this stream.</param> /// <param name="cancellationToken">A cancellation token to cancel this download in the middle.</param> /// <returns>A task with the download progress object. If an exception occurred during the download, its /// <see cref="IDownloadProgress.Exception "/> property will contain the exception.</returns> private async Task<IDownloadProgress> DownloadCoreAsync(string url, Stream stream, CancellationToken cancellationToken) { url.ThrowIfNull("url"); stream.ThrowIfNull("stream"); if (!stream.CanWrite) { throw new ArgumentException("stream doesn't support write operations"); } RequestBuilder builder = null; var uri = new Uri(url); if (string.IsNullOrEmpty(uri.Query)) { builder = new RequestBuilder() { BaseUri = new Uri(url) }; } else { builder = new RequestBuilder() { BaseUri = new Uri(url.Substring(0, url.IndexOf("?"))) }; // Remove '?' at the beginning. var query = uri.Query.Substring(1); var pairs = from parameter in query.Split('&') select parameter.Split('='); // Add each query parameter. each pair contains the key [0] and then its value [1]. foreach (var p in pairs) { builder.AddParameter(RequestParameterType.Query, p[0], p[1]); } } builder.AddParameter(RequestParameterType.Query, "alt", "media"); long currentRequestFirstBytePos = 0; try { // This "infinite" loop stops when the "to" byte position in the "Content-Range" header is the last // byte of the media ("length"-1 in the "Content-Range" header). // e.g. "Content-Range: 200-299/300" - "to"(299) = "length"(300) - 1. while (true) { var currentRequestLastBytePos = currentRequestFirstBytePos + ChunkSize - 1; // Create the request and set the Range header. var request = builder.CreateRequest(); request.Headers.Range = new RangeHeaderValue(currentRequestFirstBytePos, currentRequestLastBytePos); using (var response = await service.HttpClient.SendAsync(request, cancellationToken). ConfigureAwait(false)) { // Read the content and copy to the parameter's stream. var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); responseStream.CopyTo(stream); // Read the headers and check if all the media content was already downloaded. var contentRange = response.Content.Headers.ContentRange; long mediaContentLength; if (contentRange == null) { // Content range is null when the server doesn't adhere the media download protocol, in // that case we got all the media in one chunk. currentRequestFirstBytePos = mediaContentLength = response.Content.Headers.ContentLength.Value; } else { currentRequestFirstBytePos = contentRange.To.Value + 1; mediaContentLength = contentRange.Length.Value; } if (currentRequestFirstBytePos == mediaContentLength) { var progress = new DownloadProgress(DownloadStatus.Completed, mediaContentLength); UpdateProgress(progress); return progress; } } UpdateProgress(new DownloadProgress(DownloadStatus.Downloading, currentRequestFirstBytePos)); } } catch (TaskCanceledException ex) { Logger.Error(ex, "Download media was canceled"); UpdateProgress(new DownloadProgress(ex, currentRequestFirstBytePos)); throw ex; } catch (Exception ex) { Logger.Error(ex, "Exception occurred while downloading media"); var progress = new DownloadProgress(ex, currentRequestFirstBytePos); UpdateProgress(progress); return progress; } }
/// <summary>A subtest for path parameters.</summary> /// <param name="dic">Dictionary that contain all path parameters.</param> /// <param name="path">Path part.</param> /// <param name="expected">Expected part after base URI.</param> private void SubtestPathParameters(IDictionary<string, IEnumerable<string>> dic, string path, string expected) { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com"), Path = path, }; foreach (var pair in dic) { foreach (var value in pair.Value) { builder.AddParameter(RequestParameterType.Path, pair.Key, value); } } Assert.That(builder.BuildUri().AbsoluteUri, Is.EqualTo("http://www.example.com/" + expected)); }
public void TestQueryAndPathParameters() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.test.com"), Path = "colors{?list}" }; builder.AddParameter(RequestParameterType.Path, "list", "red"); builder.AddParameter(RequestParameterType.Path, "list", "yellow"); builder.AddParameter(RequestParameterType.Query, "on", "1"); Assert.That(builder.BuildUri().AbsoluteUri, Is.EqualTo("http://www.test.com/colors?list=red,yellow&on=1")); }
public void TestPathParameterWithUrlEncode() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), Path = "test/{id}", }; builder.AddParameter(RequestParameterType.Path, "id", " %va/ue"); var request = builder.CreateRequest(); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/test/%20%25va%2Fue"))); }
public void TestInvalidPathParameters() { // A path parameter is missing. var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), Path = "test/{id}/foo/bar", }; Assert.Throws<ArgumentException>(() => { builder.CreateRequest(); }); // Invalid number after ':' in path parameter. builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), Path = "test/{id:SHOULD_BE_NUMBER}/foo/bar", }; builder.AddParameter(RequestParameterType.Path, "id", "hello"); Assert.Throws<ArgumentException>(() => { builder.CreateRequest(); }); }
public void TestPathParameter() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), Path = "test/{id}/foo/bar", }; builder.AddParameter(RequestParameterType.Path, "id", "value"); var request = builder.CreateRequest(); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/test/value/foo/bar"))); }
public void TestNullQueryParameters() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com"), Path = "", }; builder.AddParameter(RequestParameterType.Query, "q", null); builder.AddParameter(RequestParameterType.Query, "p", String.Empty); Assert.That(builder.BuildUri().AbsoluteUri, Is.EqualTo("http://www.example.com/?p")); }
public void TestMultipleQueryParameters() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), }; builder.AddParameter(RequestParameterType.Query, "q", "value1"); builder.AddParameter(RequestParameterType.Query, "q", "value2"); var request = builder.CreateRequest(); Assert.That(request.RequestUri, Is.EqualTo(new Uri("http://www.example.com/?q=value1&q=value2"))); }
public void TestQueryParameterWithUrlEncode() { var builder = new RequestBuilder() { BaseUri = new Uri("http://www.example.com/"), }; builder.AddParameter(RequestParameterType.Query, "test Query Param", "test %va/ue"); var request = builder.CreateRequest(); Assert.That(request.RequestUri, Is.EqualTo( new Uri("http://www.example.com/?test%20Query%20Param=test%20%25va%2Fue"))); }
public GoogleTopicsImageFinder(int w, int h) { width = w; height = h; requestBuilder = new RequestBuilder(); }