HttpObservable 1.2.1
dotnet add package HttpObservable --version 1.2.1
NuGet\Install-Package HttpObservable -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HttpObservable" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpObservable" Version="1.2.1" />
<PackageReference Include="HttpObservable" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HttpObservable --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HttpObservable, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HttpObservable@1.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HttpObservable&version=1.2.1
#tool nuget:?package=HttpObservable&version=1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HttpObservable
Implement Observable httpClient in C#
✨Get Started
- Any Blazor hosting model - Server, WebAssembly, or .NET MAUI
- .NET 8 or later
Installation
- Add the package HttpObservable. If you're using the command line, that's:
dotnet add package HttpObservable
Usage
- your services can inherit the HttpObservable, HttpObservable class has provide the following methods.
- public IAsyncObservable<TDto> PostAsJson<TDto, TPayload>(string url, TPayload data);
- public IAsyncObservable<TDto> PostContent<TDto>(string url, HttpContent content);
- public IAsyncObservable<TDto> PutAsJson<TDto, TPayload>(string url, TPayload data);
- public IAsyncObservable<TDto> Delete<TDto>(string url);
- public IAsyncObservable<TDto> Get<TDto>(string url);
- your main method in program, as we httpclient we used in HttpObservable, SampleService is our one of implementation of httpObservable.
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(ApiEnd.BaseUrl) });
builder.Services.AddScoped<SampleService>();
await builder.Build().RunAsync();
}
}
- In our blazor page, you can use it like this.
@page "/token"
@using HttpObservable.Models
@using HttpObservable.Samples.Services
@inject SampleService sampleService;
<h3>TokenPage</h3>
<p>@token</p>
<button class="btn btn-primary" @onclick="getToken">GetToken</button>
@code {
private string token = string.Empty;
private async void getToken(MouseEventArgs e)
{
var request = new { username = "jack.chen", password = "abcdsd23e23" };
await sampleService!.SigninAsync(request.username, request.password)
.SubscribeAsync(OnSuccess, OnError);
}
private void OnSuccess(ApiResponse<string> result)
{
if (!result.Succeeded)
{
Console.WriteLine(result.Error!.Message);
return;
}
token = result.Data!;
Console.Write(result);
StateHasChanged();
}
private void OnError(Exception exception)
{
var error = exception.Message;
Console.WriteLine(exception.ToString());
}
}
🌈 Online Examples
- please refer to the project in our project. "HttpObservable Sample"
- we provide the ApiResponse<T>, ApiError, and PagedList<T>
ApiResponse<string> is the response you got from your webapi, just like this. for sure, you can use your own customized response.
public class ApiResponse<TData>
{
public int Code { get; set; }
public bool Succeeded { get; set; }
public TData? Data { get; set; }
public ApiError? Error { get; set; }
public long Timestamp { get; set; } = DateTime.UtcNow.Ticks;
}
// here is my sample, I got the response from our webapi.
{
"code": 0,
"succeeded": true,
"data": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiJlMmVhZjAwYWNlM2M0OTE2OWQ1MmEzNzQ3YjdhNjM4ZiIsIlVzZXJuYW1lIjoiaGVucnkuemhhbmciLCJFbWFpbCI6IkhlbnJ5LlpoYW5nQHdlLW9ubGluZS5jb20iLCJEaXNwbGF5TmFtZSI6IlpoYW5nLCBIZW5yeSAoUVMpIiwiVGl0bGUiOiJUZWFtIGxlYWRlciIsIkRlcGFydG1lbnQiOiJRdWFsaXR5IERlc2lnbiBDZW50ZXIgQXNpYSBBcHBsaWNhdGlvbiBEZXNpZ24iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOlsiVXNlciIsIlB1cmNoYXNlciIsIlByb2N1cmVtZW50IE1hbmFnZXIiLCJGaW5hbmNlIE1hbmFnZXIiLCJGaW5hbmNlIiwiQWRtaW4iLCJBcHByb3ZlciIsIkRldmVsb3BlciJdLCJleHAiOjE3MzU2NTgzMTIsImlzcyI6IldFIiwiYXVkIjoiV0UifQ.IdSesRJLRPkt9Pl8yyJrHer1EjwtoUikT1dOsBO179g",
"error": null,
"timestamp": 638712119125196300
}
Finally
- if you meet any question, please feel free contact me.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.Extensions.Http (>= 6.0.1)
- System.Reactive (>= 6.0.0)
- System.Reactive.Async (>= 6.0.0-alpha.18)
- System.Text.Json (>= 6.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.