From 9614f41bb1464355f87047616839acd8e8f4159a Mon Sep 17 00:00:00 2001 From: Lucas Simas Date: Sun, 22 Sep 2019 13:08:29 -0300 Subject: [PATCH 1/3] More tests implemented + Moved namespace + modify method access --- Models/Consumers/TheMovieDatabaseConsumer.cs | 5 ++- Rx.Http/RxConsumer.cs | 14 ++++---- Samples/Program.cs | 3 +- Tests/ConsumersTests.cs | 21 ++++++++++-- Tests/InjectionTests.cs | 34 +++++++++++++++++++ Tests/Injector.cs | 35 ++++++++++++++++++++ Tests/RequestTests.cs | 3 +- Tests/Tests.csproj | 1 + 8 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 Tests/InjectionTests.cs create mode 100644 Tests/Injector.cs diff --git a/Models/Consumers/TheMovieDatabaseConsumer.cs b/Models/Consumers/TheMovieDatabaseConsumer.cs index 2ade255..c596147 100644 --- a/Models/Consumers/TheMovieDatabaseConsumer.cs +++ b/Models/Consumers/TheMovieDatabaseConsumer.cs @@ -1,10 +1,9 @@ -using Models; using Rx.Http; using Rx.Http.Interceptors; using Rx.Http.Requests; using System; -namespace Samples.Consumers +namespace Models.Consumers { public class TheMovieDatabaseConsumer : RxConsumer { @@ -20,7 +19,7 @@ public class TheMovieDatabaseInterceptor : RxInterceptor { public void Intercept(RxHttpRequest request) { - request.QueryStrings.Add("api_key", "key"); + request.QueryStrings.Add("api_key", "eb7b25db28349bd4eef1498a5be9842f"); } } } \ No newline at end of file diff --git a/Rx.Http/RxConsumer.cs b/Rx.Http/RxConsumer.cs index d923d46..04b8025 100644 --- a/Rx.Http/RxConsumer.cs +++ b/Rx.Http/RxConsumer.cs @@ -25,7 +25,7 @@ protected IObservable Get(string url, Action(); } - public IObservable Get(string url, Action opts = null) + protected IObservable Get(string url, Action opts = null) { var request = http.CreateGetRequest(url, opts); interceptors.ForEach(x => x.Intercept(request)); @@ -33,7 +33,7 @@ public IObservable Get(string url, Action opts = n } - public IObservable Post(string url, object obj = null, Action opts = null) + protected IObservable Post(string url, object obj = null, Action opts = null) where TResponse : class { var request = http.CreatePostRequest(url, obj, opts); @@ -41,14 +41,14 @@ public IObservable Post(string url, object obj = null, Act return request.Request(); } - public IObservable Post(string url, object obj = null, Action opts = null) + protected IObservable Post(string url, object obj = null, Action opts = null) { var request = http.CreatePostRequest(url, obj, opts); interceptors.ForEach(x => x.Intercept(request)); return request.Request(); } - public IObservable Put(string url, object obj = null, Action opts = null) + protected IObservable Put(string url, object obj = null, Action opts = null) where TResponse : class { var request = http.CreatePutRequest(url, obj, opts); @@ -56,14 +56,14 @@ public IObservable Put(string url, object obj = null, Acti return request.Request(); } - public IObservable Put(string url, object obj = null, Action opts = null) + protected IObservable Put(string url, object obj = null, Action opts = null) { var request = http.CreatePutRequest(url, obj, opts); interceptors.ForEach(x => x.Intercept(request)); return request.Request(); } - public IObservable Delete(string url, Action opts = null) + protected IObservable Delete(string url, Action opts = null) where TResponse : class { var request = http.CreateDeleteRequest(url, opts); @@ -71,7 +71,7 @@ public IObservable Delete(string url, Action(); } - public IObservable Delete(string url, Action opts = null) + protected IObservable Delete(string url, Action opts = null) { var request = http.CreateDeleteRequest(url, opts); interceptors.ForEach(x => x.Intercept(request)); diff --git a/Samples/Program.cs b/Samples/Program.cs index 34e719d..7b1750c 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; using Rx.Http; -using Samples.Consumers; +using Models.Consumers; using System; using System.Threading.Tasks; @@ -42,6 +42,5 @@ private static void ConfigureServices(ServiceCollection services) }) .AddTransient(); } - } } \ No newline at end of file diff --git a/Tests/ConsumersTests.cs b/Tests/ConsumersTests.cs index 2e7ab9c..1570141 100644 --- a/Tests/ConsumersTests.cs +++ b/Tests/ConsumersTests.cs @@ -1,8 +1,25 @@ -namespace Rx.Http.Tests +using Models.Consumers; +using System.Reactive.Linq; +using Xunit; + +namespace Tests { - internal class ConsumersTests + public class ConsumersTests { + private Injector injector; + public ConsumersTests() + { + injector = new Injector(); + } + + [Fact] + public async void CheckIfRequestIsWorking() + { + var tmdbConsumer = this.injector.Get(); + var response = await tmdbConsumer.ListMovies(); + Assert.NotNull(response); + } } } diff --git a/Tests/InjectionTests.cs b/Tests/InjectionTests.cs new file mode 100644 index 0000000..ebeb131 --- /dev/null +++ b/Tests/InjectionTests.cs @@ -0,0 +1,34 @@ +using Models.Consumers; +using Rx.Http; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace Tests +{ + public class InjectionTests + { + private Injector injector; + public InjectionTests() + { + injector = new Injector(); + } + + [Fact] + public void CheckIfConsumerIsInjected() + { + var tmdbConsumer = this.injector.Get(); + + Assert.NotNull(tmdbConsumer); + } + + [Fact] + public void CheckIfRxHttpClientIsInjected() + { + var http = this.injector.Get(); + + Assert.NotNull(http); + } + } +} diff --git a/Tests/Injector.cs b/Tests/Injector.cs new file mode 100644 index 0000000..776b34d --- /dev/null +++ b/Tests/Injector.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.DependencyInjection; +using Models.Consumers; +using Rx.Http; +using System; + +namespace Tests +{ + class Injector + { + private ServiceProvider serviceProvider; + public Injector() + { + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); + serviceProvider = serviceCollection.BuildServiceProvider(); + } + + private void ConfigureServices(ServiceCollection services) + { + services.AddHttpClient(); + + services.AddConsumer(http => + { + http.BaseAddress = new Uri(@"https://api.themoviedb.org/3/"); + }) + .AddTransient(); + } + + public T Get() + { + return this.serviceProvider.GetService(); + } + + } +} diff --git a/Tests/RequestTests.cs b/Tests/RequestTests.cs index 207ac50..fbe18b9 100644 --- a/Tests/RequestTests.cs +++ b/Tests/RequestTests.cs @@ -1,5 +1,6 @@ using Models; using Models.Postman; +using Rx.Http; using Rx.Http.Exceptions; using Rx.Http.MediaTypes; using System.Collections.Generic; @@ -8,7 +9,7 @@ using System.Reactive.Linq; using Xunit; -namespace Rx.Http.Tests +namespace Tests { public class RequestTests { diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index f6e50cb..b68d3d3 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -14,6 +14,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + From 73afe01d0b382db203617838b04780acdc0128cd Mon Sep 17 00:00:00 2001 From: Lucas Simas Date: Mon, 23 Sep 2019 18:53:59 -0300 Subject: [PATCH 2/3] Update ConsumersTests.cs --- Tests/ConsumersTests.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/ConsumersTests.cs b/Tests/ConsumersTests.cs index 1570141..78bdca4 100644 --- a/Tests/ConsumersTests.cs +++ b/Tests/ConsumersTests.cs @@ -21,5 +21,16 @@ public async void CheckIfRequestIsWorking() Assert.NotNull(response); } + [Fact] + public async void CheckSameRequestTwice() + { + var tmdbConsumer = this.injector.Get(); + var response1 = await tmdbConsumer.ListMovies(); + var response2 = await tmdbConsumer.ListMovies(); + + Assert.NotNull(response1); + Assert.NotNull(response2); + } + } } From b731726616cedd0ee92405fdb8c0c9438e65b696 Mon Sep 17 00:00:00 2001 From: Lucas Simas Date: Fri, 8 Nov 2019 10:21:34 -0300 Subject: [PATCH 3/3] Version 1.0.0 --- README.md | 11 +++++------ Rx.Http/Rx.Http.csproj | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0fa5ed4..b18647b 100644 --- a/README.md +++ b/README.md @@ -17,20 +17,19 @@ A lightweight library that is inpired in [Angular 2+ Http Client](https://angular.io/guide/http) built on top of [.NET Http Client](https://docs.microsoft.com/pt-br/dotnet/api/system.net.http.httpclient) that help programmers to make asynchronous http requests, -**Note: This project is under development and is NOT recommend to use in production environment yet.** # Installation If you are using Package Manager: ```bash -Install-Package Rx.Http -Version 0.9.7 +Install-Package Rx.Http -Version 1.0.0 ``` If you are using .NET CLI ```bash -dotnet add package Rx.Http --version 0.9.7 +dotnet add package Rx.Http --version 1.0.0 ``` @@ -45,11 +44,11 @@ public class Program public static async void Main() { //Initialize the RxHttpClient - var http = new RxHttpClient(new HttpClient()) + var http = new RxHttpClient(new HttpClient()); //Retrieve a list of To-Do item and print the title of each element asynchronously - http.Get>("https://jsonplaceholder.typicode.com/todos/").Subscribe(itens => { - itens.ForEach(item => Console.WriteLine(item.title)); + http.Get>("https://jsonplaceholder.typicode.com/todos/").Subscribe(items => { + items.ForEach(item => Console.WriteLine(item.title)); }); //Making the same request using await diff --git a/Rx.Http/Rx.Http.csproj b/Rx.Http/Rx.Http.csproj index 55ce30a..09d3c5e 100644 --- a/Rx.Http/Rx.Http.csproj +++ b/Rx.Http/Rx.Http.csproj @@ -2,7 +2,7 @@ netstandard2.0;net45 - 0.9.7 + 1.0.0 true Lucas Simas A lightweight library that is inpired in Angular Http Client built on top of .NET HttpClient that and help programmers to make asynchronous http requests.