-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2092 from sbwalker/dev
remote service support via Jwt
- Loading branch information
Showing
20 changed files
with
272 additions
and
80 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
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,147 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Net.Http.Headers; | ||
using Oqtane.Shared; | ||
|
||
namespace Oqtane.Services | ||
{ | ||
public class RemoteServiceBase | ||
{ | ||
private readonly SiteState _siteState; | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
protected RemoteServiceBase(IHttpClientFactory httpClientFactory, SiteState siteState) | ||
{ | ||
_siteState = siteState; | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
private HttpClient GetHttpClient() | ||
{ | ||
var httpClient = _httpClientFactory.CreateClient("Remote"); | ||
if (!httpClient.DefaultRequestHeaders.Contains(HeaderNames.Authorization) && _siteState != null && !string.IsNullOrEmpty(_siteState.AuthorizationToken)) | ||
{ | ||
httpClient.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + _siteState.AuthorizationToken); | ||
} | ||
return httpClient; | ||
} | ||
|
||
protected async Task GetAsync(string uri) | ||
{ | ||
var response = await GetHttpClient().GetAsync(uri); | ||
CheckResponse(response); | ||
} | ||
|
||
protected async Task<string> GetStringAsync(string uri) | ||
{ | ||
try | ||
{ | ||
return await GetHttpClient().GetStringAsync(uri); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
|
||
return default; | ||
} | ||
|
||
protected async Task<byte[]> GetByteArrayAsync(string uri) | ||
{ | ||
try | ||
{ | ||
return await GetHttpClient().GetByteArrayAsync(uri); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
|
||
return default; | ||
} | ||
|
||
protected async Task<T> GetJsonAsync<T>(string uri) | ||
{ | ||
var response = await GetHttpClient().GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None); | ||
if (CheckResponse(response) && ValidateJsonContent(response.Content)) | ||
{ | ||
return await response.Content.ReadFromJsonAsync<T>(); | ||
} | ||
|
||
return default; | ||
} | ||
|
||
protected async Task PutAsync(string uri) | ||
{ | ||
var response = await GetHttpClient().PutAsync(uri, null); | ||
CheckResponse(response); | ||
} | ||
|
||
protected async Task<T> PutJsonAsync<T>(string uri, T value) | ||
{ | ||
return await PutJsonAsync<T, T>(uri, value); | ||
} | ||
|
||
protected async Task<TResult> PutJsonAsync<TValue, TResult>(string uri, TValue value) | ||
{ | ||
var response = await GetHttpClient().PutAsJsonAsync(uri, value); | ||
if (CheckResponse(response) && ValidateJsonContent(response.Content)) | ||
{ | ||
var result = await response.Content.ReadFromJsonAsync<TResult>(); | ||
return result; | ||
} | ||
return default; | ||
} | ||
|
||
protected async Task PostAsync(string uri) | ||
{ | ||
var response = await GetHttpClient().PostAsync(uri, null); | ||
CheckResponse(response); | ||
} | ||
|
||
protected async Task<T> PostJsonAsync<T>(string uri, T value) | ||
{ | ||
return await PostJsonAsync<T, T>(uri, value); | ||
} | ||
|
||
protected async Task<TResult> PostJsonAsync<TValue, TResult>(string uri, TValue value) | ||
{ | ||
var response = await GetHttpClient().PostAsJsonAsync(uri, value); | ||
if (CheckResponse(response) && ValidateJsonContent(response.Content)) | ||
{ | ||
var result = await response.Content.ReadFromJsonAsync<TResult>(); | ||
return result; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
protected async Task DeleteAsync(string uri) | ||
{ | ||
var response = await GetHttpClient().DeleteAsync(uri); | ||
CheckResponse(response); | ||
} | ||
|
||
private bool CheckResponse(HttpResponseMessage response) | ||
{ | ||
if (response.IsSuccessStatusCode) return true; | ||
if (response.StatusCode != HttpStatusCode.NoContent && response.StatusCode != HttpStatusCode.NotFound) | ||
{ | ||
Console.WriteLine($"Request: {response.RequestMessage.RequestUri}"); | ||
Console.WriteLine($"Response status: {response.StatusCode} {response.ReasonPhrase}"); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool ValidateJsonContent(HttpContent content) | ||
{ | ||
var mediaType = content?.Headers.ContentType?.MediaType; | ||
return mediaType != null && mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
Oops, something went wrong.