| using ProtoBuf; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace GrpcShared.DTO | |||||
| { | |||||
| [ProtoContract] | |||||
| public class HeaderParams | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public string Token { get; set; } | |||||
| } | |||||
| } |
| [ProtoContract] | [ProtoContract] | ||||
| public class SearchRequest | public class SearchRequest | ||||
| { | { | ||||
| [ProtoMember(1)] | [ProtoMember(1)] | ||||
| public string Query { get; set; } | public string Query { get; set; } | ||||
| [ProtoMember(5)] | [ProtoMember(5)] | ||||
| public int Offset { get; set; } | public int Offset { get; set; } | ||||
| [ProtoMember(6)] | |||||
| public string Token { get; set; } | |||||
| } | } | ||||
| } | } |
| @page "/fetchdata" | |||||
| @inject HttpClient Http | |||||
| @page "/search" | |||||
| @using GrpcShared.DTO.Search | |||||
| @using NemAnBlazor.Services.Interfaces | |||||
| @*@inject HttpClient Http*@ | |||||
| @inject Blazored.SessionStorage.ISessionStorageService sessionStorage | |||||
| @inject ISearchClientService SearchService | |||||
| <PageTitle>Weather forecast</PageTitle> | |||||
| <h1>Weather forecast</h1> | |||||
| <PageTitle>Search</PageTitle> | |||||
| <p>This component demonstrates fetching data from the server.</p> | |||||
| <h1>Search</h1> | |||||
| @if (forecasts == null) | |||||
| { | |||||
| <p><em>Loading...</em></p> | |||||
| } | |||||
| else | |||||
| { | |||||
| <table class="table"> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>Date</th> | |||||
| <th>Temp. (C)</th> | |||||
| <th>Temp. (F)</th> | |||||
| <th>Summary</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| @foreach (var forecast in forecasts) | |||||
| { | |||||
| <tr> | |||||
| <td>@forecast.Date.ToShortDateString()</td> | |||||
| <td>@forecast.TemperatureC</td> | |||||
| <td>@forecast.TemperatureF</td> | |||||
| <td>@forecast.Summary</td> | |||||
| </tr> | |||||
| } | |||||
| </tbody> | |||||
| </table> | |||||
| } | |||||
| @code { | @code { | ||||
| private WeatherForecast[]? forecasts; | |||||
| protected override async Task OnInitializedAsync() | protected override async Task OnInitializedAsync() | ||||
| { | { | ||||
| forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json"); | |||||
| } | |||||
| public class WeatherForecast | |||||
| { | |||||
| public DateTime Date { get; set; } | |||||
| var token = await sessionStorage.GetItemAsync<string>("token"); | |||||
| public int TemperatureC { get; set; } | |||||
| SearchRequest request = new() { Query = "aitch", Type = "track", Token = token }; | |||||
| SearchResponse searchResponse = await SearchService.GetListSearchAsync(request); | |||||
| } | |||||
| public string? Summary { get; set; } | |||||
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |||||
| } | |||||
| } | } |
| @using GrpcShared.DTO.Search | @using GrpcShared.DTO.Search | ||||
| @using NemAnBlazor.Services.Interfaces | @using NemAnBlazor.Services.Interfaces | ||||
| @inject NavigationManager NavigationManager | @inject NavigationManager NavigationManager | ||||
| @*@inject IAuthClientService AuthService*@ | |||||
| @inject IAuthClientService AuthService | |||||
| @inject ISearchClientService SearchService | @inject ISearchClientService SearchService | ||||
| <PageTitle>Index</PageTitle> | <PageTitle>Index</PageTitle> | ||||
| { | { | ||||
| private readonly IHttpClientFactory _httpClientFactory; | private readonly IHttpClientFactory _httpClientFactory; | ||||
| public SearchService(IHttpClientFactory httpClientFactory) | public SearchService(IHttpClientFactory httpClientFactory) | ||||
| { | { | ||||
| _httpClientFactory = httpClientFactory; | _httpClientFactory = httpClientFactory; | ||||
| public async Task<SearchResponse> ListSearchAsync(SearchRequest request) | public async Task<SearchResponse> ListSearchAsync(SearchRequest request) | ||||
| { | { | ||||
| var client = _httpClientFactory.CreateClient("HttpClient"); | var client = _httpClientFactory.CreateClient("HttpClient"); | ||||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer BQBG6iAJDhs8YGMsPG-Fj_nDP_6IrP6WIFtKJO2H_dBsEzSggWiIE9UQlEq9csweFIJTvoixP-OaWqQdI_OzW8zxsHjj-Hbsp9lKGV0EjbzsoezhMpa3Ee5D61akQGSRtZtO2L795kGHnfAtOcNhhtcayU9PsFsSlJbz3xbLACBPBzCk49DRRccDKeZUVgs"); | |||||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + request.Token ); | |||||
| var searchResult = await client.GetAsync($"search?q={request.Query}&type={request.Type}"); | var searchResult = await client.GetAsync($"search?q={request.Query}&type={request.Type}"); | ||||