private static HttpContent GetContent(WebApiRestRequest request) { if (request.File != null) { var formData = new MultipartFormDataContent(); foreach (var parameter in request.Parameters) { var content = new StringContent(parameter.Value.ToString()); formData.Add(content, $"\"{parameter.Key}\""); } var byteContent = new ByteArrayContent(request.File); formData.Add(byteContent, "\"file\"", $"\"{request.FileName}\""); return(formData); } if (request.Body == null) { return(null); } var body = TrelloConfiguration.Serializer.Serialize(request.Body); var jsonContent = new StringContent(body); jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(jsonContent); }
/// <summary> /// Creates a general request using a collection of objects and an additional parameter to /// generate the resource string and an object to supply additional parameters. /// </summary> /// <param name="endpoint">The method endpoint the request calls.</param> /// <param name="parameters">A list of paramaters to include in the request.</param> /// <returns>An IRestRequest instance which can be sent to an IRestClient.</returns> public IRestRequest Create(string endpoint, IDictionary <string, object> parameters = null) { var request = new WebApiRestRequest { Resource = endpoint }; if (parameters != null) { foreach (var parameter in parameters) { if (parameter.Key == RestFile.ParameterKey) { var rf = (RestFile)parameter.Value; if (rf.FilePath != null) { request.AddFile(parameter.Key, rf.FilePath, rf.FileName); } else { request.AddFile(parameter.Key, rf.ContentBytes, rf.FileName); } } else { request.AddParameter(parameter.Key, parameter.Value); } } } return(request); }
private string GetFullResource(WebApiRestRequest request) { if (request.File != null) { return($"{_baseUri}/{request.Resource}"); } return($"{_baseUri}/{request.Resource}?{string.Join("&", request.Parameters.Select(kvp => $"{kvp.Key}={UrlEncode(kvp.Value)}").ToList())}"); }
private string GetFullResource(WebApiRestRequest request) { TrelloConfiguration.Log.Info($"Sending: {request.Method} {request.Resource}"); if (request.File != null) { return($"{_baseUri}/{request.Resource}"); } return($"{_baseUri}/{request.Resource}?{string.Join("&", request.Parameters.Select(kvp => $"{kvp.Key}={UrlEncode(kvp.Value)}"))}"); }
private static HttpContent GetContent(WebApiRestRequest request) { if (request.File != null) { var formData = new MultipartFormDataContent(); foreach (var parameter in request.Parameters) { var content = new StringContent(parameter.Value.ToString()); formData.Add(content, $"\"{parameter.Key}\""); } if (request.FilePath != null) { var fileStream = File.Open(request.FilePath, FileMode.Open); var fileInfo = new FileInfo(request.FilePath); formData.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", "as" + fileInfo.Extension)); formData.Add(new StringContent("mimeType"), "image/" + fileInfo.Extension.Remove(0, 1)); formData.Add(new StringContent("name"), request.FileName); TrelloConfiguration.Log.Debug($"\tContent: {formData}"); } else { var byteContent = new ByteArrayContent(request.File); formData.Add(byteContent, "\"file\"", $"\"{request.FileName}\""); TrelloConfiguration.Log.Debug($"\tContent: {formData}"); } return(formData); } if (request.Body == null) { return(null); } var body = TrelloConfiguration.Serializer.Serialize(request.Body); var jsonContent = new StringContent(body); jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); TrelloConfiguration.Log.Debug($"\tContent: {body}"); return(jsonContent); }