-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTBClient.cs
67 lines (60 loc) · 2.16 KB
/
STBClient.cs
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
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace EnigmaLiveTV {
public class STBClient : ISTBClient {
private readonly ILogger<STBClient> _logger;
private readonly HttpClient _client;
private readonly IOptions<Settings> _settings;
public STBClient(ILogger<STBClient> logger, HttpClient client, IOptions<Settings> settings) {
_logger = logger;
_client = client;
_settings = settings;
_client.BaseAddress = new Uri(_settings.Value.STB);
}
public async Task<STBDeviceInfo> GetDeviceInfoAsync() {
try {
var response = await _client.GetAsync("/api/deviceinfo");
response.EnsureSuccessStatusCode();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var content = await response.Content.ReadAsStringAsync();
var ret = JsonSerializer.Deserialize<STBDeviceInfo>(content, options);
_logger.LogDebug("/api/deviceinfo returned: " + content);
return ret;
} catch (Exception ex) {
_logger.LogError("Can't read device info: " + ex.Message);
throw;
}
}
public async Task<STBEPG> GetEPGAsync() {
try {
var response = await _client.GetAsync("/api/epgxmltv" + "?" + _settings.Value.Filter);
response.EnsureSuccessStatusCode();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var content = await response.Content.ReadAsStringAsync();
var ret = JsonSerializer.Deserialize<STBEPG>(content, options);
_logger.LogDebug("/api/epgxmltv returned: " + content);
return ret;
} catch (Exception ex) {
_logger.LogError("Can't read epg: " + ex.Message);
throw;
}
}
public async Task<(MediaTypeHeaderValue, string)> GetAnyURL(string path) {
try {
var response = await _client.GetAsync("/" + path);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var type = response.Content.Headers.ContentType;
return (type, content);
} catch (Exception ex) {
_logger.LogError($"Can't read {path}: " + ex.Message);
throw;
}
}
}
}