-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add Chuck Norris Jokes (#157)
* feature: add Chuck Norris Jokes * changing dotnet 6 version
- Loading branch information
1 parent
fe4eaa2
commit b92f360
Showing
19 changed files
with
458 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reactive.Linq; | ||
using DynamicData; | ||
|
||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
internal static class CacheFunctions | ||
{ | ||
/// <summary> | ||
/// Caches the list of <see cref="T"/>. | ||
/// </summary> | ||
/// <param name="result">The result.</param> | ||
/// <param name="cache">The cache.</param> | ||
/// <param name="clearCache">A value determining whether to clear the cache.</param> | ||
/// <returns>A completion notification.</returns> | ||
public static IObservable<IChangeSet<T, string>> Cache<T>(this IObservable<IEnumerable<T>> result, SourceCache<T, string> cache, bool clearCache) | ||
where T : IHaveIdentifier<string> => result | ||
.Do(UpdateCache(cache, clearCache)) | ||
.Select(_ => cache.Connect().RefCount()) | ||
.Switch(); | ||
|
||
/// <summary> | ||
/// Caches the list of <see cref="T"/>. | ||
/// </summary> | ||
/// <param name="result">The result.</param> | ||
/// <param name="cache">The cache.</param> | ||
/// <returns>A completion notification.</returns> | ||
public static IObservable<T> Cache<T>(this IObservable<T> result, SourceCache<T, string> cache) | ||
where T : IHaveIdentifier<string> => result | ||
.Do(x => Update(x, cache)); | ||
|
||
private static void Update<T>(T item, ISourceCache<T, string> cache) | ||
where T : IHaveIdentifier<string> => cache.AddOrUpdate(item); | ||
|
||
private static Action<IEnumerable<T>> UpdateCache<T>(ISourceCache<T, string> cache, bool clearCache) | ||
where T : IHaveIdentifier<string> => results => | ||
{ | ||
if (clearCache) | ||
{ | ||
cache | ||
.Edit(updater => | ||
{ | ||
updater.Clear(); | ||
updater.AddOrUpdate(results); | ||
}); | ||
} | ||
else | ||
{ | ||
cache.EditDiff(results, (first, second) => first.Id == second.Id); | ||
} | ||
}; | ||
} | ||
} |
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,14 @@ | ||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
/// <summary> | ||
/// Interface representing an entity with an identifier. | ||
/// </summary> | ||
/// <typeparam name="T">The identifier type.</typeparam> | ||
public interface IHaveIdentifier<T> | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier. | ||
/// </summary> | ||
T Id { get; set; } | ||
} | ||
} |
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,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
/// <summary> | ||
/// Represents a joke from https://api.chucknorris.io/. | ||
/// </summary> | ||
public class ChuckNorrisJoke : IHaveIdentifier<string> | ||
{ | ||
/// <summary> | ||
/// Gets or sets the id. | ||
/// </summary> | ||
[JsonProperty("id")] | ||
public string Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the icon url. | ||
/// </summary> | ||
[JsonProperty("icon_url")] | ||
public Uri IconUrl { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the url. | ||
/// </summary> | ||
[JsonProperty("url")] | ||
public Uri Url { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value. | ||
/// </summary> | ||
[JsonProperty("value")] | ||
public string Value { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the categories. | ||
/// </summary> | ||
[JsonProperty("categories")] | ||
public IEnumerable<string> Categories { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date of creation. | ||
/// </summary> | ||
[JsonProperty("created_at")] | ||
public DateTimeOffset CreatedAt { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the last updated date. | ||
/// </summary> | ||
[JsonProperty("updated_at")] | ||
public DateTimeOffset UpdatedAt { get; set; } | ||
} | ||
} |
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,61 @@ | ||
using System; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using DynamicData; | ||
|
||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
/// <summary> | ||
/// Represents a service that produces <see cref="ChuckNorrisJoke"/>. | ||
/// </summary> | ||
public class ChuckNorrisJokeService : IChuckNorrisJokeService | ||
{ | ||
private readonly IChuckNorrisJokeApiClient _chuckNorrisJokeApiClient; | ||
private readonly SourceCache<ChuckNorrisJoke, string> _jokes = new SourceCache<ChuckNorrisJoke, string>(x => x.Id); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ChuckNorrisJokeService"/> class. | ||
/// </summary> | ||
/// <param name="chuckNorrisJokeApiClient">The api client.</param> | ||
public ChuckNorrisJokeService(IChuckNorrisJokeApiClient chuckNorrisJokeApiClient) => _chuckNorrisJokeApiClient = chuckNorrisJokeApiClient; | ||
|
||
/// <inheritdoc/> | ||
public IObservable<ChuckNorrisJoke> Random() => Observable | ||
.Create<ChuckNorrisJoke>(observer => | ||
_chuckNorrisJokeApiClient | ||
.Random() | ||
.Cache(_jokes) | ||
.Subscribe(observer)); | ||
|
||
/// <inheritdoc/> | ||
public IObservable<ChuckNorrisJoke> Random(params string[] categories) => Observable.Create<ChuckNorrisJoke>( | ||
observer => | ||
{ | ||
var disposable = new CompositeDisposable(); | ||
|
||
foreach (var category in categories) | ||
{ | ||
_chuckNorrisJokeApiClient | ||
.RandomFromCategory(category) | ||
.SelectMany(jokes => jokes) | ||
.Cache(_jokes) | ||
.Subscribe(observer) | ||
.DisposeWith(disposable); | ||
} | ||
|
||
return disposable; | ||
}); | ||
|
||
/// <inheritdoc/> | ||
public IObservable<IChangeSet<ChuckNorrisJoke, string>> Query(string query) => Query(query, true); | ||
|
||
/// <inheritdoc/> | ||
public IObservable<IChangeSet<ChuckNorrisJoke, string>> Query(string query, bool clearCache) => Observable | ||
.Create<IChangeSet<ChuckNorrisJoke, string>>( | ||
observer => | ||
_chuckNorrisJokeApiClient | ||
.Search(query) | ||
.Cache(_jokes, clearCache) | ||
.Subscribe(observer)); | ||
} | ||
} |
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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Refit; | ||
|
||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
/// <summary> | ||
/// Interface that defines an api client for <c>https://api.chucknorris.io/</c>. | ||
/// </summary> | ||
/// <remarks>https://api.chucknorris.io</remarks> | ||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1629:Documentation text should end with a period", Justification = "Url")] | ||
public interface IChuckNorrisJokeApiClient | ||
{ | ||
/// <summary> | ||
/// Gets a random <see cref="ChuckNorrisJoke"/>. | ||
/// </summary> | ||
/// <returns>An <see cref="IObservable{T}"/> representing the result of the asynchronous operation.</returns> | ||
[Get("/jokes/random")] | ||
IObservable<ChuckNorrisJoke> Random(); | ||
|
||
/// <summary> | ||
/// Gets a random <see cref="ChuckNorrisJoke"/>. | ||
/// </summary> | ||
/// <param name="category">The category.</param> | ||
/// <returns>An <see cref="IObservable{T}"/> representing the result of the asynchronous operation.</returns> | ||
[Get("/jokes/random?category={category}")] | ||
IObservable<IEnumerable<ChuckNorrisJoke>> RandomFromCategory(string category); | ||
|
||
/// <summary> | ||
/// Gets a random <see cref="ChuckNorrisJoke"/>. | ||
/// </summary> | ||
/// <returns>An <see cref="IObservable{T}"/> representing the result of the asynchronous operation.</returns> | ||
[Get("/jokes/categories")] | ||
IObservable<IEnumerable<string>> Categories(); | ||
|
||
/// <summary> | ||
/// Gets a random <see cref="ChuckNorrisJoke"/>. | ||
/// </summary> | ||
/// <param name="query">The query.</param> | ||
/// <returns>An <see cref="IObservable{T}"/> representing the result of the asynchronous operation.</returns> | ||
[Get("/jokes/search?query={query}")] | ||
IObservable<IEnumerable<ChuckNorrisJoke>> Search(string query); | ||
} | ||
} |
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,39 @@ | ||
using System; | ||
using DynamicData; | ||
|
||
namespace Rocket.Surgery.Airframe.Data | ||
{ | ||
/// <summary> | ||
/// Interface representing a <see cref="ChuckNorrisJoke"/> service. | ||
/// </summary> | ||
public interface IChuckNorrisJokeService | ||
{ | ||
/// <summary> | ||
/// Gets a random chuck norris joke. | ||
/// </summary> | ||
/// <returns>An observable of chang sets.</returns> | ||
IObservable<ChuckNorrisJoke> Random(); | ||
|
||
/// <summary> | ||
/// Gets a random chuck norris joke from the provided categories. | ||
/// </summary> | ||
/// <param name="categories">The categories.</param> | ||
/// <returns>An observable of chang sets.</returns> | ||
IObservable<ChuckNorrisJoke> Random(params string[] categories); | ||
|
||
/// <summary> | ||
/// Queries chuck norris jokes. | ||
/// </summary> | ||
/// <param name="query">The query.</param> | ||
/// <returns>An observable of chang sets.</returns> | ||
IObservable<IChangeSet<ChuckNorrisJoke, string>> Query(string query); | ||
|
||
/// <summary> | ||
/// Queries chuck norris jokes. | ||
/// </summary> | ||
/// <param name="query">The query.</param> | ||
/// <param name="clearCache">A value indicating whether to clear the cache.</param> | ||
/// <returns>An observable of chang sets.</returns> | ||
IObservable<IChangeSet<ChuckNorrisJoke, string>> Query(string query, bool clearCache); | ||
} | ||
} |
Oops, something went wrong.