Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices.WindowsRuntime;
  4. using System.Threading.Tasks;
  5.  
  6. using ClientSoft.Postall.ApplicationCore.Enums;
  7. using ClientSoft.Postall.Services.Helpers;
  8.  
  9. using Windows.Storage;
  10. using Windows.Storage.Streams;
  11. using Windows.Web.Http;
  12.  
  13. namespace ClientSoft.Postall.ApplicationCore.Models
  14. {
  15.     public class RequestMessage
  16.     {
  17.         #region Fields
  18.         private readonly HttpMethod _httpMethod;
  19.         private readonly Func<Task<IHttpContent>> _httpContentFactory;
  20.         private readonly string _url;
  21.         #endregion
  22.  
  23.  
  24.         public RequestMessage(string url, HttpMethod method)
  25.         {
  26.             if (string.IsNullOrWhiteSpace(url))
  27.             {
  28.                 throw new ArgumentException("Invalid url", nameof(url));
  29.             }
  30.  
  31.             _url = url;
  32.             _httpMethod = method;
  33.         }
  34.  
  35.         public RequestMessage(string url, HttpMethod method, string content, string mediaType = "plain/text")
  36.             : this(url, method)
  37.         {
  38.             _httpContentFactory = () =>
  39.                 Task.FromResult<IHttpContent>(new HttpStringContent(content, UnicodeEncoding.Utf8, mediaType));
  40.         }
  41.  
  42.         public RequestMessage(string url, HttpMethod method, StorageFile file) : this(url, method)
  43.         {
  44.             _httpContentFactory = async () =>
  45.             {
  46.                 var bytes = await file.GetBytesFromFileAsync();
  47.                 var content = new HttpBufferContent(bytes.AsBuffer());
  48.                 content.Headers.Add("Content-Type", "application/octet-stream");
  49.  
  50.                 return content;
  51.             };
  52.         }
  53.  
  54.         public RequestMessage(string url, HttpMethod method, IList<UrlEncodedItem> items) : this(url, method)
  55.         {
  56.             _httpContentFactory = () =>
  57.             {
  58.                 var list = new List<KeyValuePair<string, string>>();
  59.                 for (int i = 0; i < items.Count; i++)
  60.                 {
  61.                     if (items[i].IsValid())
  62.                     {
  63.                         list.Add(new KeyValuePair<string, string>(items[i].Key, items[i].Value));
  64.                     }
  65.                 }
  66.  
  67.                 return Task.FromResult<IHttpContent>(list.Count > 0 ? new HttpFormUrlEncodedContent(list) : default);
  68.             };
  69.         }
  70.  
  71.         public RequestMessage(string url, HttpMethod method, IList<FormDataItem> items) : this(url, method)
  72.         {
  73.             _httpContentFactory = async () =>
  74.             {
  75.                 var multipartContent = new HttpMultipartFormDataContent();
  76.                 int parametersCount = 0;
  77.                 for (int i = 0; i < items.Count; i++)
  78.                 {
  79.                     var item = items[i];
  80.  
  81.                     if (item.IsValid())
  82.                     {
  83.                         switch (item.ContentType)
  84.                         {
  85.                             case MultipartContentType.Text:
  86.                                 multipartContent.Add(
  87.                                     new HttpStringContent(item.Value, UnicodeEncoding.Utf8), item.Key);
  88.                                 parametersCount++;
  89.                                 break;
  90.  
  91.                             case MultipartContentType.File:
  92.                                 var streamContent = await item.FileWrapper.OriginalFile.OpenAsync(FileAccessMode.Read);
  93.                                 multipartContent.Add(
  94.                                         new HttpStreamContent(streamContent), item.Key, item.FileWrapper.Name);
  95.                                 parametersCount++;
  96.                                 break;
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 return parametersCount > 0 ? multipartContent : default;
  102.             };
  103.         }
  104.  
  105.         public async Task<HttpRequestMessage> GetRequestMessage()
  106.         {
  107.             var requestMessage = new HttpRequestMessage
  108.             {
  109.                 RequestUri = new Uri(_url),
  110.                 Method = _httpMethod
  111.             };
  112.  
  113.             if (_httpContentFactory != null)
  114.             {
  115.                 requestMessage.Content = await _httpContentFactory();
  116.             }
  117.  
  118.             return requestMessage;
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement