Skip to content

Commit

Permalink
Merge pull request #14 from lucassklp/develop
Browse files Browse the repository at this point in the history
Version 1.0.0
  • Loading branch information
lucassklp authored Nov 8, 2019
2 parents 3d49bd1 + b731726 commit af3a719
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
5 changes: 2 additions & 3 deletions Models/Consumers/TheMovieDatabaseConsumer.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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");
}
}
}
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand All @@ -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<List<Todo>>("https://jsonplaceholder.typicode.com/todos/").Subscribe(itens => {
itens.ForEach(item => Console.WriteLine(item.title));
http.Get<List<Todo>>("https://jsonplaceholder.typicode.com/todos/").Subscribe(items => {
items.ForEach(item => Console.WriteLine(item.title));
});

//Making the same request using await
Expand Down
2 changes: 1 addition & 1 deletion Rx.Http/Rx.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.7</Version>
<Version>1.0.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Lucas Simas</Authors>
<Description>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.</Description>
Expand Down
14 changes: 7 additions & 7 deletions Rx.Http/RxConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,53 @@ protected IObservable<TResponse> Get<TResponse>(string url, Action<RxHttpRequest
return request.Request<TResponse>();
}

public IObservable<string> Get(string url, Action<RxHttpRequestOptions> opts = null)
protected IObservable<string> Get(string url, Action<RxHttpRequestOptions> opts = null)
{
var request = http.CreateGetRequest(url, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request();
}


public IObservable<TResponse> Post<TResponse>(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
protected IObservable<TResponse> Post<TResponse>(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
where TResponse : class
{
var request = http.CreatePostRequest(url, obj, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request<TResponse>();
}

public IObservable<string> Post(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
protected IObservable<string> Post(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
{
var request = http.CreatePostRequest(url, obj, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request();
}

public IObservable<TResponse> Put<TResponse>(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
protected IObservable<TResponse> Put<TResponse>(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
where TResponse : class
{
var request = http.CreatePutRequest(url, obj, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request<TResponse>();
}

public IObservable<string> Put(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
protected IObservable<string> Put(string url, object obj = null, Action<RxHttpRequestOptions> opts = null)
{
var request = http.CreatePutRequest(url, obj, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request();
}

public IObservable<TResponse> Delete<TResponse>(string url, Action<RxHttpRequestOptions> opts = null)
protected IObservable<TResponse> Delete<TResponse>(string url, Action<RxHttpRequestOptions> opts = null)
where TResponse : class
{
var request = http.CreateDeleteRequest(url, opts);
interceptors.ForEach(x => x.Intercept(request));
return request.Request<TResponse>();
}

public IObservable<string> Delete(string url, Action<RxHttpRequestOptions> opts = null)
protected IObservable<string> Delete(string url, Action<RxHttpRequestOptions> opts = null)
{
var request = http.CreateDeleteRequest(url, opts);
interceptors.ForEach(x => x.Intercept(request));
Expand Down
3 changes: 1 addition & 2 deletions Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,6 +42,5 @@ private static void ConfigureServices(ServiceCollection services)
})
.AddTransient<Application>();
}

}
}
32 changes: 30 additions & 2 deletions Tests/ConsumersTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
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<TheMovieDatabaseConsumer>();
var response = await tmdbConsumer.ListMovies();

Assert.NotNull(response);
}

[Fact]
public async void CheckSameRequestTwice()
{
var tmdbConsumer = this.injector.Get<TheMovieDatabaseConsumer>();
var response1 = await tmdbConsumer.ListMovies();
var response2 = await tmdbConsumer.ListMovies();

Assert.NotNull(response1);
Assert.NotNull(response2);
}

}
}
34 changes: 34 additions & 0 deletions Tests/InjectionTests.cs
Original file line number Diff line number Diff line change
@@ -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<TheMovieDatabaseConsumer>();

Assert.NotNull(tmdbConsumer);
}

[Fact]
public void CheckIfRxHttpClientIsInjected()
{
var http = this.injector.Get<RxHttpClient>();

Assert.NotNull(http);
}
}
}
35 changes: 35 additions & 0 deletions Tests/Injector.cs
Original file line number Diff line number Diff line change
@@ -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<RxHttpClient>();

services.AddConsumer<TheMovieDatabaseConsumer>(http =>
{
http.BaseAddress = new Uri(@"https://api.themoviedb.org/3/");
})
.AddTransient<ConsumersTests>();
}

public T Get<T>()
{
return this.serviceProvider.GetService<T>();
}

}
}
3 changes: 2 additions & 1 deletion Tests/RequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Models;
using Models.Postman;
using Rx.Http;
using Rx.Http.Exceptions;
using Rx.Http.MediaTypes;
using System.Collections.Generic;
Expand All @@ -8,7 +9,7 @@
using System.Reactive.Linq;
using Xunit;

namespace Rx.Http.Tests
namespace Tests
{
public class RequestTests
{
Expand Down
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit af3a719

Please sign in to comment.