Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions Activout.RestClient.Test/MultipartFormDataContentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,8 @@ await client.SendFormInForm(new FormModel
MyString = "foobar"
}, new[]
{
new Part<string>
{
Content = "foo",
FileName = "foo.txt"
},
new Part<string>
{
Content = "bar",
FileName = "bar.txt"
}
new Part(Content: "foo", FileName: "foo.txt"),
new Part(Content: "bar", FileName: "bar.txt")
});

// Assert
Expand Down
10 changes: 4 additions & 6 deletions Activout.RestClient/IRestClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#nullable disable
namespace Activout.RestClient
namespace Activout.RestClient;

public interface IRestClientFactory
{
public interface IRestClientFactory
{
IRestClientBuilder CreateBuilder();
}
IRestClientBuilder CreateBuilder();
}
28 changes: 14 additions & 14 deletions Activout.RestClient/Implementation/RequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
Expand All @@ -16,6 +15,8 @@

namespace Activout.RestClient.Implementation;

internal record HttpContentPart(HttpContent Content, string Name, string? FileName);

internal class RequestHandler
{
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1
Expand Down Expand Up @@ -210,7 +211,7 @@ private void PrepareRequestMessage(HttpRequestMessage request)
var routeParams = new Dictionary<string, object>();
var queryParams = new List<string>();
var formParams = new List<KeyValuePair<string, string>>();
var partParams = new List<Part<HttpContent>>();
var partParams = new List<HttpContentPart>();
var cancellationToken = GetParams(args, routeParams, queryParams, formParams, headers, partParams);

var requestUriString = ExpandTemplate(routeParams);
Expand Down Expand Up @@ -251,7 +252,7 @@ private void PrepareRequestMessage(HttpRequestMessage request)
}

private static MultipartFormDataContent CreateMultipartFormDataContent(
IEnumerable<Part<HttpContent>> partParams)
IEnumerable<HttpContentPart> partParams)
{
var content = new MultipartFormDataContent();
foreach (var part in partParams)
Expand Down Expand Up @@ -293,7 +294,7 @@ private CancellationToken GetParams(
List<string> queryParams,
List<KeyValuePair<string, string>> formParams,
List<KeyValuePair<string, object>> headers,
List<Part<HttpContent>> parts)
List<HttpContentPart> parts)
{
var cancellationToken = CancellationToken.None;

Expand Down Expand Up @@ -430,7 +431,7 @@ private CancellationToken GetParams(
return cancellationToken;
}

private IEnumerable<Part<HttpContent>> GetPartNameAndHttpContent(PartParamAttribute partAttribute,
private IEnumerable<HttpContentPart> GetPartNameAndHttpContent(PartParamAttribute partAttribute,
string parameterName,
object? rawValue)
{
Expand All @@ -439,19 +440,17 @@ private IEnumerable<Part<HttpContent>> GetPartNameAndHttpContent(PartParamAttrib

if (rawValue is Part part)
{
rawValue = part.InternalContent;
rawValue = part.Content;
partName = part.Name;
fileName = part.FileName;
}

if (rawValue is { })
if (rawValue is not null)
{
yield return new Part<HttpContent>
{
Content = GetPartHttpContent(partAttribute, rawValue),
Name = partName ?? partAttribute.Name ?? parameterName,
FileName = fileName ?? partAttribute.FileName
};
yield return new HttpContentPart(
Content: GetPartHttpContent(partAttribute, rawValue),
Name: partName ?? partAttribute.Name ?? parameterName,
FileName: fileName ?? partAttribute.FileName);
}
}

Expand Down Expand Up @@ -481,7 +480,8 @@ private static HttpContent GetHttpContent(ISerializer serializer, object? value,
}


private async Task<object?> SendRequestAndHandleResponse(HttpRequestMessage request, CancellationToken cancellationToken)
private async Task<object?> SendRequestAndHandleResponse(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var response = await SendRequest(request, cancellationToken);
return await HandleResponse(request, response);
Expand Down
20 changes: 2 additions & 18 deletions Activout.RestClient/Part.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#nullable disable
namespace Activout.RestClient
{
public class Part
{
internal object InternalContent { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
}
namespace Activout.RestClient;

public class Part<T> : Part
{
public T Content
{
get => (T)InternalContent;
set => InternalContent = value;
}
}
}
public record Part(object Content, string? Name = null, string? FileName = null);