Skip to content

Commit d83e64f

Browse files
committed
allow disposing & severals fixes
1 parent fd7b6b9 commit d83e64f

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

src/Http/Request.cs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
using System.IO;
2-
using System.Text;
1+
using System;
2+
using System.Net.Http;
33

44
namespace SubDBSharp.Models
55
{
66
public class Request
77
{
8-
public string MovieHash { get; set; }
9-
public string Content { get; set; }
10-
11-
public Request(string movieFile, string subtitleFile)
8+
public Request(Uri endpoint, HttpMethod method, HttpContent content)
129
{
13-
if (File.Exists(movieFile))
14-
{
15-
throw new FileNotFoundException(movieFile);
16-
}
17-
if (File.Exists(subtitleFile))
18-
{
19-
throw new FileNotFoundException(movieFile);
20-
}
21-
MovieHash = Utils.GetMovieHash(movieFile);
22-
Content = File.ReadAllText(subtitleFile, Encoding.UTF8);
10+
EndPoint = endpoint;
11+
Method = method;
12+
Body = content;
2313
}
2414

15+
public Uri EndPoint { get; }
16+
public HttpMethod Method { get; }
17+
public HttpContent Body { get; }
2518
}
2619
}

src/Http/SubDBApi.cs

+30-25
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace SubDBSharp
1212
{
13-
public class SubDBApi
13+
public class SubDBApi : IDisposable
1414
{
1515
public static readonly Uri SubDBApiUrl = new Uri("http://api.thesubdb.com/", UriKind.Absolute);
1616
private readonly HttpClient _httpClient;
@@ -39,7 +39,7 @@ public SubDBApi(ProductHeaderValue productInformation, Uri baseAddress)
3939
{
4040
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
4141
};
42-
_httpClient = new HttpClient(_httpClientHandler);
42+
_httpClient = new HttpClient(_httpClientHandler, true);
4343

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

5957
/// <summary>
@@ -65,10 +63,8 @@ public Task<Response> GetAvailableLanguagesAsync()
6563
public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
6664
{
6765
var fullUrl = SubDBApiUrl.ApplySearchSubtitleParameters(hash, getVersions);
68-
using (HttpRequestMessage requestMessage = BuildRequestMessage(fullUrl, HttpMethod.Get, null))
69-
{
70-
return SendDataAsync(requestMessage);
71-
}
66+
Request request = BuildRequest(fullUrl, HttpMethod.Get, null);
67+
return SendDataAsync(request);
7268
}
7369

7470
/// <summary>
@@ -81,10 +77,8 @@ public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
8177
public Task<Response> DownloadSubtitle(string hash, params string[] languages)
8278
{
8379
var fullUrl = SubDBApiUrl.ApplyDownloadSubtitleParameters(hash, languages);
84-
using (var requestMessage = BuildRequestMessage(fullUrl, HttpMethod.Get, null))
85-
{
86-
return SendDataAsync(requestMessage);
87-
}
80+
Request request = BuildRequest(fullUrl, HttpMethod.Get, null);
81+
return SendDataAsync(request);
8882
}
8983

9084
/// <summary>
@@ -95,29 +89,27 @@ public Task<Response> DownloadSubtitle(string hash, params string[] languages)
9589
public Task<Response> UploadSubtitle(string subtitle, string movie)
9690
{
9791
var uriBuilder = new UriBuilder(BaseAddress) { Query = "action=upload" };
98-
using (HttpContent httpContent = CreateFormContent(subtitle, movie))
99-
using (HttpRequestMessage httpRequestMessage = BuildRequestMessage(uriBuilder.Uri, HttpMethod.Post, httpContent))
100-
{
101-
return SendDataAsync(httpRequestMessage);
102-
}
92+
Request request = BuildRequest(uriBuilder.Uri, HttpMethod.Post, CreateFormContent(subtitle, movie));
93+
return SendDataAsync(request);
10394
}
10495

105-
// ALL THE METHOD MUST GO THROUGH THIS!!!
106-
private async Task<Response> SendDataAsync(HttpRequestMessage requestMessage)
96+
// ALL THE REQUEST MUST TO THROUGH THIS METHOD!!!
97+
private async Task<Response> SendDataAsync(Request request)
10798
{
99+
using (HttpRequestMessage requestMessage = BuildRequestMessage(request))
108100
using (HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false))
109101
{
110102
return await BuildResponse(responseMessage);
111103
}
112104
}
113105

114-
protected virtual HttpRequestMessage BuildRequestMessage(Uri endPoint, HttpMethod httpMethod, HttpContent content)
106+
protected virtual HttpRequestMessage BuildRequestMessage(Request request)
115107
{
116108
return new HttpRequestMessage()
117109
{
118-
RequestUri = endPoint,
119-
Method = httpMethod,
120-
Content = content
110+
RequestUri = request.EndPoint,
111+
Method = request.Method,
112+
Content = request.Body
121113
};
122114
}
123115

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

132+
protected virtual Request BuildRequest(Uri endPoint, HttpMethod method, HttpContent body)
133+
{
134+
return new Request(endPoint, method, body);
135+
}
136+
140137
private static HttpContent CreateFormContent(string subtitle, string movie)
141138
{
142139
const string dispositionType = "form-data";
140+
143141
var content = new MultipartFormDataContent("xYzZY");
144142

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

167165
return content;
168166
}
167+
168+
public void Dispose()
169+
{
170+
// will dispose _handler aswell (mentioned in contructor)
171+
_httpClient.Dispose();
172+
//_httpClientHandler.Dispose();
173+
}
169174
}
170175
}

src/SubDBClient.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace SubDBSharp
88
{
9-
public class SubDBClient
9+
public class SubDBClient : IDisposable
1010
{
1111
private readonly SubDBApi _subDbApi;
1212
private readonly IResponseParser _responseParser;
@@ -75,5 +75,9 @@ public Task<Response> UploadSubtitleAsync(string subtitle, string movie)
7575
return _subDbApi.UploadSubtitle(subtitle, movie);
7676
}
7777

78+
public void Dispose()
79+
{
80+
_subDbApi?.Dispose();
81+
}
7882
}
7983
}

src/SubDBSharp.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard1.4</TargetFramework>
5-
<Copyright>Copyright © 2017</Copyright>
5+
<Copyright>Copyright © 2017 Ivandrofly</Copyright>
66
<PackageProjectUrl>https://github.com/nibblesoft/SubDBSharp</PackageProjectUrl>
77
<Description>C# implementation of http://thesubdb.com/api/</Description>
8-
<Version>1.0.1</Version>
8+
<Version>1.0.2</Version>
99
<PackageTags>subdb subtitleapi subdbapi thesubdb ivandrofly ivandro ismael</PackageTags>
1010
<RepositoryUrl>https://github.com/nibblesoft/SubDBSharp</RepositoryUrl>
1111
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1212
</PropertyGroup>
1313

1414
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
15+
<Reference Include="System.Net.Http" />
1516
<Reference Include="System" />
1617
<Reference Include="Microsoft.CSharp" />
1718
</ItemGroup>

0 commit comments

Comments
 (0)