chef 3745 Posted October 9, 2022 Share Posted October 9, 2022 10 minutes ago, TallBoiDez said: Would it be possible to incorporate my REST APi into my emby plugin or would that cause issues? Sorry work has been very busy the last couple days. Very short staffed. Okay so it sounds like you want to request data from your API. Indeed you want to use the IHttpclient to request the data. You can build out the IHttpClient with the proper headers and urls to access the data from your API. Then (depending on how the data is formed, ie. JSON or maybe xml) read the data into your plugin. Inorder to store the read data from your API, you will have to create the appropriate classes to hold it. From there you can use the stored data however you want. Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 9, 2022 Share Posted October 9, 2022 22 minutes ago, chef said: Sorry work has been very busy the last couple days. Very short staffed. Okay so it sounds like you want to request data from your API. Indeed you want to use the IHttpclient to request the data. You can build out the IHttpClient with the proper headers and urls to access the data from your API. Then (depending on how the data is formed, ie. JSON or maybe xml) read the data into your plugin. Inorder to store the read data from your API, you will have to create the appropriate classes to hold it. From there you can use the stored data however you want. How should my headers look and what would be the appropriate classes to hold the info? Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 10, 2022 Share Posted October 10, 2022 @chef //IHttpClient Configuration public Task<HttpResponseInfo> GetResponse(HttpRequestOptions options) { var response = new HttpResponseInfo(); Uri baseUrl = new Uri(); IRestClient client = new RestClient(baseUrl); } public Task<Stream> Get(HttpRequestOptions options) { throw new NotImplementedException(); } public Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string httpMethod) { throw new NotImplementedException(); } public Task<HttpResponseInfo> Post(HttpRequestOptions options) { throw new NotImplementedException(); } public Task<string> GetTempFile(HttpRequestOptions options) { throw new NotImplementedException(); } public Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options) { throw new NotImplementedException(); } public IDisposable GetConnectionContext(HttpRequestOptions options) { throw new NotImplementedException(); } } is this correct so far? Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 10, 2022 Share Posted October 10, 2022 Is my web api supposed to be part of my emby plugin to properly use the IHttpClient Interface? Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 11, 2022 Share Posted October 11, 2022 @Luke can you explain to me how to use the IHttpClient Interface or provide and example of how to use it Link to comment Share on other sites More sharing options...
Luke 37007 Posted October 11, 2022 Share Posted October 11, 2022 12 minutes ago, TallBoiDez said: @Luke can you explain to me how to use the IHttpClient Interface or provide and example of how to use it You don't implement it. Just add it to your classes as part of the constructor and the server will pass it in : https://github.com/MediaBrowser/Emby/wiki/Dependency-Injection Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 11, 2022 Share Posted October 11, 2022 @Lukei have created my REST API could i use the IRestfulService interface to add my API to my plugin and use it as a metadata scraper for actors and actor images? Link to comment Share on other sites More sharing options...
Luke 37007 Posted October 11, 2022 Share Posted October 11, 2022 21 hours ago, TallBoiDez said: @Lukei have created my REST API could i use the IRestfulService interface to add my API to my plugin and use it as a metadata scraper for actors and actor images? hi, best thing to do is look at similar plugins. For example: https://github.com/MediaBrowser/Emby.Plugins.MyAnimeList 1 Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 21, 2022 Share Posted October 21, 2022 I'm completely stumped at this point, is it possible to run my rest API as a script in emby to fetch actor metadata? @Luke @chef Link to comment Share on other sites More sharing options...
bakes82 89 Posted October 21, 2022 Share Posted October 21, 2022 …. Why can’t you just inject the httpclient and make the call. The referenced anime project implemented a method called webrequestapi that uses the httpclient. But as I said before. You’re doing a disservice trying to learn c# on emby. Link to comment Share on other sites More sharing options...
chef 3745 Posted October 21, 2022 Share Posted October 21, 2022 It's true debugging gets really difficult because you don't have an instant trace of issues in the code. You've got to restart the server and read logs. That can be hard. Where is your actors images API right now? Where is it hosted? Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 21, 2022 Share Posted October 21, 2022 1 hour ago, chef said: It's true debugging gets really difficult because you don't have an instant trace of issues in the code. You've got to restart the server and read logs. That can be hard. Where is your actors images API right now? Where is it hosted? I'm trying to host on my NAS Link to comment Share on other sites More sharing options...
chef 3745 Posted October 21, 2022 Share Posted October 21, 2022 (edited) I really hope this helps. I'm not a very good teacher, and I apologize. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Serialization; namespace Emby.Example { public class TempClass : IServerEntryPoint //<== Because I inherit IServerEntryPoint, I initialized when the server starts! Thanks Emby for taking care of this for me. { private IHttpClient HttpClient { get; set; } private IJsonSerializer JsonSerializer { get; set; } public static TempClass Instance { get; set; } //<== I am a public object that makes it possible to access me from anywhere in my application. public TempClass(IHttpClient httpClient, IJsonSerializer jsonSerializer) { HttpClient = httpClient; //<== I was added through Emby's dependency injection! Thanks Emby for giving me a fully initialized object I can use! JsonSerializer = jsonSerializer; //<== I was also added through Emby's dependency injection! Thanks Emby for giving me another fully initialized object I can use! Instance = this; } private async Task<Stream> GetResponseStreamDataFromMyApi(string url) //<== I am an example of a method that will request data from my API. { var requestOptions = (new HttpRequestOptions() //<== all the options your API is expecting during the request/response transaction. { AcceptHeader = "", //<== All the Headers my API is expecting to get when requesting data goes here. Url = url, //<== the URL I will use to request/access data from my API. RequestContent = "application/json" .AsMemory() //<== I am telling the API I am about to send JSON data in this example. But, there are many possible content types I could send. //Note:I am using AsMemory extension methods from System.Memory namespace so I am left off the HEAP. It looks trick, but is not. }); var response = await HttpClient.Get(requestOptions); //Go get it! I am await'd so response will eventually continue with my data. so I'll chill return response; //<== I am the response stream from the API. I contain all the data that the API just gave me... } public async Task<MyResponseClass> GetDataFromMyApi() { var streamData = await GetResponseStreamDataFromMyApi("MY_API_URL"); return JsonSerializer.DeserializeFromStream<MyResponseClass>(streamData); //== I am now a fully serialized object of whatever cam back form the API response. } public void Dispose() { } public void Run() { } } } public class MyResponseClass //<== I am an example of a class that will hold data that returned from my API. { public string Name { get; set; } public string ImageUrl { get; set; } } You can use the TempClass anywhere in the plugin, using the "Instance" variable. In essence it is now a sort of Singleton. (You can read more about design patterns later perhaps). var myData = await TempClass.Instance.GetDataFromMyApi() Edited October 21, 2022 by chef 1 Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 30, 2022 Share Posted October 30, 2022 (edited) @chef Where exactly do I put this but of code: var myData = await TempClass.Instance.GetDataFromMyApi() Edited October 30, 2022 by TallBoiDez Link to comment Share on other sites More sharing options...
Luke 37007 Posted October 30, 2022 Share Posted October 30, 2022 When is it needed? Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 30, 2022 Share Posted October 30, 2022 On 10/21/2022 at 10:54 AM, chef said: I really hope this helps. I'm not a very good teacher, and I apologize. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Serialization; namespace Emby.Example { public class TempClass : IServerEntryPoint //<== Because I inherit IServerEntryPoint, I initialized when the server starts! Thanks Emby for taking care of this for me. { private IHttpClient HttpClient { get; set; } private IJsonSerializer JsonSerializer { get; set; } public static TempClass Instance { get; set; } //<== I am a public object that makes it possible to access me from anywhere in my application. public TempClass(IHttpClient httpClient, IJsonSerializer jsonSerializer) { HttpClient = httpClient; //<== I was added through Emby's dependency injection! Thanks Emby for giving me a fully initialized object I can use! JsonSerializer = jsonSerializer; //<== I was also added through Emby's dependency injection! Thanks Emby for giving me another fully initialized object I can use! Instance = this; } private async Task<Stream> GetResponseStreamDataFromMyApi(string url) //<== I am an example of a method that will request data from my API. { var requestOptions = (new HttpRequestOptions() //<== all the options your API is expecting during the request/response transaction. { AcceptHeader = "", //<== All the Headers my API is expecting to get when requesting data goes here. Url = url, //<== the URL I will use to request/access data from my API. RequestContent = "application/json" .AsMemory() //<== I am telling the API I am about to send JSON data in this example. But, there are many possible content types I could send. //Note:I am using AsMemory extension methods from System.Memory namespace so I am left off the HEAP. It looks trick, but is not. }); var response = await HttpClient.Get(requestOptions); //Go get it! I am await'd so response will eventually continue with my data. so I'll chill return response; //<== I am the response stream from the API. I contain all the data that the API just gave me... } public async Task<MyResponseClass> GetDataFromMyApi() { var streamData = await GetResponseStreamDataFromMyApi("MY_API_URL"); return JsonSerializer.DeserializeFromStream<MyResponseClass>(streamData); //== I am now a fully serialized object of whatever cam back form the API response. } public void Dispose() { } public void Run() { } } } public class MyResponseClass //<== I am an example of a class that will hold data that returned from my API. { public string Name { get; set; } public string ImageUrl { get; set; } } You can use the TempClass anywhere in the plugin, using the "Instance" variable. In essence it is now a sort of Singleton. (You can read more about design patterns later perhaps). var myData = await TempClass.Instance.GetDataFromMyApi() @Luke he posted this for me and idk if I need that last or where to put it Link to comment Share on other sites More sharing options...
TallBoiDez 8 Posted October 30, 2022 Share Posted October 30, 2022 Ok! i figured out what kind of plugin i want to make, I trying to make a metadata provider plugin @chef 1 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now