88using System . Collections . Generic ;
99using Newtonsoft . Json ;
1010using Newtonsoft . Json . Linq ;
11- using CloudCMS . Platforms ;
12- using CloudCMS . Exceptions ;
11+ using CloudCMS ;
1312
1413namespace CloudCMS
1514{
@@ -118,30 +117,82 @@ public async Task<JObject> DeleteAsync(string uri, IDictionary<string, string> q
118117 return await RequestAsync ( uri , method , queryParams ) ;
119118 }
120119
120+ public async Task < Stream > DownloadAsync ( string uri )
121+ {
122+ HttpResponseMessage response = await _requestAsync ( uri , HttpMethod . Get ) ;
123+ return await response . Content . ReadAsStreamAsync ( ) ;
124+ }
125+
126+ public async Task < byte [ ] > DownloadBytesAsync ( string uri )
127+ {
128+ HttpResponseMessage response = await _requestAsync ( uri , HttpMethod . Get ) ;
129+ return await response . Content . ReadAsByteArrayAsync ( ) ;
130+ }
131+
132+ public async Task UploadAsync ( string uri , byte [ ] bytes , string mimetype , IDictionary < string , string > paramMap = null )
133+ {
134+ paramMap ??= new Dictionary < string , string > ( ) ;
135+ HttpContent content = GenerateUploadContent ( bytes , mimetype ) ;
136+ await _requestAsync ( uri , HttpMethod . Post , paramMap , content ) ;
137+ }
138+
139+ public async Task UploadAsync ( string uri , Stream stream , string mimetype , IDictionary < string , string > paramMap = null )
140+ {
141+ paramMap ??= new Dictionary < string , string > ( ) ;
142+ HttpContent content = GenerateUploadContent ( stream , mimetype ) ;
143+ await _requestAsync ( uri , HttpMethod . Post , paramMap , content ) ;
144+ }
145+
146+ public async Task UploadAsync ( string uri , IDictionary < string , string > paramMap , IDictionary < string , AttachmentContent > payloads )
147+ {
148+ HttpContent content = GenerateUploadContent ( payloads ) ;
149+ await _requestAsync ( uri , HttpMethod . Post , paramMap , content ) ;
150+ }
151+
152+ private HttpContent GenerateUploadContent ( byte [ ] bytes , string mimetype )
153+ {
154+ return new AttachmentContent ( bytes , mimetype ) ;
155+ }
156+
157+ private HttpContent GenerateUploadContent ( Stream stream , string mimetype )
158+ {
159+ return new AttachmentContent ( stream , mimetype ) ;
160+
161+ }
162+
163+ private HttpContent GenerateUploadContent ( IDictionary < string , AttachmentContent > payloads )
164+ {
165+ MultipartFormDataContent multi = new MultipartFormDataContent ( ) ;
166+ foreach ( var kv in payloads )
167+ {
168+ string filename = kv . Key ;
169+ AttachmentContent payload = kv . Value ;
170+
171+ multi . Add ( payload , filename , filename ) ;
172+ }
173+
174+ return multi ;
175+ }
176+
177+
121178 public async Task < JObject > RequestAsync ( string uri , HttpMethod method , IDictionary < string , string > queryParams = null , HttpContent body = null )
122179 {
123- using ( HttpClient client = new HttpClient ( ) )
180+ HttpResponseMessage response = await _requestAsync ( uri , method , queryParams , body ) ;
181+ string responseString = await response . Content . ReadAsStringAsync ( ) ;
182+ if ( ! response . IsSuccessStatusCode )
124183 {
125- var uriBuilder = new UriBuilder ( Config . baseURL + uri ) ;
184+ throw new CloudCMSRequestException ( responseString ) ;
185+ }
126186
127- var query = HttpUtility . ParseQueryString ( uriBuilder . Query ) ;
128- // Add "full" parameter unless already set
129- if ( query [ "full" ] == null )
130- {
131- query [ "full" ] = "true" ;
132- }
133- // Add all params to query string
134- if ( queryParams != null )
135- {
136- foreach ( var kvp in queryParams )
137- {
138- query [ kvp . Key ] = kvp . Value ;
139- }
140- }
141- uriBuilder . Query = query . ToString ( ) ;
187+ return JObject . Parse ( responseString ) ;
188+ }
189+
190+ private async Task < HttpResponseMessage > _requestAsync ( string uri , HttpMethod method , IDictionary < string , string > queryParams = null , HttpContent body = null )
191+ {
192+ using ( HttpClient client = new HttpClient ( ) )
193+ {
194+ var url = BuildUrl ( uri , queryParams ) ;
142195
143- string url = uriBuilder . ToString ( ) ;
144-
145196 HttpRequestMessage request = new HttpRequestMessage ( method , url ) ;
146197 if ( body != null )
147198 {
@@ -157,16 +208,36 @@ public async Task<JObject> RequestAsync(string uri, HttpMethod method, IDictiona
157208 client . DefaultRequestHeaders . Authorization = auth ;
158209
159210 HttpResponseMessage response = await client . SendAsync ( request ) ;
160- string responseString = await response . Content . ReadAsStringAsync ( ) ;
161- if ( ! response . IsSuccessStatusCode )
211+ return response ;
212+ }
213+ }
214+
215+ private string BuildUrl ( string uri , IDictionary < string , string > queryParams )
216+ {
217+ var uriBuilder = new UriBuilder ( Config . baseURL + uri ) ;
218+
219+ var query = HttpUtility . ParseQueryString ( uriBuilder . Query ) ;
220+ // Add "full" parameter unless already set
221+ if ( query [ "full" ] == null )
222+ {
223+ query [ "full" ] = "true" ;
224+ }
225+
226+ // Add all params to query string
227+ if ( queryParams != null )
228+ {
229+ foreach ( var kvp in queryParams )
162230 {
163- throw new CloudCMSRequestException ( responseString ) ;
231+ query [ kvp . Key ] = kvp . Value ;
164232 }
165-
166- return JObject . Parse ( responseString ) ;
167233 }
234+
235+ uriBuilder . Query = query . ToString ( ) ?? "" ;
236+
237+ return uriBuilder . ToString ( ) ;
168238 }
169239
240+
170241 private async Task GetTokenAsync ( )
171242 {
172243 using ( HttpClient client = new HttpClient ( ) )
0 commit comments