Skip to content

Commit

Permalink
allow disposing & severals fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandrofly committed Jun 10, 2017
1 parent fd7b6b9 commit d83e64f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
25 changes: 9 additions & 16 deletions src/Http/Request.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using System.IO;
using System.Text;
using System;
using System.Net.Http;

namespace SubDBSharp.Models
{
public class Request
{
public string MovieHash { get; set; }
public string Content { get; set; }

public Request(string movieFile, string subtitleFile)
public Request(Uri endpoint, HttpMethod method, HttpContent content)
{
if (File.Exists(movieFile))
{
throw new FileNotFoundException(movieFile);
}
if (File.Exists(subtitleFile))
{
throw new FileNotFoundException(movieFile);
}
MovieHash = Utils.GetMovieHash(movieFile);
Content = File.ReadAllText(subtitleFile, Encoding.UTF8);
EndPoint = endpoint;
Method = method;
Body = content;
}

public Uri EndPoint { get; }
public HttpMethod Method { get; }
public HttpContent Body { get; }
}
}
55 changes: 30 additions & 25 deletions src/Http/SubDBApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace SubDBSharp
{
public class SubDBApi
public class SubDBApi : IDisposable
{
public static readonly Uri SubDBApiUrl = new Uri("http://api.thesubdb.com/", UriKind.Absolute);
private readonly HttpClient _httpClient;
Expand Down Expand Up @@ -39,7 +39,7 @@ public SubDBApi(ProductHeaderValue productInformation, Uri baseAddress)
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
_httpClient = new HttpClient(_httpClientHandler);
_httpClient = new HttpClient(_httpClientHandler, true);

// we can't go with this logic because the User-Agent must contain implementor url (information)
//_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(productInformation));
Expand All @@ -50,10 +50,8 @@ public SubDBApi(ProductHeaderValue productInformation, Uri baseAddress)
public Task<Response> GetAvailableLanguagesAsync()
{
var uriBuilder = new UriBuilder(BaseAddress) { Query = "action=languages" };
using (HttpRequestMessage requestMessage = BuildRequestMessage(uriBuilder.Uri, HttpMethod.Get, null))
{
return SendDataAsync(requestMessage);
}
Request request = BuildRequest(uriBuilder.Uri, HttpMethod.Get, null);
return SendDataAsync(request);
}

/// <summary>
Expand All @@ -65,10 +63,8 @@ public Task<Response> GetAvailableLanguagesAsync()
public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
{
var fullUrl = SubDBApiUrl.ApplySearchSubtitleParameters(hash, getVersions);
using (HttpRequestMessage requestMessage = BuildRequestMessage(fullUrl, HttpMethod.Get, null))
{
return SendDataAsync(requestMessage);
}
Request request = BuildRequest(fullUrl, HttpMethod.Get, null);
return SendDataAsync(request);
}

/// <summary>
Expand All @@ -81,10 +77,8 @@ public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
public Task<Response> DownloadSubtitle(string hash, params string[] languages)
{
var fullUrl = SubDBApiUrl.ApplyDownloadSubtitleParameters(hash, languages);
using (var requestMessage = BuildRequestMessage(fullUrl, HttpMethod.Get, null))
{
return SendDataAsync(requestMessage);
}
Request request = BuildRequest(fullUrl, HttpMethod.Get, null);
return SendDataAsync(request);
}

/// <summary>
Expand All @@ -95,29 +89,27 @@ public Task<Response> DownloadSubtitle(string hash, params string[] languages)
public Task<Response> UploadSubtitle(string subtitle, string movie)
{
var uriBuilder = new UriBuilder(BaseAddress) { Query = "action=upload" };
using (HttpContent httpContent = CreateFormContent(subtitle, movie))
using (HttpRequestMessage httpRequestMessage = BuildRequestMessage(uriBuilder.Uri, HttpMethod.Post, httpContent))
{
return SendDataAsync(httpRequestMessage);
}
Request request = BuildRequest(uriBuilder.Uri, HttpMethod.Post, CreateFormContent(subtitle, movie));
return SendDataAsync(request);
}

// ALL THE METHOD MUST GO THROUGH THIS!!!
private async Task<Response> SendDataAsync(HttpRequestMessage requestMessage)
// ALL THE REQUEST MUST TO THROUGH THIS METHOD!!!
private async Task<Response> SendDataAsync(Request request)
{
using (HttpRequestMessage requestMessage = BuildRequestMessage(request))
using (HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false))
{
return await BuildResponse(responseMessage);
}
}

protected virtual HttpRequestMessage BuildRequestMessage(Uri endPoint, HttpMethod httpMethod, HttpContent content)
protected virtual HttpRequestMessage BuildRequestMessage(Request request)
{
return new HttpRequestMessage()
{
RequestUri = endPoint,
Method = httpMethod,
Content = content
RequestUri = request.EndPoint,
Method = request.Method,
Content = request.Body
};
}

Expand All @@ -137,9 +129,15 @@ protected virtual async Task<Response> BuildResponse(HttpResponseMessage respons
responseMessage.Headers.ToDictionary(h => h.Key, h => h.Value.First()));
}

protected virtual Request BuildRequest(Uri endPoint, HttpMethod method, HttpContent body)
{
return new Request(endPoint, method, body);
}

private static HttpContent CreateFormContent(string subtitle, string movie)
{
const string dispositionType = "form-data";

var content = new MultipartFormDataContent("xYzZY");

// hash info
Expand All @@ -166,5 +164,12 @@ private static HttpContent CreateFormContent(string subtitle, string movie)

return content;
}

public void Dispose()
{
// will dispose _handler aswell (mentioned in contructor)
_httpClient.Dispose();
//_httpClientHandler.Dispose();
}
}
}
6 changes: 5 additions & 1 deletion src/SubDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SubDBSharp
{
public class SubDBClient
public class SubDBClient : IDisposable
{
private readonly SubDBApi _subDbApi;
private readonly IResponseParser _responseParser;
Expand Down Expand Up @@ -75,5 +75,9 @@ public Task<Response> UploadSubtitleAsync(string subtitle, string movie)
return _subDbApi.UploadSubtitle(subtitle, movie);
}

public void Dispose()
{
_subDbApi?.Dispose();
}
}
}
5 changes: 3 additions & 2 deletions src/SubDBSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<Copyright>Copyright © 2017</Copyright>
<Copyright>Copyright © 2017 Ivandrofly</Copyright>
<PackageProjectUrl>https://github.com/nibblesoft/SubDBSharp</PackageProjectUrl>
<Description>C# implementation of http://thesubdb.com/api/</Description>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<PackageTags>subdb subtitleapi subdbapi thesubdb ivandrofly ivandro ismael</PackageTags>
<RepositoryUrl>https://github.com/nibblesoft/SubDBSharp</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System.Net.Http" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
Expand Down

0 comments on commit d83e64f

Please sign in to comment.