Skip to content

Commit

Permalink
fix FormParam
Browse files Browse the repository at this point in the history
  • Loading branch information
SilkageNet committed Feb 16, 2023
1 parent 0c89ed1 commit 886b1ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
27 changes: 16 additions & 11 deletions Runtime/Param/FormParam.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
using System.Text;
using UnityEngine.Networking;

namespace PinealCtx.RestClient.Param
{
public class FormParam : IParam
{
public FormParam(IMultipartFormSection section) => Section = section;

public FormParam(string name, string value) : this(new MultipartFormDataSection(name, value))
{
}

public FormParam(string name, byte[] value, string fileName = "", string contentType = "") : this(
new MultipartFormFileSection(name, value, fileName, contentType))
public FormParam(string fieldName, string value, Encoding encoding = null)
{
FieldName = fieldName;
Value = value;
Encoding = encoding ?? Encoding.UTF8;
}

public FormParam(string name, FormFile file) : this(name, file.Data, file.FileName, file.ContentType)
public FormParam(string fieldName, byte[] data, string fileName = null, string mimeType = null)
{
FieldName = fieldName;
Data = data;
FileName = fileName;
MimeType = mimeType;
}

public IMultipartFormSection Section { get; set; }
public string FieldName { get; }
public string Value { get; }
public Encoding Encoding { get; }
public byte[] Data { get; }
public string FileName { get; }
public string MimeType { get; }

public ParamType ParamType() => Param.ParamType.Form;
}
Expand All @@ -28,6 +34,5 @@ public class FormFile
{
public byte[] Data { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
}
}
58 changes: 24 additions & 34 deletions Runtime/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Newtonsoft.Json;
using PinealCtx.RestClient.Attribute;
using PinealCtx.RestClient.Param;
using UnityEngine;
using UnityEngine.Networking;
using RestHeaderProperty = PinealCtx.RestClient.Attribute.RestHeaderProperty;

namespace PinealCtx.RestClient
{
Expand All @@ -29,7 +31,7 @@ public RestRequest(string path) : this(Method.Get, path)

public List<HeaderParam> Headers { get; set; } = new();

public List<FormParam> Forms { get; set; } = new();
public WWWForm Forms { get; set; }

public BodyParam Body { get; set; }

Expand All @@ -51,7 +53,15 @@ public RestRequest WithParam(IParam param)
Body = bodyParam;
break;
case FormParam formParam:
Forms.Add(formParam);
if (formParam.Data == null)
{
Forms.AddField(formParam.FieldName, formParam.Value, formParam.Encoding);
}
else
{
Forms.AddBinaryData(formParam.FieldName, formParam.Data, formParam.MimeType);
}

break;
}

Expand Down Expand Up @@ -80,24 +90,6 @@ public RestRequest WithHeader(string name, string value)
return this;
}

public RestRequest WithForm(string name, string value)
{
Forms.Add(new FormParam(name, value));
return this;
}

public RestRequest WithForm(string name, FormFile file)
{
Forms.Add(new FormParam(name, file));
return this;
}

public RestRequest WithForm(string name, byte[] value)
{
Forms.Add(new FormParam(name, value));
return this;
}

public RestRequest WithBody(string contentType, byte[] body)
{
Body = new BodyParam(contentType, body);
Expand Down Expand Up @@ -137,16 +129,17 @@ public RestRequest WithParamObject(object obj)
case RestFormProperty formProperty:
{
hasForm = true;
Forms ??= new WWWForm();
var value = prop.GetValue(obj);
if (value.GetType() == typeof(FormFile))
if (value == null) continue;
if (value.GetType() != typeof(FormFile))
{
WithForm(formProperty.Name, value as FormFile);
}
else
{
WithForm(formProperty.Name, Object2String(value));
Forms.AddField(formProperty.Name, Object2String(value));
continue;
}

if (value is FormFile formFile) Forms.AddBinaryData(formProperty.Name, formFile.Data);

break;
}
}
Expand Down Expand Up @@ -219,13 +212,10 @@ public UploadHandlerRaw BuildBody()
if (Method == Method.Get) return null;
if (Body == null)
{
if (Forms.Count == 0) return null;
var sections = Forms.Select(f => f.Section).ToList();
var boundary = UnityWebRequest.GenerateBoundary();
var data = UnityWebRequest.SerializeFormSections(sections, boundary);
if (data == null) return null;
var contentType =
$"multipart/form-data; boundary={Encoding.UTF8.GetString(boundary, 0, boundary.Length)}";
if (Forms == null) return null;
var data = Forms.data;
if (data.Length == 0) return null;
Forms.headers.TryGetValue("Content-Type", out var contentType);
Body = new BodyParam(contentType, data);
}

Expand All @@ -239,7 +229,7 @@ public void Dispose()
Queries?.Clear();
Queries = null;
Headers?.Clear();
Forms?.Clear();
Forms = null;
Headers = null;
Body = null;
}
Expand Down

0 comments on commit 886b1ac

Please sign in to comment.