-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
1 parent
4cf252f
commit 45edbcf
Showing
10 changed files
with
420 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using JoySoftware.HomeAssistant.NetDaemon.Common; | ||
|
||
namespace JoySoftware.HomeAssistant.NetDaemon.Daemon | ||
{ | ||
public class HttpHandler : IHttpHandler | ||
{ | ||
private readonly IHttpClientFactory? _httpClientFactory; | ||
|
||
public HttpHandler(IHttpClientFactory? httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
public HttpClient CreateHttpClient(string? name = null) | ||
{ | ||
_ = _httpClientFactory ?? throw new NullReferenceException("No IHttpClientFactory provided, please add AddHttpClient() in configure services!"); | ||
return _httpClientFactory.CreateClient(name); | ||
} | ||
|
||
public async Task<T> GetJson<T>(string url, JsonSerializerOptions? options = null, params (string, object)[] headers) | ||
{ | ||
_ = _httpClientFactory ?? throw new NullReferenceException("No IHttpClientFactory provided, please add AddHttpClient() in configure services!"); | ||
|
||
var httpClient = _httpClientFactory.CreateClient(); | ||
|
||
AddHeaders(httpClient, headers); | ||
|
||
var streamTask = httpClient.GetStreamAsync(url); | ||
|
||
return await JsonSerializer.DeserializeAsync<T>(await streamTask.ConfigureAwait(false), options); | ||
} | ||
|
||
public async Task<T> PostJson<T>(string url, object request, JsonSerializerOptions? options = null, params (string, object)[] headers) | ||
{ | ||
_ = _httpClientFactory ?? throw new NullReferenceException("No IHttpClientFactory provided, please add AddHttpClient() in configure services!"); | ||
|
||
var httpClient = _httpClientFactory.CreateClient(); | ||
|
||
AddHeaders(httpClient, headers); | ||
|
||
var bytesToPost = JsonSerializer.SerializeToUtf8Bytes(request, request.GetType(), options); | ||
|
||
var response = await httpClient.PostAsync(url, new ByteArrayContent(bytesToPost)); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
var streamTask = response.Content.ReadAsStreamAsync(); | ||
return await JsonSerializer.DeserializeAsync<T>(await streamTask.ConfigureAwait(false)); | ||
} | ||
|
||
public async Task PostJson(string url, object request, JsonSerializerOptions? options = null, params (string, object)[] headers) | ||
{ | ||
_ = _httpClientFactory ?? throw new NullReferenceException("No IHttpClientFactory provided, please add AddHttpClient() in configure services!"); | ||
|
||
var httpClient = _httpClientFactory.CreateClient(); | ||
|
||
AddHeaders(httpClient, headers); | ||
|
||
var bytesToPost = JsonSerializer.SerializeToUtf8Bytes(request, request.GetType(), options); | ||
|
||
var response = await httpClient.PostAsync(url, new ByteArrayContent(bytesToPost)); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
|
||
private void AddHeaders(HttpClient httpClient, (string, object)[] headers) | ||
{ | ||
if (headers is object && headers.Length > 0) | ||
{ | ||
httpClient.DefaultRequestHeaders.Clear(); | ||
foreach (var (name, header) in headers) | ||
{ | ||
if (header is string) | ||
httpClient.DefaultRequestHeaders.Add(name, (string)header); | ||
else if (header is IEnumerable<string>) | ||
httpClient.DefaultRequestHeaders.Add(name, (IEnumerable<string>)header); | ||
else | ||
throw new ApplicationException($"Unsupported header, expected string or IEnumerable<string> for {name}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.