Skip to content

Commit

Permalink
Merge pull request #15 from JSkimming/release-connections
Browse files Browse the repository at this point in the history
Correctly dispose objects to release HTTP connections
  • Loading branch information
JSkimming committed Aug 28, 2015
2 parents 9c3719a + 0e74d65 commit fa94f83
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 54 deletions.
18 changes: 2 additions & 16 deletions src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,17 @@ public static class HttpClientDnsRecordExtensions
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static async Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
this HttpClient client,
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records");
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<IReadOnlyList<DnsRecord>>(cancellationToken)
.ConfigureAwait(false))
.Result;
return client.GetAsync<IReadOnlyList<DnsRecord>>(uri, auth, cancellationToken);
}
}
}
39 changes: 34 additions & 5 deletions src/CloudFlare.NET/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
/// <seealso href="https://api.cloudflare.com"/>
public static class HttpClientExtensions
{
/// <summary>
/// Gets the base address of the CloudFlare API.
/// </summary>
public static Uri ZonesUri { get; } = new Uri(CloudFlareConstants.BaseUri, "zones");

/// <summary>
/// Gets the <see cref="CloudFlareResponse{T}"/> of a CloudFlare API <paramref name="response"/>.
/// </summary>
Expand Down Expand Up @@ -60,5 +55,39 @@ public static void AddAuth(this HttpRequestMessage request, CloudFlareAuth auth)
request.Headers.Add("X-Auth-Key", auth.Key);
request.Headers.Add("X-Auth-Email", auth.Email);
}

/// <summary>
/// Executes a <see cref="HttpMethod.Get"/> request returning the type specified by <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the <see cref="CloudFlareResponse{T}.Result"/>.</typeparam>
public static async Task<T> GetAsync<T>(
this HttpClient client,
Uri uri,
CloudFlareAuth auth,
CancellationToken cancellationToken)
where T : class
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (uri == null)
throw new ArgumentNullException(nameof(uri));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
{
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
using (response)
{
CloudFlareResponse<T> cloudFlareResponse =
await response.GetResultAsync<T>(cancellationToken).ConfigureAwait(false);
return cloudFlareResponse.Result;
}
}
}
}
}
37 changes: 4 additions & 33 deletions src/CloudFlare.NET/HttpClientZoneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,36 @@ public static class HttpClientZoneExtensions
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-list-zones"/>
public static async Task<IReadOnlyList<Zone>> GetZonesAsync(
public static Task<IReadOnlyList<Zone>> GetZonesAsync(
this HttpClient client,
CancellationToken cancellationToken,
CloudFlareAuth auth,
PagedZoneParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, "zones");
if (parameters != null)
{
uri = new UriBuilder(uri) { Query = parameters.ToQuery() }.Uri;
}

var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<IReadOnlyList<Zone>>(cancellationToken)
.ConfigureAwait(false))
.Result;
return client.GetAsync<IReadOnlyList<Zone>>(uri, auth, cancellationToken);
}

/// <summary>
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-zone-details"/>
public static async Task<Zone> GetZoneAsync(
public static Task<Zone> GetZoneAsync(
this HttpClient client,
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}");
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<Zone>(cancellationToken)
.ConfigureAwait(false))
.Result;
return client.GetAsync<Zone>(uri, auth, cancellationToken);
}
}
}

0 comments on commit fa94f83

Please sign in to comment.