aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/ApiInteraction/ApiController.cs
blob: b3a51c512e839410f5dd5f7297df2987f7094ccb (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Json;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Users;

namespace MediaBrowser.Common.ApiInteraction
{
    public class ApiController
    {
        public string ApiUrl { get; set; }

        private WebClient WebClient { get; set; }

        public ApiController()
        {
            WebClient = new WebClient();
        }

        public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetRootItem(Guid userId)
        {
            string url = ApiUrl + "/item?userId=" + userId.ToString();

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return DeserializeBaseItemWrapper(gzipStream);
                }
            }
        }

        public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetItem(Guid id, Guid userId)
        {
            string url = ApiUrl + "/item?userId=" + userId.ToString();

            if (id != Guid.Empty)
            {
                url += "&id=" + id.ToString();
            }

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return DeserializeBaseItemWrapper(gzipStream);
                }
            }
        }

        public async Task<IEnumerable<User>> GetAllUsers()
        {
            string url = ApiUrl + "/users";

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
                }
            }
        }

        public async Task<IEnumerable<CategoryInfo>> GetAllGenres(Guid userId)
        {
            string url = ApiUrl + "/genres?userId=" + userId.ToString();

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
                }
            }
        }

        public async Task<CategoryInfo> GetGenre(string name, Guid userId)
        {
            string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
                }
            }
        }

        public async Task<IEnumerable<CategoryInfo>> GetAllStudios(Guid userId)
        {
            string url = ApiUrl + "/studios?userId=" + userId.ToString();

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
                }
            }
        }

        public async Task<CategoryInfo> GetStudio(string name, Guid userId)
        {
            string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;

            using (Stream stream = await WebClient.OpenReadTaskAsync(url))
            {
                using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                {
                    return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
                }
            }
        }

        private static ApiBaseItemWrapper<ApiBaseItem> DeserializeBaseItemWrapper(Stream stream)
        {
            ApiBaseItemWrapper<ApiBaseItem> data = JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(stream);

            return data;
        }
    }
}