Преглед изворни кода

Connect SearchService(SpotifyService) with IdentityProvider; get success response.

tags/v1.1.0^2
nemanja.grkovic пре 3 година
родитељ
комит
57545084aa

+ 9
- 13
GrpcShared/DTO/Search/SearchResponse.cs Прегледај датотеку

[ProtoContract] [ProtoContract]
public class SearchResponse public class SearchResponse
{ {
[ProtoMember(1)]
public Tracks? Tracks { get; set; }
}
[ProtoContract] [ProtoContract]
public partial class SearchContracts
{
[ProtoMember(1)]
public Tracks? Tracks { get; set; }
}
[ProtoContract]
public partial class Tracks
public class Tracks
{ {
[ProtoMember(1)] [ProtoMember(1)]


public List<Item>? Items { get; set; } public List<Item>? Items { get; set; }
} }
[ProtoContract] [ProtoContract]
public partial class Item
public class Item
{ {
[ProtoMember(1)] [ProtoMember(1)]


public string Uri { get; set; } public string Uri { get; set; }
} }
[ProtoContract] [ProtoContract]
public partial class Album
public class Album
{ {
[ProtoMember(1)] [ProtoMember(1)]


public string Uri { get; set; } public string Uri { get; set; }
} }
[ProtoContract] [ProtoContract]
public partial class Image
public class Image
{ {
[ProtoMember(1)] [ProtoMember(1)]


public long Width { get; set; } public long Width { get; set; }
} }
[ProtoContract] [ProtoContract]
public partial class Artist
public class Artist
{ {
[ProtoMember(1)] [ProtoMember(1)]


public string Uri { get; set; } public string Uri { get; set; }
} }
[ProtoContract] [ProtoContract]
public partial class ExternalUrls
public class ExternalUrls
{ {
[ProtoMember(1)] [ProtoMember(1)]


public Uri Spotify { get; set; } public Uri Spotify { get; set; }
} }
}
} }

+ 1
- 0
IdentityProvider/IdentityProvider.csproj Прегледај датотеку

</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<ProjectReference Include="..\gRPCServer\SpotifyService.csproj" />
<ProjectReference Include="..\GrpcShared\GrpcShared.csproj" /> <ProjectReference Include="..\GrpcShared\GrpcShared.csproj" />
<ProjectReference Include="..\NemAnCore\NemAnBlazor.csproj" /> <ProjectReference Include="..\NemAnCore\NemAnBlazor.csproj" />
</ItemGroup> </ItemGroup>

+ 8
- 1
IdentityProvider/Program.cs Прегледај датотеку

using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using ProtoBuf.Grpc.Server; using ProtoBuf.Grpc.Server;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using SpotifyService.Services;


var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
#if DEBUG #if DEBUG
}); });


#endif #endif

builder.Services.AddHttpClient("HttpClient", c =>
{
c.BaseAddress = new Uri(SpotifyService.GLOBALS.SPOTIFYURL);
c.DefaultRequestHeaders.Add("Accept", SpotifyService.GLOBALS.MEDIATYPE);
});
builder.Services.AddOptions(); builder.Services.AddOptions();
// Additional configuration is required to successfully run gRPC on macOS. // Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682 // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682


//app.MapGrpcService<WeatherService>(); //app.MapGrpcService<WeatherService>();
app.MapGrpcService<AuthService>().EnableGrpcWeb(); app.MapGrpcService<AuthService>().EnableGrpcWeb();
app.MapGrpcService<SearchService>().EnableGrpcWeb();


app.MapCodeFirstGrpcReflectionService(); app.MapCodeFirstGrpcReflectionService();



+ 9
- 6
NemAnCore/Pages/Index.razor Прегледај датотеку

@using Grpc.Net.Client.Web @using Grpc.Net.Client.Web
@using GrpcShared @using GrpcShared
@using GrpcShared.DTO.Auth @using GrpcShared.DTO.Auth
@using GrpcShared.DTO.Search
@using NemAnBlazor.Services.Interfaces @using NemAnBlazor.Services.Interfaces
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IAuthClientService AuthService
@*@inject ISearchClientService SearchService*@
@*@inject IAuthClientService AuthService*@
@inject ISearchClientService SearchService
<PageTitle>Index</PageTitle> <PageTitle>Index</PageTitle>


<h1>Pozdrav Diligent!</h1> <h1>Pozdrav Diligent!</h1>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
//var response = await SearchService.GetListSearchAsync(new GrpcShared.DTO.Search.SearchRequest() { Query="venom", Type = "track"}); //var response = await SearchService.GetListSearchAsync(new GrpcShared.DTO.Search.SearchRequest() { Query="venom", Type = "track"});
AuthParams authParams = await AuthService.GetAuthParams();
//AuthParams authParams = await AuthService.GetAuthParams();
// await AuthService.GetAccessToken(new CodeResponse{ Code = "hello"}); // await AuthService.GetAccessToken(new CodeResponse{ Code = "hello"});
AuthRequest request = new() { ResponseType = "code", Scope = authParams.Scope, ClientId = authParams.ClientId, RedirectURI = authParams.RedirectURI};
string url = $"https://accounts.spotify.com/en/authorize?client_id={request.ClientId}&redirect_uri={request.RedirectURI}&response_type={request.ResponseType}&scope={request.Scope}&show_dialog=true";
//AuthRequest request = new() { ResponseType = "code", Scope = authParams.Scope, ClientId = authParams.ClientId, RedirectURI = authParams.RedirectURI};
//string url = $"https://accounts.spotify.com/en/authorize?client_id={request.ClientId}&redirect_uri={request.RedirectURI}&response_type={request.ResponseType}&scope={request.Scope}&show_dialog=true";




NavigationManager.NavigateTo(url);
//NavigationManager.NavigateTo(url);
SearchRequest request = new() { Query = "aitch", Type = "track" };
SearchResponse searchResponse = await SearchService.GetListSearchAsync(request);
} }
} }

+ 5
- 3
NemAnCore/Services/SearchClientService.cs Прегледај датотеку

using GrpcShared.DTO.Search;
using Grpc.Net.Client;
using GrpcShared.DTO.Search;
using GrpcShared.Interfaces; using GrpcShared.Interfaces;
using NemAnBlazor.Services.Interfaces; using NemAnBlazor.Services.Interfaces;
using ProtoBuf.Grpc.Client;


namespace NemAnBlazor.Services namespace NemAnBlazor.Services
{ {
{ {
private ISearchService _serviceClient; private ISearchService _serviceClient;


public SearchClientService(ISearchService serviceClient)
public SearchClientService(GrpcChannel grpcChannel)
{ {
_serviceClient = serviceClient;
_serviceClient = grpcChannel.CreateGrpcService<ISearchService>();
} }


public async Task<SearchResponse> GetListSearchAsync(SearchRequest request) public async Task<SearchResponse> GetListSearchAsync(SearchRequest request)

+ 9
- 0
gRPCServer/GLOBALS.cs Прегледај датотеку

using System;
namespace SpotifyService
{
public static class GLOBALS
{
public const String SPOTIFYURL = "https://api.spotify.com/v1/";
public const String MEDIATYPE = "application/json";
}
}

+ 7
- 1
gRPCServer/Program.cs Прегледај датотеку



// Add services to the container. // Add services to the container.


builder.Services.AddHttpClient("HttpClient", c =>
{
c.BaseAddress = new Uri(SpotifyService.GLOBALS.SPOTIFYURL);
c.DefaultRequestHeaders.Add("Accept", SpotifyService.GLOBALS.MEDIATYPE);
});

builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();


app.MapControllers(); app.MapControllers();


//app.MapGrpcService<WeatherService>(); //app.MapGrpcService<WeatherService>();
app.MapGrpcService<SearchService>().EnableGrpcWeb();
//app.MapGrpcService<SearchService>().EnableGrpcWeb();


app.MapCodeFirstGrpcReflectionService(); app.MapCodeFirstGrpcReflectionService();



+ 25
- 1
gRPCServer/Services/SearchService.cs Прегледај датотеку

using Grpc.Core; using Grpc.Core;
using GrpcShared;
using GrpcShared.DTO.Search;
using GrpcShared.Interfaces;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json; using System.Text.Json;


namespace SpotifyService.Services namespace SpotifyService.Services
{ {
public class SearchService
public class SearchService : ISearchService
{ {
private readonly IHttpClientFactory _httpClientFactory; private readonly IHttpClientFactory _httpClientFactory;


{ {
_httpClientFactory = httpClientFactory; _httpClientFactory = httpClientFactory;
} }

public async Task<SearchResponse> ListSearchAsync(SearchRequest request)
{
var client = _httpClientFactory.CreateClient("HttpClient");
client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer BQBG6iAJDhs8YGMsPG-Fj_nDP_6IrP6WIFtKJO2H_dBsEzSggWiIE9UQlEq9csweFIJTvoixP-OaWqQdI_OzW8zxsHjj-Hbsp9lKGV0EjbzsoezhMpa3Ee5D61akQGSRtZtO2L795kGHnfAtOcNhhtcayU9PsFsSlJbz3xbLACBPBzCk49DRRccDKeZUVgs");

var searchResult = await client.GetAsync($"search?q={request.Query}&type={request.Type}");


var responses = JsonConvert.DeserializeObject<SearchResponse>(await searchResult.Content.ReadAsStringAsync())!;

return new SearchResponse
{
Tracks = responses!.Tracks
};


}
} }
} }



+ 1
- 0
gRPCServer/SpotifyService.csproj Прегледај датотеку

<PackageReference Include="Grpc.AspNetCore" Version="2.40.0" /> <PackageReference Include="Grpc.AspNetCore" Version="2.40.0" />
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.47.0" /> <PackageReference Include="Grpc.AspNetCore.Web" Version="2.47.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="protobuf-net.Grpc" Version="1.0.171" /> <PackageReference Include="protobuf-net.Grpc" Version="1.0.171" />
<PackageReference Include="protobuf-net.Grpc.AspNetCore" Version="1.0.152" /> <PackageReference Include="protobuf-net.Grpc.AspNetCore" Version="1.0.152" />
<PackageReference Include="protobuf-net.Grpc.AspNetCore.Reflection" Version="1.0.152" /> <PackageReference Include="protobuf-net.Grpc.AspNetCore.Reflection" Version="1.0.152" />

Loading…
Откажи
Сачувај