OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright 2013 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System; | |
18 using System.CodeDom; | |
19 using System.Collections.Generic; | |
20 using System.IO; | |
21 using System.Linq; | |
22 using System.Text; | |
23 using System.Threading; | |
24 using System.Threading.Tasks; | |
25 | |
26 using Google.Apis.Discovery; | |
27 using Google.Apis.Download; | |
28 | |
29 namespace Google.Apis.Tools.CodeGen.Decorator.ResourceDecorator.RequestDecorator | |
30 { | |
31 /// <summary> | |
32 /// Media Downloader decorator which adds the following members in case the
method supports media download: | |
33 /// <list type="bullet"> | |
34 /// <item><description>MediaDownloader getter property</description></item>· | |
35 /// <item><description>Download method which gets a stream</description></it
em> | |
36 /// <item><description>DownloadAsync method which gets a stream</description
></item> | |
37 /// <item><description>DownloadAsync method which gets a stream and a cancel
lation token</description></item> | |
38 /// </list>· | |
39 /// </summary> | |
40 public class MediaDownloaderDecorator : IRequestDecorator | |
41 { | |
42 public void DecorateClass(IResource resource, IMethod request, CodeTypeD
eclaration requestClass, | |
43 CodeTypeDeclaration resourceClass) | |
44 { | |
45 if (!request.SupportMediaDownload) | |
46 return; | |
47 | |
48 GenerateMediaDownloaderProperty(requestClass); | |
49 GenerateDownload(requestClass); | |
50 GenerateDownloadAsync(requestClass, false); | |
51 GenerateDownloadAsync(requestClass, true); | |
52 } | |
53 | |
54 /// <summary>Generates an <seealso cref="Google.Apis.Download.IMediaDown
loader"/> property.</summary> | |
55 private static void GenerateMediaDownloaderProperty(CodeTypeDeclaration
requestClass) | |
56 { | |
57 // Generates: | |
58 // private IMediaDownloader _mediaDownloader; | |
59 // public IMediaDownloader MediaDownloader { get { return _mediaDown
loader; } } | |
60 | |
61 foreach (CodeTypeMember memeber in DecoratorUtil.CreateAutoProperty(
"mediaDownloader", null, | |
62 new CodeTypeReference(typeof(IMediaDownloader)), new List<string
>(), true)) | |
63 { | |
64 requestClass.Members.Add(memeber); | |
65 } | |
66 } | |
67 | |
68 /// <summary>Generates a <c>Download</c> method which gets a stream.</su
mmary> | |
69 private static void GenerateDownload(CodeTypeDeclaration requestClass) | |
70 { | |
71 // Generates:· | |
72 // private void Download(Stream stream) | |
73 // { | |
74 // _mediaDownloader.Download(GenerateRequestUri(), stream); | |
75 // } | |
76 | |
77 var methodName = "Download"; | |
78 var comment = "Synchronously download the media into the given strea
m."; | |
79 var method = new CodeMemberMethod(); | |
80 | |
81 method.Name = methodName; | |
82 method.Comments.Add(new CodeCommentStatement("<summary>" + comment +
"</summary>", true)); | |
83 method.ReturnType = new CodeTypeReference(typeof(void)); | |
84 method.Attributes = MemberAttributes.Public; | |
85 method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(
Stream), "stream")); | |
86 | |
87 method.Statements.Add( | |
88 new CodeMethodInvokeExpression(new CodeVariableReferenceExpressi
on("_mediaDownloader"), | |
89 methodName, new CodeMethodInvokeExpression( | |
90 new CodeMethodReferenceExpression(new CodeThisReferenceE
xpression(), "GenerateRequestUri")), | |
91 new CodeVariableReferenceExpression("stream"))); | |
92 | |
93 requestClass.Members.Add(method); | |
94 } | |
95 | |
96 /// <summary> | |
97 /// Generates a <c>DownloadAsync</c> method which gets the stream and if
the given <c>useCancellationToken</c>· | |
98 /// parameter is <c>true</c> a cancellation token as well. | |
99 /// </summary> | |
100 private static void GenerateDownloadAsync(CodeTypeDeclaration requestCla
ss, bool useCancellationToken) | |
101 { | |
102 // Generates:· | |
103 // If useCancelltionToken | |
104 // private Task<IDownloadProgress> DownloadAsync(Stream stream, Canc
ellationToken cancelleationToken) | |
105 // { | |
106 // return _mediaDownloader.DownloadAsync(GenerateRequestUri(), st
ream, cancellationToken); | |
107 // } | |
108 | |
109 // else | |
110 // private Task<IDownloadProgress> DownloadAsync(Stream stream) | |
111 // { | |
112 // return _mediaDownloader.DownloadAsync(GenerateRequestUri(), st
ream); | |
113 // } | |
114 | |
115 var methodName = "DownloadAsync"; | |
116 var comment = "Asynchronously download the media into the given stre
am."; | |
117 | |
118 var method = new CodeMemberMethod(); | |
119 | |
120 method.Name = methodName; | |
121 method.Comments.Add(new CodeCommentStatement("<summary>" + comment +
"</summary>", true)); | |
122 method.ReturnType = new CodeTypeReference(typeof(Task<IDownloadProgr
ess>)); | |
123 method.Attributes = MemberAttributes.Public; | |
124 method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(
Stream), "stream")); | |
125 if (useCancellationToken) | |
126 { | |
127 method.Parameters.Add(new CodeParameterDeclarationExpression(typ
eof(CancellationToken), | |
128 "cancellationToken")); | |
129 } | |
130 | |
131 IList<CodeExpression> parameters = new List<CodeExpression>(); | |
132 parameters.Add(new CodeMethodInvokeExpression( | |
133 new CodeMethodReferenceExpression(new CodeThisReferenceExpressio
n(), "GenerateRequestUri"))); | |
134 parameters.Add(new CodeVariableReferenceExpression("stream")); | |
135 if (useCancellationToken) | |
136 { | |
137 parameters.Add(new CodeVariableReferenceExpression("cancellation
Token")); | |
138 } | |
139 | |
140 method.Statements.Add( | |
141 new CodeMethodReturnStatement( | |
142 new CodeMethodInvokeExpression(new CodeVariableReferenceExpressi
on("_mediaDownloader"), | |
143 "DownloadAsync", parameters.ToArray()))); | |
144 | |
145 requestClass.Members.Add(method); | |
146 } | |
147 } | |
148 } | |
OLD | NEW |