-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IHttpContentSerializer and GraphHttpContentSerializer
(cherry picked from commit b6f7553)
- Loading branch information
1 parent
b1a98be
commit 83e1221
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
ShopifySharp/Infrastructure/Serialization/Http/GraphHttpContentSerializer.cs
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,38 @@ | ||
#if NETSTANDARD2_0 | ||
using System.Text; | ||
#else | ||
using System.Buffers; | ||
using System.Net.Http.Headers; | ||
#endif | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
|
||
namespace ShopifySharp.Infrastructure.Serialization.Http; | ||
|
||
public class GraphHttpContentSerializer(JsonSerializerOptions jsonSerializerOptions) : IHttpContentSerializer | ||
{ | ||
public HttpContent SerializeGraphRequest(RequestUri requestUri, GraphRequest graphRequest) | ||
{ | ||
var jsonData = new Dictionary<string, object> | ||
{ | ||
{ "query", graphRequest.Query }, | ||
{ "variables", graphRequest.Variables }, | ||
}; | ||
|
||
#if NET6_0_OR_GREATER | ||
var bufferWriter = new ArrayBufferWriter<byte>(); | ||
using var utf8JsonWriter = new Utf8JsonWriter(bufferWriter); | ||
|
||
JsonSerializer.Serialize(utf8JsonWriter, jsonData, jsonSerializerOptions); | ||
|
||
var content = new ReadOnlyMemoryContent(bufferWriter.WrittenMemory); | ||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
#else | ||
var jsonStr = JsonSerializer.Serialize(jsonData, jsonSerializerOptions); | ||
const string mediaType = "application/json"; | ||
var content = new StringContent(jsonStr, Encoding.UTF8, mediaType); | ||
#endif | ||
return content; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
ShopifySharp/Infrastructure/Serialization/Http/IHttpContentSerializer.cs
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,8 @@ | ||
using System.Net.Http; | ||
|
||
namespace ShopifySharp.Infrastructure.Serialization.Http; | ||
|
||
public interface IHttpContentSerializer | ||
{ | ||
public HttpContent SerializeGraphRequest(RequestUri requestUri, GraphRequest graphRequest); | ||
} |