aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Plugin.cs
blob: af885ef825c1d1e1e2ea199586aca8a85b30e1e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Reactive.Linq;
using MediaBrowser.Api.HttpHandlers;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller;
using MediaBrowser.Net;
using MediaBrowser.Net.Handlers;

namespace MediaBrowser.Api
{
    public class Plugin : BasePlugin<BasePluginConfiguration>
    {
        protected override void InitInternal()
        {
            var httpServer = Kernel.Instance.HttpServer;

            httpServer.Where(ctx => ctx.LocalPath.IndexOf("/api/", StringComparison.OrdinalIgnoreCase) != -1).Subscribe(ctx =>
            {
                BaseHandler handler = GetHandler(ctx);

                if (handler != null)
                {
                    ctx.Respond(handler);
                }
            });
        }

        private BaseHandler GetHandler(RequestContext ctx)
        {
            BaseHandler handler = null;

            string localPath = ctx.LocalPath;

            if (localPath.EndsWith("/api/item", StringComparison.OrdinalIgnoreCase))
            {
                handler = new ItemHandler();
            }
            else if (localPath.EndsWith("/api/image", StringComparison.OrdinalIgnoreCase))
            {
                handler = new ImageHandler();
            } 
            else if (localPath.EndsWith("/api/users", StringComparison.OrdinalIgnoreCase))
            {
                handler = new UsersHandler();
            }
            else if (localPath.EndsWith("/api/media", StringComparison.OrdinalIgnoreCase))
            {
                handler = new MediaHandler();
            }
            else if (localPath.EndsWith("/api/genre", StringComparison.OrdinalIgnoreCase))
            {
                handler = new GenreHandler();
            }
            else if (localPath.EndsWith("/api/genres", StringComparison.OrdinalIgnoreCase))
            {
                handler = new GenresHandler();
            }
            else if (localPath.EndsWith("/api/studio", StringComparison.OrdinalIgnoreCase))
            {
                handler = new StudioHandler();
            }
            else if (localPath.EndsWith("/api/studios", StringComparison.OrdinalIgnoreCase))
            {
                handler = new StudiosHandler();
            }
            else if (localPath.EndsWith("/api/recentlyaddeditems", StringComparison.OrdinalIgnoreCase))
            {
                handler = new RecentlyAddedItemsHandler();
            }
            else if (localPath.EndsWith("/api/inprogressitems", StringComparison.OrdinalIgnoreCase))
            {
                handler = new InProgressItemsHandler();
            }

            if (handler != null)
            {
                handler.RequestContext = ctx;
            }

            return handler;
        }
    }
}