OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / Connection / LazyJson.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 #nullable enable annotations
23
24 using System;
25 using System.Collections.Generic;
26 using System.Diagnostics.CodeAnalysis;
27 using System.Linq;
28 using System.Net.Http;
29 using System.Runtime.Serialization;
30 using System.Text;
31 using System.Threading.Tasks;
32 using OpenTween.Api;
33
34 namespace OpenTween.Connection
35 {
36     /// <summary>
37     /// HTTP レスポンスの受信と JSON のパース処理を延期させるクラス
38     /// </summary>
39     public sealed class LazyJson<T> : IDisposable
40     {
41         public HttpResponseMessage? Response { get; }
42
43         private T instance;
44         private bool completed = false;
45
46         public LazyJson(HttpResponseMessage response)
47             => this.Response = response;
48
49         internal LazyJson(T instance)
50         {
51             this.Response = null;
52
53             this.instance = instance;
54             this.completed = true;
55         }
56
57         public async Task<T> LoadJsonAsync()
58         {
59             if (this.completed)
60                 return this.instance!;
61
62             using var content = this.Response.Content;
63             var responseText = await content.ReadAsStringAsync()
64                 .ConfigureAwait(false);
65
66             try
67             {
68                 this.instance = MyCommon.CreateDataFromJson<T>(responseText);
69                 this.completed = true;
70
71                 return this.instance;
72             }
73             catch (SerializationException ex)
74             {
75                 throw TwitterApiException.CreateFromException(ex, responseText);
76             }
77         }
78
79         public void Dispose()
80             => this.Response?.Dispose();
81     }
82
83     public static class LazyJson
84     {
85         public static LazyJson<T> Create<T>(T instance)
86             => new LazyJson<T>(instance);
87     }
88
89     public static class LazyJsonTaskExtension
90     {
91         public static async Task IgnoreResponse<T>(this Task<LazyJson<T>> task)
92         {
93             using var lazyJson = await task.ConfigureAwait(false);
94             // レスポンスボディを読み込まず破棄する
95         }
96     }
97 }