Skip to content

Commit

Permalink
+ RestRequest.Multipart* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Nov 16, 2023
1 parent 96b0337 commit e17d381
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
49 changes: 49 additions & 0 deletions BytecodeApi.Rest/RestRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BytecodeApi.Data;
using BytecodeApi.Extensions;
using System.Collections;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
Expand Down Expand Up @@ -147,6 +149,46 @@ public RestRequest FormUrlEncodedContent<T>(Dictionary<string, T?> content)
HttpContent = new FormUrlEncodedContent(content.ToDictionary(property => property.Key, property => property.Value?.ToString() ?? ""));
return this;
}
/// <summary>
/// Includes a file into the multipart request.
/// </summary>
/// <param name="name">The name of the HTTP content.</param>
/// <param name="file">A <see cref="Blob" /> with the file to send.</param>
/// <param name="contentType">The content type of <paramref name="file" />.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public RestRequest MultipartFileContent(string name, Blob file, string contentType)
{
Check.ObjectDisposed<RestClient>(RestClient.Disposed);
Check.ArgumentNull(name);
Check.ArgumentNull(file);
Check.ArgumentNull(contentType);

ByteArrayContent fileContent = new(file.Content);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
AddMultipartContent().Add(fileContent, name, file.Name);
return this;
}
/// <summary>
/// Includes a <see cref="string" /> into the multipart request.
/// </summary>
/// <param name="name">The name of the HTTP content.</param>
/// <param name="content">The <see cref="string" /> content to be sent with the multipart request.</param>
/// <param name="contentType">The content type of <paramref name="content" />.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public RestRequest MultipartStringContent(string name, string content, string contentType)
{
Check.ObjectDisposed<RestClient>(RestClient.Disposed);
Check.ArgumentNull(name);
Check.ArgumentNull(content);
Check.ArgumentNull(contentType);

AddMultipartContent().Add(new StringContent(content, Encoding.UTF8, contentType), name);
return this;
}

/// <summary>
/// Sends the request and reads the response <see cref="string" />.
Expand Down Expand Up @@ -242,4 +284,11 @@ private static string AddQueryString(string url, string key, string value)

return $"{url}{(url.Contains('?') ? '&' : '?')}{UrlEncoder.Default.Encode(key)}={UrlEncoder.Default.Encode(value)}{fragment}";
}
private MultipartFormDataContent AddMultipartContent()
{
Check.InvalidOperation(HttpContent is null or MultipartFormDataContent, "Cannot mix multipart content with non-multipart content.");

HttpContent ??= new MultipartFormDataContent();
return (MultipartFormDataContent)HttpContent;
}
}
24 changes: 24 additions & 0 deletions BytecodeApi/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ public static string ToCSharpName(this Type type, TypeNaming namingConvention)
}
}
/// <summary>
/// Returns the value of a static field.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <param name="field">The <see cref="FieldInfo" /> to retrieve the value from.</param>
/// <returns>
/// The value of a static field.
/// </returns>
public static T? GetValue<T>(this FieldInfo field)
{
return field.GetValue<T>(null);
}
/// <summary>
/// Returns the value of a field supported by a given <see cref="object" />.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
Expand All @@ -172,6 +184,18 @@ public static string ToCSharpName(this Type type, TypeNaming namingConvention)
return (T?)field.GetValue(obj);
}
/// <summary>
/// Returns the value of a static property.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <param name="property">The <see cref="PropertyInfo" /> to retrieve the value from.</param>
/// <returns>
/// The value of a static property.
/// </returns>
public static T? GetValue<T>(this PropertyInfo property)
{
return property.GetValue<T>(null);
}
/// <summary>
/// Returns the value of a property supported by a given <see cref="object" />.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
Expand Down

0 comments on commit e17d381

Please sign in to comment.