| using ProtoBuf; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace GrpcShared.DTO.Search | |||||
| { | |||||
| [ProtoContract] | |||||
| public class SearchDetails | |||||
| { | |||||
| public partial class SearchContracts | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Tracks? Tracks { get; set; } | |||||
| } | |||||
| public partial class Tracks | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Item[]? Items { get; set; } | |||||
| } | |||||
| public partial class Item | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Album Album { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Artist[] Artists { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public long DurationMs { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public ExternalUrls ExternalUrls { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(7)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(8)] | |||||
| public long Popularity { get; set; } | |||||
| [ProtoMember(9)] | |||||
| public long TrackNumber { get; set; } | |||||
| [ProtoMember(10)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(11)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class Album | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public Image[] Images { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public DateTimeOffset ReleaseDate { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public long TotalTracks { get; set; } | |||||
| [ProtoMember(7)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(8)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class Image | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public long Height { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Uri Url { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public long Width { get; set; } | |||||
| } | |||||
| public partial class Artist | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public ExternalUrls ExternalUrls { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class ExternalUrls | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Spotify { get; set; } | |||||
| } | |||||
| } | |||||
| } |
| using ProtoBuf; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace GrpcShared.DTO.Search | |||||
| { | |||||
| [ProtoContract] | |||||
| public class SearchDetailsRequest | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public string Query { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public string Include_External { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public int Limit { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public int Offset { get; set; } | |||||
| } | |||||
| } |
| using ProtoBuf; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace GrpcShared.DTO.Search | |||||
| { | |||||
| [ProtoContract] | |||||
| public class SearhForItemResponse | |||||
| { | |||||
| public partial class SearchContracts | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Tracks? Tracks { get; set; } | |||||
| } | |||||
| public partial class Tracks | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Item[]? Items { get; set; } | |||||
| } | |||||
| public partial class Item | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Album Album { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Artist[] Artists { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public long DurationMs { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public ExternalUrls ExternalUrls { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(7)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(8)] | |||||
| public long Popularity { get; set; } | |||||
| [ProtoMember(9)] | |||||
| public long TrackNumber { get; set; } | |||||
| [ProtoMember(10)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(11)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class Album | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public Image[] Images { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public DateTimeOffset ReleaseDate { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public long TotalTracks { get; set; } | |||||
| [ProtoMember(7)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(8)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class Image | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public long Height { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Uri Url { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public long Width { get; set; } | |||||
| } | |||||
| public partial class Artist | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public ExternalUrls ExternalUrls { get; set; } | |||||
| [ProtoMember(2)] | |||||
| public Uri Href { get; set; } | |||||
| [ProtoMember(3)] | |||||
| public string Id { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public string Name { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public string Type { get; set; } | |||||
| [ProtoMember(6)] | |||||
| public string Uri { get; set; } | |||||
| } | |||||
| public partial class ExternalUrls | |||||
| { | |||||
| [ProtoMember(1)] | |||||
| public Uri Spotify { get; set; } | |||||
| } | |||||
| } | |||||
| } |
| <Project Sdk="Microsoft.NET.Sdk"> | |||||
| <PropertyGroup> | |||||
| <OutputType>Library</OutputType> | |||||
| <TargetFramework>net6.0</TargetFramework> | |||||
| <ImplicitUsings>enable</ImplicitUsings> | |||||
| <Nullable>enable</Nullable> | |||||
| </PropertyGroup> | |||||
| <ItemGroup> | |||||
| <Folder Include="DTO\" /> | |||||
| </ItemGroup> | |||||
| <ItemGroup> | |||||
| <PackageReference Include="protobuf-net.Grpc" Version="1.0.171" /> | |||||
| </ItemGroup> | |||||
| </Project> |
| using GrpcShared.DTO.Search; | |||||
| using ProtoBuf.Grpc.Configuration; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace GrpcShared.Interfaces | |||||
| { | |||||
| [Service] | |||||
| public interface ISearchService | |||||
| { | |||||
| Task<SearchDetails> ListSearchAsync(SearchDetailsRequest request); | |||||
| } | |||||
| } |
| <ImplicitUsings>enable</ImplicitUsings> | <ImplicitUsings>enable</ImplicitUsings> | ||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <ItemGroup> | |||||
| <None Remove="Protos\search.proto" /> | |||||
| </ItemGroup> | |||||
| <ItemGroup> | <ItemGroup> | ||||
| <PackageReference Include="Google.Protobuf" Version="3.21.4" /> | <PackageReference Include="Google.Protobuf" Version="3.21.4" /> | ||||
| <PackageReference Include="Grpc.Net.Client" Version="2.47.0" /> | <PackageReference Include="Grpc.Net.Client" Version="2.47.0" /> | ||||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" /> | <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <Protobuf Include="Protos\search.proto" GrpcServices="Client" /> | |||||
| </ItemGroup> | |||||
| </Project> | </Project> |
| Dobrodošli u našu NemAn aplikaciju. | Dobrodošli u našu NemAn aplikaciju. | ||||
| <button class="btn-outline-success" @onclick="Grpc">Spotify</button> | |||||
| @code { | @code { | ||||
| private async Task SpotifyRedirect() | |||||
| { | |||||
| NavigationManager.NavigateTo( | |||||
| "https://accounts.spotify.com/en/authorize?client_id=83e1d09876b049c4bb1953185a4b3bfb&redirect_uri=https%3A%2F%2Flocalhost%3A7229%2F&response_type=code&scope=user-read-currently-playing%20user-read-email%20user-library-modify%20user-top-read%0A%0A%0A&show_dialog=true"); | |||||
| } | |||||
| private async Task Grpc() | |||||
| { | |||||
| var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()); | |||||
| var channel = GrpcChannel.ForAddress("https://localhost:7251/", new GrpcChannelOptions | |||||
| { | |||||
| HttpHandler = httpHandler | |||||
| }); | |||||
| // var client = new Search.SearchClient(channel); | |||||
| var response = SearchClient.SearchForItem(new SearchForItemRequest { Query = "venom", Type = "track"/*, Limit = 5, Offset = 0, IncludeExternal = "audio"*/}); | |||||
| Console.WriteLine(response); | |||||
| } | |||||
| } | } |
| using Microsoft.AspNetCore.Components.Web; | using Microsoft.AspNetCore.Components.Web; | ||||
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||||
| using NemAnBlazor; | using NemAnBlazor; | ||||
| using SpotifyService.Protos; | |||||
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||||
| builder.RootComponents.Add<App>("#app"); | builder.RootComponents.Add<App>("#app"); | ||||
| builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | ||||
| builder.Services.AddSingleton(services => | |||||
| { | |||||
| var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler())); | |||||
| var baseUri = services.GetRequiredService<NavigationManager>().BaseUri; | |||||
| var channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions { HttpClient = httpClient }); | |||||
| return new Search.SearchClient(channel); | |||||
| }); | |||||
| await builder.Build().RunAsync(); | await builder.Build().RunAsync(); | ||||
| syntax = "proto3"; | |||||
| option csharp_namespace = "SpotifyService.Protos"; |
| syntax = "proto3"; | |||||
| option csharp_namespace = "SpotifyService.Protos"; | |||||
| package search; | |||||
| service Search{ | |||||
| rpc SearchForItem(SearchForItemRequest) returns (SearchForItemResponse); | |||||
| } | |||||
| message SearchForItemRequest{ | |||||
| string query = 1; | |||||
| string type = 2; | |||||
| //string include_external = 3; | |||||
| //int32 limit = 4; | |||||
| //int32 offset = 5; | |||||
| } | |||||
| message SearchForItemResponse{ | |||||
| message Tracks { | |||||
| string href = 1; | |||||
| repeated Items items = 2; | |||||
| } | |||||
| Tracks tracks = 1; | |||||
| message Items{ | |||||
| Album album = 1; | |||||
| repeated Artists artists = 2; | |||||
| int32 duration_ms = 3; | |||||
| External_urls1 external_urls = 4; | |||||
| string href = 5; | |||||
| string id = 6; | |||||
| string name = 7; | |||||
| int32 popularity = 8; | |||||
| int32 track_number = 9; | |||||
| string type = 10; | |||||
| string uri = 11; | |||||
| } | |||||
| } | |||||
| message Images { | |||||
| uint32 height = 1; | |||||
| string url = 2; | |||||
| uint32 width = 3; | |||||
| } | |||||
| message Album { | |||||
| string href = 1; | |||||
| string id = 2; | |||||
| repeated Images images = 3; | |||||
| string name = 4; | |||||
| string release_date = 5; | |||||
| uint32 total_tracks = 6; | |||||
| string type = 7; | |||||
| string uri = 8; | |||||
| } | |||||
| message External_urls { | |||||
| string spotify = 1; | |||||
| } | |||||
| message Artists { | |||||
| External_urls external_urls = 1; | |||||
| string href = 2; | |||||
| string id = 3; | |||||
| string name = 4; | |||||
| string type = 5; | |||||
| string uri = 6; | |||||
| } | |||||
| message External_urls1 { | |||||
| string spotify = 1; | |||||
| } |
| syntax = "proto3"; | |||||
| option csharp_namespace = "SpotifyService.Protos"; | |||||
| package search; | |||||
| service Search{ | |||||
| rpc SearchForItem(SearchForItemRequest) returns (SearchForItemResponse); | |||||
| } | |||||
| message SearchForItemRequest{ | |||||
| string query = 1; | |||||
| string type = 2; | |||||
| //string include_external = 3; | |||||
| //int32 limit = 4; | |||||
| //int32 offset = 5; | |||||
| } | |||||
| message SearchForItemResponse{ | |||||
| message Tracks { | |||||
| string href = 1; | |||||
| repeated Items items = 2; | |||||
| } | |||||
| Tracks tracks = 1; | |||||
| message Items{ | |||||
| Album album = 1; | |||||
| repeated Artists artists = 2; | |||||
| int32 duration_ms = 3; | |||||
| External_urls1 external_urls = 4; | |||||
| string href = 5; | |||||
| string id = 6; | |||||
| string name = 7; | |||||
| int32 popularity = 8; | |||||
| int32 track_number = 9; | |||||
| string type = 10; | |||||
| string uri = 11; | |||||
| } | |||||
| } | |||||
| message Images { | |||||
| uint32 height = 1; | |||||
| string url = 2; | |||||
| uint32 width = 3; | |||||
| } | |||||
| message Album { | |||||
| string href = 1; | |||||
| string id = 2; | |||||
| repeated Images images = 3; | |||||
| string name = 4; | |||||
| string release_date = 5; | |||||
| uint32 total_tracks = 6; | |||||
| string type = 7; | |||||
| string uri = 8; | |||||
| } | |||||
| message External_urls { | |||||
| string spotify = 1; | |||||
| } | |||||
| message Artists { | |||||
| External_urls external_urls = 1; | |||||
| string href = 2; | |||||
| string id = 3; | |||||
| string name = 4; | |||||
| string type = 5; | |||||
| string uri = 6; | |||||
| } | |||||
| message External_urls1 { | |||||
| string spotify = 1; | |||||
| } |
| namespace SpotifyService.Services | namespace SpotifyService.Services | ||||
| { | { | ||||
| public class SearchService : Search.SearchBase | |||||
| public class SearchService | |||||
| { | { | ||||
| private readonly IHttpClientFactory _httpClientFactory; | private readonly IHttpClientFactory _httpClientFactory; | ||||
| { | { | ||||
| _httpClientFactory = httpClientFactory; | _httpClientFactory = httpClientFactory; | ||||
| } | } | ||||
| public override async Task<SearchForItemResponse> SearchForItem(SearchForItemRequest request, ServerCallContext context) | |||||
| { | |||||
| var httpClient = _httpClientFactory.CreateClient(); | |||||
| httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "BQB7gF3eqg3xxqizhCuto24OgIbr_RFE6u6RJj30gHF7urqm2A0Q9venGsRXUlVuUSNOxdiG-8qb8Qp9_7luX_FqgrLswcnkgvtjm5toGR81HW-Dedul0E-K1OFc4sHz_iFsCN5QwBvIKqJjWmb6fioK4AZxp1N268WcoXzvW2LX9dv1gd6w-zSV"); | |||||
| var responseText = await httpClient.GetStringAsync($"https://api.spotify.com/v1/search?q={request.Query}&type={request.Type}"); | |||||
| var tracks = JsonSerializer.Deserialize<SearchContracts>(responseText); | |||||
| Console.WriteLine(tracks); | |||||
| //return new SearchForItemResponse | |||||
| //{ | |||||
| // Tracks = tracks!.Tracks | |||||
| //}; | |||||
| return null; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| </PackageReference> | </PackageReference> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <Protobuf Include="Protos\search.proto" GrpcServices="Server"></Protobuf> | |||||
| </ItemGroup> | |||||
| </Project> | </Project> |