Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
</PropertyGroup>

<ItemGroup Label="Microsoft NuGet Packages (Source)" Condition="'$(TargetFramework)' == 'net6.0'">
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
</ItemGroup>

<ItemGroup Label="Microsoft NuGet Packages (Source)" Condition="'$(TargetFramework)' == 'net7.0'">
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
</ItemGroup>

<ItemGroup Label="Microsoft NuGet Packages (Source)" Condition="'$(TargetFramework)' == 'net8.0'">
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0-rc.1.23419.4" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

<ItemGroup Label="Microsoft NuGet Packages (Test)">
Expand Down
5 changes: 3 additions & 2 deletions samples/ClientSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// -------------------------------------------------------

using BlazorFocused.Http.Client;
using Microsoft.Extensions.DependencyInjection;

namespace ClientSample;

Expand All @@ -14,8 +15,8 @@ public static void Main(string[] args) =>

public static void Test()
{
var test = new Class1();
IServiceCollection services = new ServiceCollection();

test.SampleMethod();
services.AddWebApiClient("Program");
}
}
6 changes: 3 additions & 3 deletions src/Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Streamlines HttpClient operations, configuration, request mapping, and dependenc

## NuGet Packages

| Package | Description |
| -------------------------------------------------------------------------------------- | --------------- |
| [BlazorFocused.Http.Client](https://www.nuget.org/packages/BlazorFocused.Http.Client/) | Sample Template |
| Package | Description |
| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| [BlazorFocused.Http.Client](https://www.nuget.org/packages/BlazorFocused.Http.Client/) | Streamlines HttpClient operations, configuration, request mapping, and dependency injection |

## Documentation

Expand Down
5 changes: 5 additions & 0 deletions src/Client/src/BlazorFocused.Http.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<Description>Streamlines HttpClient operations, configuration, request mapping, and dependency injection</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>
Expand Down
31 changes: 0 additions & 31 deletions src/Client/src/Class1.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Client/src/Client/WebApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client.Client;

internal class WebApiClient : IWebApiClient
{
public WebApiClient(HttpClient httpClient)
{
this.Client = httpClient;
}

internal HttpClient Client { get; }

public IWebApiClientMethod Delete(string relativeUrl) => throw new NotImplementedException();
public IWebApiClientMethod Get(string relativeUrl) => throw new NotImplementedException();
public IWebApiClientContent Patch(string relativeUrl) => throw new NotImplementedException();
public IWebApiClientContent Post(string relativeUrl) => throw new NotImplementedException();
public IWebApiClientContent Put(string relativeUrl) => throw new NotImplementedException();
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken = default) => throw new NotImplementedException();
}
22 changes: 22 additions & 0 deletions src/Client/src/Client/WebApiOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client.Client;

internal class WebApiOptions
{
public string BaseAddress { get; set; }

public Dictionary<string, string> DefaultRequestHeaders { get; set; } = new();

public long? MaxResponseContentBufferSize { get; set; }

public double? Timeout { get; set; }

public bool IsConfigured => !string.IsNullOrEmpty(BaseAddress) ||
DefaultRequestHeaders.Count > 0 ||
MaxResponseContentBufferSize > 0 ||
Timeout > 0;
}
21 changes: 21 additions & 0 deletions src/Client/src/IWebApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client;

internal interface IWebApiClient
{
IWebApiClientMethod Delete(string relativeUrl);

IWebApiClientMethod Get(string relativeUrl);

IWebApiClientContent Patch(string relativeUrl);

IWebApiClientContent Post(string relativeUrl);

IWebApiClientContent Put(string relativeUrl);

Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken = default);
}
11 changes: 11 additions & 0 deletions src/Client/src/IWebApiClientActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client;

internal interface IWebApiClientActivator
{
IWebApiClient Create(string name);
}
15 changes: 15 additions & 0 deletions src/Client/src/IWebApiClientContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client;

internal interface IWebApiClientContent
{
IWebApiClientMethod WithRequestBody(object content);

IWebApiClientMethod WithHttpContent(HttpContent content);

IWebApiClientMethod WithNoContent();
}
26 changes: 26 additions & 0 deletions src/Client/src/IWebApiClientMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

using System.Text.Json;

namespace BlazorFocused.Http.Client;

internal interface IWebApiClientMethod
{
IWebApiClientMethod AddParameter(string key, string value);

IWebApiClientMethod AddHeader(string key, string value);

IWebApiClientMethod AddSerializationOptions(JsonSerializerOptions jsonSerializerOptions);

Task<T> ExecuteAsync<T>(CancellationToken cancellationToken = default);

Task<WebApiTaskResponse> ExecuteAsync(CancellationToken cancellationToken = default);

Task<WebApiTaskResponse<T>> TryExecuteAsync<T>(CancellationToken cancellationToken = default)
where T : class;

Task<WebApiTaskResponse> TryExecuteAsync(CancellationToken cancellationToken = default);
}
44 changes: 44 additions & 0 deletions src/Client/src/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

using BlazorFocused.Http.Client.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace BlazorFocused.Http.Client;

/// <summary>
/// Extension used to register WebApiClient Services
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Extension used to register WebApiClient Services
/// </summary>
/// <param name="services">Current service dependencies</param>
/// <param name="name">Name of web api client instance</param>
/// <returns><see cref="IHttpClientBuilder"/> to extend <see cref="HttpClient"/> properties</returns>
public static IHttpClientBuilder AddWebApiClient(this IServiceCollection services, string name)
{
services.TryAddScoped<IWebApiClient, WebApiClient>();
services.TryAddScoped<IWebApiClientActivator, WebApiClientActivator>();

// Detect/bind configurations settings
services.AddOptions<WebApiOptions>(name)
.BindConfiguration($"BlazorFocused:WebApiClient:{name}");

#if NET8_0_OR_GREATER
services.AddKeyedScoped<IWebApiClient, WebApiClient>(name, (serviceProvider, test) =>
{
IHttpClientFactory httpClientFactory =
serviceProvider.GetRequiredService<IHttpClientFactory>();

return new WebApiClient(httpClientFactory.CreateClient(name));
});
#endif

return services.AddHttpClient(name);
}
}
13 changes: 13 additions & 0 deletions src/Client/src/WebApiClientActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

namespace BlazorFocused.Http.Client;

internal class WebApiClientActivator : IWebApiClientActivator
{
public IWebApiClient Create(string name) => throw new NotImplementedException();

public static IWebApiClient Create(HttpClient httpClient) => throw new NotImplementedException();
}
33 changes: 33 additions & 0 deletions src/Client/src/WebApiTaskResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

using System.Net;
using System.Net.Http.Headers;

namespace BlazorFocused.Http.Client;

internal class WebApiTaskResponse
{
public Stream Content { get; init; }

public HttpStatusCode? StatusCode { get; set; }

public virtual bool IsSuccess => HasSuccessStatusCode();

public HttpResponseHeaders Headers { get; init; }

public string GetContentString() => new StreamReader(Content).ReadToEnd();

protected bool HasSuccessStatusCode() =>
StatusCode.HasValue &&
new HttpResponseMessage(StatusCode.Value).IsSuccessStatusCode;
}

internal class WebApiTaskResponse<T> : WebApiTaskResponse where T : class
{
public T Value { get; set; }

public override bool IsSuccess => HasSuccessStatusCode() && Value is not null;
}
24 changes: 24 additions & 0 deletions src/Client/test/ServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// -------------------------------------------------------
// Copyright (c) BlazorFocused All rights reserved.
// Licensed under the MIT License
// -------------------------------------------------------

using Microsoft.Extensions.DependencyInjection;

namespace BlazorFocused.Http.Client.Test;

public class ServiceCollectionExtensionsTests
{
[Fact]
public void SampleMethod_ShouldCallMethod()
{
IServiceCollection services = new ServiceCollection();

services.AddWebApiClient("Test");

IServiceProvider serviceProvider = services.BuildServiceProvider();

Assert.NotNull(serviceProvider.GetService<IWebApiClient>());
Assert.NotNull(serviceProvider.GetService<IWebApiClientActivator>());
}
}
32 changes: 0 additions & 32 deletions src/Client/test/UnitTest1.cs

This file was deleted.