Skip to content

Commit

Permalink
Merge pull request #15 from lucassklp/develop
Browse files Browse the repository at this point in the history
Versão 1.2.0
  • Loading branch information
lucassklp authored Dec 25, 2019
2 parents af3a719 + 1c004a4 commit f3c1231
Show file tree
Hide file tree
Showing 35 changed files with 723 additions and 254 deletions.
15 changes: 15 additions & 0 deletions Models/Consumers/GoogleConsumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Rx.Http;
using System;

namespace Models.Consumers
{
public class GoogleConsumer : RxConsumer
{
public GoogleConsumer(IConsumerConfiguration<GoogleConsumer> configuration) : base(configuration)
{

}

public IObservable<string> GetGoogleContent() => Get("");
}
}
17 changes: 17 additions & 0 deletions Models/Consumers/JsonPlaceHolderConsumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Rx.Http;
using System;
using System.Collections.Generic;

namespace Models.Consumers
{
public class JsonPlaceHolderConsumer : RxConsumer
{
public JsonPlaceHolderConsumer(IConsumerConfiguration<JsonPlaceHolderConsumer> configuration) : base(configuration)
{

}

public IObservable<List<Todo>> GetTodos() => Get<List<Todo>>("todos");
public IObservable<Identifiable> SendPost(Post post) => Post<Identifiable>("posts", post);
}
}
9 changes: 4 additions & 5 deletions Models/Consumers/TheMovieDatabaseConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Rx.Http;
using Rx.Http.Interceptors;
using Rx.Http.Requests;
using System;

namespace Models.Consumers
Expand All @@ -9,17 +8,17 @@ public class TheMovieDatabaseConsumer : RxConsumer
{
public TheMovieDatabaseConsumer(IConsumerConfiguration<TheMovieDatabaseConsumer> config) : base(config)
{
config.AddInterceptors(new TheMovieDatabaseInterceptor());
config.RequestInterceptors.Add(new TheMovieDatabaseInterceptor());
}

public IObservable<Result> ListMovies() => Get<Result>("movie/popular");
}

public class TheMovieDatabaseInterceptor : RxInterceptor
public class TheMovieDatabaseInterceptor : RxRequestInterceptor
{
public void Intercept(RxHttpRequest request)
public void Intercept(RxHttpRequestOptions request)
{
request.QueryStrings.Add("api_key", "eb7b25db28349bd4eef1498a5be9842f");
request.AddQueryString("api_key", "eb7b25db28349bd4eef1498a5be9842f");
}
}
}
2 changes: 1 addition & 1 deletion Models/Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion Models/Post.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
namespace Models
using Newtonsoft.Json;

namespace Models
{
public class Post
{
[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("body")]
public string Body { get; set; }

[JsonProperty("userId")]
public int UserId { get; set; }

}
Expand Down
25 changes: 25 additions & 0 deletions Models/Postman/PostResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Models.Postman
{
public class PostResponse : PostResponse<string>
{

}

public class PostResponse<T>
{
[JsonProperty("args")]
public Dictionary<string, string> Args { get; set; }

[JsonProperty("data")]
public T Data { get; set; }

[JsonProperty("form")]
public Dictionary<string, string> Form { get; set; }

[JsonProperty("headers")]
public Dictionary<string, string> Headers { get; set; }
}
}
28 changes: 23 additions & 5 deletions Models/Todo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
using Newtonsoft.Json;
using System;

namespace Models
{
public class Todo
public class Todo : IEquatable<Todo>
{
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public bool completed { get; set; }
[JsonProperty("userId")]
public int UserId { get; set; }

[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("completed")]
public bool IsCompleted { get; set; }

public bool Equals(Todo other)
{
return this.Id == other.Id &&
this.IsCompleted == other.IsCompleted &&
this.Title == other.Title &&
this.UserId == other.UserId;
}
}
}
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,35 @@
</a>
<img alt="npm version" src="https://img.shields.io/nuget/v/Rx.Http.svg">


<!-- snyk does not support .NET Core yet -->
<!-- https://github.com/snyk/snyk/issues/489 -->
<!-- <a href="https://snyk.io//test/github/lucassklp/Rx.Http?targetFile=Rx.Http/Rx.Http.csproj"><img src="https://snyk.io//test/github/lucassklp/Rx.Http/badge.svg?targetFile=Rx.Http/Rx.Http.csproj" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io//test/github/lucassklp/Rx.Http?targetFile=Rx.Http/Rx.Http.csproj" style="max-width:100%;"></a> -->

</p>

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,

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.

# Installation

If you are using Package Manager:

```bash
Install-Package Rx.Http -Version 1.0.0
Install-Package Rx.Http -Version 1.2.0
```

If you are using .NET CLI

```bash
dotnet add package Rx.Http --version 1.0.0
dotnet add package Rx.Http --version 1.2.0
```


## Example of use

```csharp
using Rx.Http;
using System.Reactive.Linq;

public class Program
public class Program
{
public static async void Main()
{
Expand All @@ -66,10 +63,16 @@ You can customize your request by using options. It make possible you set **quer
```csharp
http.Get<List<Todo>>("https://jsonplaceholder.typicode.com/todos/", options =>
{
options.RequestMediaType = new JsonHttpMediaType();
options.ResponseMediaType = new JsonHttpMediaType();
options.AddHeader("Authorization", "Bearer <token>");
options.QueryStrings.Add("name", "John Doe");
options.SetRequestMediaType(new JsonHttpMediaType())
.SetResponseMediaType(new JsonHttpMediaType())
.AddHeader(new {
Authorization = "Bearer <token>"
Accept = "application/json"
})
.AddQueryString(new {
name = "John Doe",
index = 1
});
});
```

Expand Down Expand Up @@ -101,7 +104,7 @@ The code above shows how to use Consumers and Interceptors.
{
public TheMovieDatabaseConsumer(IConsumerConfiguration<TheMovieDatabaseConsumer> configuration): base(configuration)
{
configuration.Interceptors.Add(new TheMovieDatabaseInterceptor());
configuration.AddRequestInterceptors(new TheMovieDatabaseInterceptor());
}

public IObservable<Result> ListMovies() => Get<Result>("movie/popular");
Expand All @@ -111,12 +114,13 @@ The code above shows how to use Consumers and Interceptors.
{
public void Intercept(RxHttpRequest request)
{
request.QueryStrings.Add("api_key", "key");
request.AddQueryString("api_key", "key");
}
}
```

## RxHttpRequestException

This exception is threw when the server reply with a HTTP Status different of 2xx. There's two ways to handle this exception:

```csharp
Expand All @@ -134,10 +138,10 @@ This exception is threw when the server reply with a HTTP Status different of 2x
}

//Or using reactive way
http.Get<List<Todo>>(url).Subscribe(response =>
http.Get<List<Todo>>(url).Subscribe(response =>
{
//...
}, exception =>
}, exception =>
{
HttpResponseMessage response = (exception as RxHttpRequestException)?.Response;
//...
Expand All @@ -162,6 +166,19 @@ public void ConfigureServices(ServiceCollection services)
}
```

## Logging

You can implement your own custom logging mechanism by implementing the interface RxHttpLogging.
We provide a built-in logging mechanism called "RxHttpDefaultLogging".

Here is a example that show how to use RxHttpDefaultLogging mechanism. If you have a custom logging mechanism you must replace RxHttpDefaultLogging for your class implementation.

```csharp
private static void ConfigureServices(ServiceCollection services)
{
services.AddRxHttpLogging<RxHttpDefaultLogging>();
}
```

### Roadmap

Expand Down
19 changes: 9 additions & 10 deletions Rx.Http/ConsumerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ namespace Rx.Http
{
public class ConsumerProvider<T> : IConsumerConfiguration<T>
{
public List<RxInterceptor> Interceptors { get; set; }
public ConsumerProvider(HttpClient http)
{
Interceptors = new List<RxInterceptor>();
Http = http;
}
public List<RxRequestInterceptor> RequestInterceptors { get; private set; }
public List<RxResponseInterceptor> ResponseInterceptors { get; private set; }
public HttpClient Http { get; private set; }
public RxHttpLogging Logger { get; set; }

public void AddInterceptors(params RxInterceptor[] interceptors)
public ConsumerProvider(HttpClient http, RxHttpLogging logger = null)
{
Interceptors.AddRange(interceptors);
RequestInterceptors = new List<RxRequestInterceptor>();
ResponseInterceptors = new List<RxResponseInterceptor>();
Http = http;
Logger = logger;
}

public HttpClient Http { get; private set; }
}
}
12 changes: 12 additions & 0 deletions Rx.Http/Exceptions/RxInvalidHeaderParameterException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Rx.Http.Exceptions
{
public class RxInvalidHeaderParameterException : Exception
{
public RxInvalidHeaderParameterException() : base("The header key or value provided are invalid for http request.")
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Collections.Generic;
using System.Net.Http;

namespace Rx.Http
namespace Rx.Http.Extensions
{
public static class RxHttpClientFactoryExtension
{
Expand Down
21 changes: 21 additions & 0 deletions Rx.Http/Extensions/RxHttpLoggingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if NETSTANDARD2_0

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Rx.Http.Extensions
{
public static class RxHttpLoggingExtension
{
public static IServiceCollection AddRxHttpLogging<TLogging>(this IServiceCollection services)
where TLogging : RxHttpLogging
{
services.AddScoped<RxHttpLogging, TLogging>();
return services;
}
}
}

#endif
6 changes: 4 additions & 2 deletions Rx.Http/IConsumerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ namespace Rx.Http
{
public interface IConsumerConfiguration<out T>
{
List<RxInterceptor> Interceptors { get; set; }
void AddInterceptors(params RxInterceptor[] interceptors);
List<RxRequestInterceptor> RequestInterceptors { get; }
List<RxResponseInterceptor> ResponseInterceptors { get; }
HttpClient Http { get; }
RxHttpLogging Logger { get; }

}
}
9 changes: 0 additions & 9 deletions Rx.Http/Interceptors/RxInterceptor.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Rx.Http/Interceptors/RxRequestInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Rx.Http.Interceptors
{
public interface RxRequestInterceptor
{
void Intercept(RxHttpRequestOptions request);
}
}
9 changes: 9 additions & 0 deletions Rx.Http/Interceptors/RxResponseInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Net.Http;

namespace Rx.Http.Interceptors
{
public interface RxResponseInterceptor
{
void Intercept(HttpResponseMessage response);
}
}
Loading

0 comments on commit f3c1231

Please sign in to comment.