Winify – Diff between revs 38 and 39
?pathlinks?
Rev 38 | Rev 39 | |||
---|---|---|---|---|
Line 30... | Line 30... | |||
30 | |
30 | |
|
Line 31... | Line 31... | |||
31 | private CancellationTokenSource _cancellationTokenSource; |
31 | private CancellationTokenSource _cancellationTokenSource; |
|
Line -... | Line 32... | |||
- | 32 | |
||
- | 33 | private Task _runTask; |
||
- | 34 | |
||
- | 35 | private HttpClient _httpClient; |
||
- | 36 | |
||
- | 37 | private readonly string _auth; |
||
- | 38 | |
||
- | 39 | private readonly Uri _webSocketsUri; |
||
32 | |
40 | |
|
Line 33... | Line 41... | |||
33 | private Task _runTask; |
41 | private readonly Uri _httpUri; |
|
Line -... | Line 42... | |||
- | 42 | |
||
- | 43 | #endregion |
||
- | 44 | |
||
- | 45 | #region Constructors, Destructors and Finalizers |
||
- | 46 | |
||
34 | |
47 | public GotifyConnection() |
|
35 | #endregion |
48 | { |
|
36 | |
49 | _httpClient = new HttpClient(); |
|
- | 50 | } |
||
- | 51 | |
||
- | 52 | public GotifyConnection(Server server) : this() |
||
- | 53 | { |
||
- | 54 | _server = server; |
||
- | 55 | |
||
- | 56 | _auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{_server.Username}:{_server.Password}")); |
||
- | 57 | _httpClient.DefaultRequestHeaders.Authorization = |
||
- | 58 | new AuthenticationHeaderValue("Basic", _auth); |
||
- | 59 | |
||
- | 60 | if (Uri.TryCreate(_server.Url, UriKind.Absolute, out _httpUri)) |
||
- | 61 | { |
||
- | 62 | // Build the web sockets URI. |
||
37 | #region Constructors, Destructors and Finalizers |
63 | var webSocketsUriBuilder = new UriBuilder(_httpUri); |
|
Line 38... | Line 64... | |||
38 | |
64 | webSocketsUriBuilder.Scheme = "ws"; |
|
39 | public GotifyConnection(Server server) |
65 | webSocketsUriBuilder.Path = Path.Combine(webSocketsUriBuilder.Path, "stream"); |
|
40 | { |
66 | _webSocketsUri = webSocketsUriBuilder.Uri; |
|
41 | _server = server; |
67 | } |
|
42 | } |
68 | } |
|
43 | |
69 | |
|
44 | public void Dispose() |
70 | public void Dispose() |
|
- | 71 | { |
||
- | 72 | if (_cancellationTokenSource != null) |
||
- | 73 | { |
||
45 | { |
74 | _cancellationTokenSource.Dispose(); |
|
Line 46... | Line 75... | |||
46 | if (_cancellationTokenSource != null) |
75 | _cancellationTokenSource = null; |
|
Line 47... | Line 76... | |||
47 | { |
76 | } |
|
Line 48... | Line 77... | |||
48 | _cancellationTokenSource.Dispose(); |
77 | |
|
49 | _cancellationTokenSource = null; |
78 | _httpClient.Dispose(); |
|
50 | } |
- | ||
51 | } |
- | ||
52 | |
- | ||
53 | #endregion |
- | ||
54 | |
- | ||
55 | #region Public Methods |
- | ||
56 | |
- | ||
57 | public void Start() |
- | ||
58 | { |
79 | _httpClient = null; |
|
59 | if (!Uri.TryCreate(_server.Url, UriKind.Absolute, out var httpUri)) return; |
80 | } |
|
Line 60... | Line 81... | |||
60 | |
81 | |
|
61 | // Build the web sockets URI. |
82 | #endregion |
|
Line 62... | Line 83... | |||
62 | var webSocketsUriBuilder = new UriBuilder(httpUri); |
83 | |
|
63 | webSocketsUriBuilder.Scheme = "ws"; |
84 | #region Public Methods |
|
- | 85 | |
||
- | 86 | public void Start() |
||
64 | webSocketsUriBuilder.Path = Path.Combine($"{webSocketsUriBuilder.Path}", "stream"); |
87 | { |
|
- | 88 | _cancellationTokenSource = new CancellationTokenSource(); |
||
65 | var webSocketsUri = webSocketsUriBuilder.Uri; |
89 | _cancellationToken = _cancellationTokenSource.Token; |
|
Line 66... | Line 90... | |||
66 | |
90 | |
|
Line 67... | Line 91... | |||
67 | _cancellationTokenSource = new CancellationTokenSource(); |
91 | _runTask = Run(_cancellationToken); |
|
Line 68... | Line -... | |||
68 | _cancellationToken = _cancellationTokenSource.Token; |
- | ||
69 | |
92 | } |
|
70 | _runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken); |
93 | |
|
71 | } |
94 | public void Stop() |
|
72 | |
95 | { |
|
73 | public void Stop() |
96 | if (_cancellationTokenSource != null) |
|
74 | { |
97 | { |
|
75 | if (_cancellationTokenSource != null) _cancellationTokenSource.Cancel(); |
98 | _cancellationTokenSource.Cancel(); |
|
76 | } |
99 | } |
|
77 | |
100 | } |
|
78 | #endregion |
101 | |
|
79 | |
- | ||
80 | #region Private Methods |
- | ||
81 | |
102 | #endregion |
|
Line 82... | Line 103... | |||
82 | private async Task Run(Uri webSocketsUri, Uri httpUri, string username, string password, |
103 | |
|
Line 83... | Line 104... | |||
83 | CancellationToken cancellationToken) |
104 | #region Private Methods |
|
84 | { |
105 | |
|
85 | try |
106 | private async Task Run(CancellationToken cancellationToken) |
|
Line 86... | Line 107... | |||
86 | { |
107 | { |
|
Line 87... | Line 108... | |||
87 | do |
108 | try |
|
- | 109 | { |
||
- | 110 | do |
||
- | 111 | { |
||
Line 88... | Line 112... | |||
88 | { |
112 | try |
|
- | 113 | { |
||
- | 114 | using (var webSocketClient = new ClientWebSocket()) |
||
- | 115 | { |
||
Line 89... | Line 116... | |||
89 | try |
116 | webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {_auth}"); |
|
Line 90... | Line 117... | |||
90 | { |
117 | |
|
Line 117... | Line 144... | |||
117 | continue; |
144 | continue; |
|
118 | } |
145 | } |
|
Line 119... | Line 146... | |||
119 | |
146 | |
|
Line 120... | Line 147... | |||
120 | gotifyNotification.Server = _server; |
147 | gotifyNotification.Server = _server; |
|
121 | |
148 | |
|
- | 149 | if (!Uri.TryCreate(Path.Combine($"{_httpUri}", "application"), UriKind.Absolute, |
||
122 | if (!Uri.TryCreate(Path.Combine($"{httpUri}", "application"), UriKind.Absolute, |
150 | out var applicationUri)) |
|
- | 151 | { |
||
Line 123... | Line 152... | |||
123 | out var applicationUri)) |
152 | continue; |
|
124 | continue; |
153 | } |
|
Line 125... | Line 154... | |||
125 | |
154 | |
|
126 | var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, httpUri, |
155 | var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, |
|
Line 127... | Line 156... | |||
127 | applicationUri, auth, cancellationToken); |
156 | applicationUri, cancellationToken); |
|
Line 149... | Line 178... | |||
149 | { |
178 | { |
|
150 | Log.Warning(ex, "Failure running connection loop."); |
179 | Log.Warning(ex, "Failure running connection loop."); |
|
151 | } |
180 | } |
|
152 | } |
181 | } |
|
Line 153... | Line 182... | |||
153 | |
182 | |
|
154 | private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri, |
- | ||
155 | string auth, |
183 | private async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri applicationUri, |
|
156 | CancellationToken cancellationToken) |
184 | CancellationToken cancellationToken) |
|
157 | { |
- | ||
158 | using (var httpClient = new HttpClient()) |
- | ||
159 | { |
- | ||
160 | httpClient.DefaultRequestHeaders.Authorization = |
- | ||
161 | new AuthenticationHeaderValue("Basic", auth); |
- | ||
162 | |
185 | { |
|
Line 163... | Line 186... | |||
163 | var applicationResponse = await httpClient.GetAsync(applicationUri, cancellationToken); |
186 | var applicationResponse = await _httpClient.GetAsync(applicationUri, cancellationToken); |
|
Line 164... | Line 187... | |||
164 | |
187 | |
|
165 | var applications = await applicationResponse.Content.ReadAsStringAsync(); |
188 | var applications = await applicationResponse.Content.ReadAsStringAsync(); |
|
Line 166... | Line 189... | |||
166 | |
189 | |
|
- | 190 | var gotifyApplications = |
||
- | 191 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
||
- | 192 | |
||
Line 167... | Line 193... | |||
167 | var gotifyApplications = |
193 | if (gotifyApplications == null) |
|
- | 194 | { |
||
- | 195 | return null; |
||
168 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
196 | } |
|
169 | |
197 | |
|
- | 198 | foreach (var application in gotifyApplications) |
||
Line 170... | Line 199... | |||
170 | if (gotifyApplications == null) return null; |
199 | { |
|
171 | |
200 | if (application.Id != appId) |
|
- | 201 | { |
||
172 | foreach (var application in gotifyApplications) |
202 | continue; |
|
- | 203 | } |
||
Line 173... | Line 204... | |||
173 | { |
204 | |
|
Line 174... | Line 205... | |||
174 | if (application.Id != appId) continue; |
205 | if (!Uri.TryCreate(Path.Combine($"{_httpUri}", $"{application.Image}"), UriKind.Absolute, |
|
175 | |
206 | out var applicationImageUri)) |
|
176 | if (!Uri.TryCreate(Path.Combine($"{httpUri}", $"{application.Image}"), UriKind.Absolute, |
207 | { |
|
Line 177... | Line 208... | |||
177 | out var applicationImageUri)) |
208 | continue; |
|
178 | continue; |
- | ||
179 | |
209 | } |
|
180 | var imageResponse = await httpClient.GetAsync(applicationImageUri, cancellationToken); |
210 | |
|
Line 181... | Line 211... | |||
181 | |
211 | var imageResponse = await _httpClient.GetAsync(applicationImageUri, cancellationToken); |
|
182 | using (var memoryStream = new MemoryStream()) |
212 | |