Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices.WindowsRuntime;
- using System.Threading.Tasks;
- using ClientSoft.Postall.ApplicationCore.Enums;
- using ClientSoft.Postall.Services.Helpers;
- using Windows.Storage;
- using Windows.Storage.Streams;
- using Windows.Web.Http;
- namespace ClientSoft.Postall.ApplicationCore.Models
- {
- public class RequestMessage
- {
- #region Fields
- private readonly HttpMethod _httpMethod;
- private readonly Func<Task<IHttpContent>> _httpContentFactory;
- private readonly string _url;
- #endregion
- public RequestMessage(string url, HttpMethod method)
- {
- if (string.IsNullOrWhiteSpace(url))
- {
- throw new ArgumentException("Invalid url", nameof(url));
- }
- _url = url;
- _httpMethod = method;
- }
- public RequestMessage(string url, HttpMethod method, string content, string mediaType = "plain/text")
- : this(url, method)
- {
- _httpContentFactory = () =>
- Task.FromResult<IHttpContent>(new HttpStringContent(content, UnicodeEncoding.Utf8, mediaType));
- }
- public RequestMessage(string url, HttpMethod method, StorageFile file) : this(url, method)
- {
- _httpContentFactory = async () =>
- {
- var bytes = await file.GetBytesFromFileAsync();
- var content = new HttpBufferContent(bytes.AsBuffer());
- content.Headers.Add("Content-Type", "application/octet-stream");
- return content;
- };
- }
- public RequestMessage(string url, HttpMethod method, IList<UrlEncodedItem> items) : this(url, method)
- {
- _httpContentFactory = () =>
- {
- var list = new List<KeyValuePair<string, string>>();
- for (int i = 0; i < items.Count; i++)
- {
- if (items[i].IsValid())
- {
- list.Add(new KeyValuePair<string, string>(items[i].Key, items[i].Value));
- }
- }
- return Task.FromResult<IHttpContent>(list.Count > 0 ? new HttpFormUrlEncodedContent(list) : default);
- };
- }
- public RequestMessage(string url, HttpMethod method, IList<FormDataItem> items) : this(url, method)
- {
- _httpContentFactory = async () =>
- {
- var multipartContent = new HttpMultipartFormDataContent();
- int parametersCount = 0;
- for (int i = 0; i < items.Count; i++)
- {
- var item = items[i];
- if (item.IsValid())
- {
- switch (item.ContentType)
- {
- case MultipartContentType.Text:
- multipartContent.Add(
- new HttpStringContent(item.Value, UnicodeEncoding.Utf8), item.Key);
- parametersCount++;
- break;
- case MultipartContentType.File:
- var streamContent = await item.FileWrapper.OriginalFile.OpenAsync(FileAccessMode.Read);
- multipartContent.Add(
- new HttpStreamContent(streamContent), item.Key, item.FileWrapper.Name);
- parametersCount++;
- break;
- }
- }
- }
- return parametersCount > 0 ? multipartContent : default;
- };
- }
- public async Task<HttpRequestMessage> GetRequestMessage()
- {
- var requestMessage = new HttpRequestMessage
- {
- RequestUri = new Uri(_url),
- Method = _httpMethod
- };
- if (_httpContentFactory != null)
- {
- requestMessage.Content = await _httpContentFactory();
- }
- return requestMessage;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement