Jump to content

Tutorial/exemple on plugins/channel creation


AMenard
Go to solution Solved by Luke,

Recommended Posts

chef
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

TallBoiDez
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

TallBoiDez

@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

TallBoiDez

@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

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

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
bakes82

…. 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

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

TallBoiDez
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

I really hope this helps. I'm not a very good teacher, and I apologize.

workflow.thumb.png.0bac89964c3ba45e46c9e5eed81651f0.png

 

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 by chef
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...
TallBoiDez

@chef Where exactly do I put this but of code:

var myData = await TempClass.Instance.GetDataFromMyApi()

Edited by TallBoiDez
Link to comment
Share on other sites

TallBoiDez
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.

workflow.thumb.png.0bac89964c3ba45e46c9e5eed81651f0.png

 

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...