-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06677e9
commit 924b708
Showing
8 changed files
with
152 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Text; | ||
using Flurl.Util; | ||
|
||
namespace PlainHttp.Payloads; | ||
|
||
public class FormUrlEncodedPayload : IPayload | ||
{ | ||
private readonly object payload; | ||
|
||
public FormUrlEncodedPayload(object payload) | ||
{ | ||
this.payload = payload; | ||
} | ||
|
||
public HttpContent Serialize() | ||
{ | ||
// Already serialized | ||
if (this.payload is string stringPayload) | ||
{ | ||
return new StringContent( | ||
content: stringPayload, | ||
encoding: Encoding.UTF8, | ||
mediaType: "application/x-www-form-urlencoded" | ||
); | ||
} | ||
else | ||
{ | ||
var qp = new Flurl.QueryParamCollection(); | ||
|
||
foreach ((string key, object value) in this.payload.ToKeyValuePairs()) | ||
{ | ||
qp.AddOrReplace(key, value, false, Flurl.NullValueHandling.Ignore); | ||
} | ||
|
||
string serialized = qp.ToString(true); | ||
|
||
return new StringContent( | ||
content: serialized, | ||
encoding: Encoding.UTF8, | ||
mediaType: "application/x-www-form-urlencoded" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PlainHttp.Payloads; | ||
|
||
public interface IPayload | ||
{ | ||
HttpContent Serialize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace PlainHttp.Payloads; | ||
|
||
public class JsonPayload : IPayload | ||
{ | ||
private readonly object payload; | ||
private readonly JsonSerializerOptions? options; | ||
|
||
public JsonPayload(object payload) | ||
{ | ||
this.payload = payload; | ||
} | ||
|
||
public JsonPayload(object payload, JsonSerializerOptions options) : this(payload) | ||
{ | ||
this.options = options; | ||
} | ||
|
||
public HttpContent Serialize() | ||
{ | ||
string serialized; | ||
|
||
// Already serialized | ||
if (this.payload is string stringPayload) | ||
{ | ||
serialized = stringPayload; | ||
} | ||
else | ||
{ | ||
serialized = JsonSerializer.Serialize(this.payload, this.options); | ||
} | ||
|
||
return new StringContent( | ||
content: serialized, | ||
encoding: Encoding.UTF8, | ||
mediaType: "application/json" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
|
||
namespace PlainHttp.Payloads; | ||
|
||
public class XmlPayload : IPayload | ||
{ | ||
private readonly object payload; | ||
private readonly XmlWriterSettings? settings; | ||
|
||
public XmlPayload(object payload) | ||
{ | ||
this.payload = payload; | ||
} | ||
|
||
public XmlPayload(object payload, XmlWriterSettings settings) : this(payload) | ||
{ | ||
this.settings = settings; | ||
} | ||
|
||
public HttpContent Serialize() | ||
{ | ||
string serialized; | ||
|
||
// Already serialized | ||
if (this.payload is string stringPayload) | ||
{ | ||
serialized = stringPayload; | ||
} | ||
else | ||
{ | ||
XmlSerializer serializer = new XmlSerializer(this.payload.GetType()); | ||
StringBuilder result = new StringBuilder(); | ||
|
||
using (var writer = XmlWriter.Create(result, this.settings)) | ||
{ | ||
serializer.Serialize(writer, this.payload); | ||
} | ||
|
||
serialized = result.ToString(); | ||
} | ||
|
||
return new StringContent( | ||
content: serialized, | ||
encoding: Encoding.UTF8, | ||
mediaType: "text/xml" | ||
); | ||
} | ||
} |