Skip to content

blazor rest is a library for sending http requests in Blazor WebAssembly in the simplest way.

Notifications You must be signed in to change notification settings

MAghazade/BlazorRest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlazorRest

blazor rest is a library for sending http requests in Blazor WebAssembly in the simplest way. blazorrest also gives you the ability to intercept requests, response, and errors that you can customize them. You will no longer have to serialize or deserialize data or even add an auhtorization header.

Installing

To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file

<PackageReference Include="BlazorRest" Version="x.x.x" />

You can also install via the .NET CLI with the following command:

dotnet add package BlazorRest

Setup

In the program.cs file, you can register in the simplest way as follows

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
});

If it is registered above, you can only send http requests without any other functionality But to send Bearer tokens automatically, you can implement the ‍‍‍‍IJwtService interface

public interface IJwtService
{
   ValueTask SetTokenAsync(string? jwtToken);

   ValueTask<string?> GetTokenAsync();

   ValueTask SetRefereshTokenAsync(string? refreshToken);

   ValueTask<string?> GetRefereshTokenAsync();
}

And register the subclass that it has implemented IJwtTokenService interface as follows at the time of registration

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
    opt.UseJwtService<JwtService>();//you can implement IJwtService and use like this
});

implement the ‍‍‍‍‍‍IRequestInterceptor interface To intercept requests.

public interface IRequestInterceptor
{       
   HttpRequestMessage InterceptRequest(HttpRequestMessage request);
}

implement the ‍‍‍‍‍‍IResponseInterceptor interface To intercept response.

public interface IResponseInterceptor
{        
  HttpResponseMessage InterceptResponse(HttpResponseMessage response);
}

implement the ‍‍‍‍‍‍IErrorInterceptor interface For Intercept requests, their status code not equal to 200.

public interface IErrorInterceptor
{ 
   Task InterceptError(ErrorInterceptorModel? error);
}

The most complete way to register blazorrest is as follows.

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
    opt.UseJwtService<JwtService>(); //you can implement IJwtService and use like this
    opt.UseRequestInterceptor<RequestInterceptor>();
    opt.UseResponseInterceptor<ResponseInterceptor>();
    opt.UseErrorInterceptor<ErrorInterceptor>();   
});

sample

login

public class AccountService : IAccountService
{
   private readonly IBlazorRest _blazorRest;
   private readonly IJwtService _jwt;
     
   public AccountService(IJwtService jwt,IBlazorRest blazorRest)
   {
      _jwt = jwt;      
     _blazorRest = blazorRest;
   }

   public async Task Login(UserLoginDto loginDto)
   {
      var message =new BlazorRestMessage("/auth/login", HttpMethod.Post)
      {     
         Content = new MA.BlazorRest.Src.RequestContents.JsonContent(loginDto)   
      };
         
      var result = await _blazorRest.SendAsync<LoginResponse>(message);

      if (!result.IsSuccessful)
      {
         //do somthing
      }    
           
       
      await _jwt.SetTokenAsync(result.Data.Token);           
  }
}

UploadFile

public async Task UploadAvatarAsync(IBrowserFile ProfileImage)
{
   var message = new BlazorRestMessage("/user/avatar", HttpMethod.Post)
   { 
      Content = new FileContent(ProfileImage, "FileNameInFormData")
   };
    
   var result = await _blazorRest.SendAsync(message);
}

upload file with model

public async Task EditUserAsync(EditProfileDto editUserDto, IBrowserFile ProfileImage)
{
    var message = new BlazorRestMessage("/user", HttpMethod.Post)
    {
       Content = new FileWithModelContent(ProfileImage, nameof(ProfileImage), editUserDto)    
    };
    
    var result = await _blazorRest.SendAsync<EditProfileResponse>(message);
    //or
    //var result = await _blazorRest.SendAsync(message);
}

Simple Get Request

public async Task<IEnumerable<WeatherForecast>> Get()
{
    var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast");
    
    if (!result.IsSuccessful)
    {
      //do somthing
    }    
      
    return result.Data;    
}

Get Request with Response Options

public async Task<IEnumerable<WeatherForecast>> Get()
{
     var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast", new ResponseOptions
            {
                SerializerOptions = new System.Text.Json.JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = false
                }
            });
    
    if (!result.IsSuccessful)
    {
      //do somthing
    }    
      
    return result.Data;    
}

About

blazor rest is a library for sending http requests in Blazor WebAssembly in the simplest way.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages