This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Klemm
committed
Jul 26, 2017
1 parent
6b5c928
commit afa9478
Showing
34 changed files
with
2,132 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VideoLibrary | ||
{ | ||
public enum AdaptiveKind | ||
{ | ||
None, | ||
Audio, | ||
Video | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VideoLibrary | ||
{ | ||
public enum AudioFormat | ||
{ | ||
Mp3, | ||
Aac, | ||
Vorbis, | ||
Unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using VideoLibrary.Helpers; | ||
|
||
namespace VideoLibrary | ||
{ | ||
public static class Client | ||
{ | ||
public static Client<T> For<T>(ServiceBase<T> baseService) | ||
where T : Video => new Client<T>(baseService); | ||
} | ||
|
||
public class Client<T> : IService<T>, IAsyncService<T>, IDisposable | ||
where T : Video | ||
{ | ||
private bool disposed = false; | ||
private readonly ServiceBase<T> baseService; | ||
private readonly HttpClient client; | ||
|
||
private Task<string> SourceFactory(string address) => | ||
client.GetStringAsync(address); | ||
|
||
internal Client(ServiceBase<T> baseService) | ||
{ | ||
Require.NotNull(baseService, nameof(baseService)); | ||
|
||
this.baseService = baseService; | ||
this.client = baseService.MakeClient(); | ||
} | ||
|
||
#region IDisposable | ||
|
||
~Client() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposed) | ||
return; | ||
disposed = true; | ||
|
||
if (disposing) | ||
{ | ||
if (client != null) | ||
client.Dispose(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
public T GetVideo(string videoUri) => | ||
baseService.GetVideo(videoUri, SourceFactory); | ||
|
||
public IEnumerable<T> GetAllVideos(string videoUri) => | ||
baseService.GetAllVideos(videoUri, SourceFactory); | ||
|
||
public Task<T> GetVideoAsync(string videoUri) => | ||
baseService.GetVideoAsync(videoUri, SourceFactory); | ||
|
||
public Task<IEnumerable<T>> GetAllVideosAsync(string videoUri) => | ||
baseService.GetAllVideosAsync(videoUri, SourceFactory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VideoLibrary | ||
{ | ||
public class DelegatingClient : IDisposable | ||
{ | ||
private bool disposed = false; | ||
private readonly HttpClient client; | ||
|
||
public DelegatingClient() | ||
{ | ||
this.client = MakeClient(); | ||
} | ||
|
||
#region IDisposable | ||
|
||
~DelegatingClient() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposed) | ||
return; | ||
|
||
disposed = true; | ||
|
||
if (disposing) | ||
{ | ||
if (client != null) | ||
client.Dispose(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region MakeClient/MakeHandler | ||
|
||
private HttpClient MakeClient() => | ||
MakeClient(MakeHandler()); | ||
|
||
protected virtual HttpMessageHandler MakeHandler() | ||
{ | ||
var handler = new HttpClientHandler(); | ||
|
||
if (handler.SupportsAutomaticDecompression) | ||
{ | ||
handler.AutomaticDecompression = | ||
DecompressionMethods.GZip | | ||
DecompressionMethods.Deflate; | ||
} | ||
|
||
return handler; | ||
} | ||
|
||
protected virtual HttpClient MakeClient(HttpMessageHandler handler) | ||
{ | ||
return new HttpClient(handler); | ||
} | ||
|
||
#endregion | ||
|
||
#region Synchronous wrappers | ||
|
||
public HttpResponseMessage Get(string uri) => | ||
GetAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public byte[] GetByteArray(string uri) => | ||
GetByteArrayAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public Stream GetStream(string uri) => | ||
GetStreamAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public string GetString(string uri) => | ||
GetStringAsync(uri).GetAwaiter().GetResult(); | ||
|
||
#endregion | ||
|
||
#region HttpClient wrappers | ||
|
||
// TODO: Support other kinds of HTTP requests, | ||
// such as PUT, POST, DELETE, etc. | ||
|
||
public Task<HttpResponseMessage> GetAsync(string uri) => | ||
client.GetAsync(uri); | ||
|
||
public Task<byte[]> GetByteArrayAsync(string uri) => | ||
client.GetByteArrayAsync(uri); | ||
|
||
public Task<Stream> GetStreamAsync(string uri) => | ||
client.GetStreamAsync(uri); | ||
|
||
public Task<string> GetStringAsync(string uri) => | ||
client.GetStringAsync(uri); | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VideoLibrary.Exceptions | ||
{ | ||
internal class BadQueryException : Exception | ||
{ | ||
public BadQueryException() | ||
: base() | ||
{ } | ||
|
||
public BadQueryException(string message) | ||
: base(message) | ||
{ } | ||
|
||
public BadQueryException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ } | ||
} | ||
} |
Oops, something went wrong.