Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ability to override options used to serialize, use default op… #290

Merged
merged 15 commits into from
Nov 12, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Serialize_ShouldSucceed()

var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(model, true, true);
byte[] jsonBytes = serialization.Serialize(model, new ApiClientSerializationOptions(true, true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -42,7 +42,7 @@ public void Serialize_ShouldNotCamelCaseBindVars_WhenSerializingPostCursorBody()
};
var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, true, true);
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -65,7 +65,7 @@ public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBod

var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, true, true);
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -88,7 +88,7 @@ public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostCursorBody()
};
var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, true, true);
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -109,7 +109,7 @@ public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostTransactionBody()

var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, true, true);
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand Down
7 changes: 2 additions & 5 deletions arangodb-net-standard/ApiClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ protected T DeserializeJsonFromStream<T>(Stream stream)
}
}

protected byte[] GetContent<T>(T item, bool useCamelCasePropertyNames, bool ignoreNullValues)
protected byte[] GetContent<T>(T item, ApiClientSerializationOptions serializationOptions)
{
try
{
return _serialization.Serialize<T>(
item,
useCamelCasePropertyNames,
ignoreNullValues);
return _serialization.Serialize(item, serializationOptions);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public AqlFunctionApiClient(IApiClientTransport transport, IApiClientSerializati
/// <returns></returns>
public virtual async Task<PostAqlFunctionResponse> PostAqlFunctionAsync(PostAqlFunctionBody body)
{
var content = GetContent(body, true, true);
var content = GetContent(body, new ApiClientSerializationOptions(true, true));

using (var response = await _transport.PostAsync(_apiPath, content))
{
Expand Down
5 changes: 3 additions & 2 deletions arangodb-net-standard/AuthApi/AuthApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public virtual async Task<JwtTokenResponse> GetJwtTokenAsync(string username, st
/// </summary>
/// <param name="body">Object containing username and password.</param>
/// <returns>Object containing the encoded JWT token value.</returns>
public virtual async Task<JwtTokenResponse> GetJwtTokenAsync(JwtTokenRequestBody body)
public virtual async Task<JwtTokenResponse> GetJwtTokenAsync(
JwtTokenRequestBody body)
{
byte[] content = GetContent(body, true, false);
byte[] content = GetContent(body, new ApiClientSerializationOptions(true, false));
using (var response = await _client.PostAsync("/_open/auth", content))
{
if (response.IsSuccessStatusCode)
Expand Down
14 changes: 9 additions & 5 deletions arangodb-net-standard/CollectionApi/CollectionApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ public CollectionApiClient(IApiClientTransport transport, IApiClientSerializatio
_transport = transport;
}

public virtual async Task<PostCollectionResponse> PostCollectionAsync(PostCollectionBody body, PostCollectionQuery options = null)
public virtual async Task<PostCollectionResponse> PostCollectionAsync(
PostCollectionBody body,
PostCollectionQuery options = null)
{
string uriString = _collectionApiPath;
if (options != null)
{
uriString += "?" + options.ToQueryString();
}
var content = GetContent(body, true, true);
var content = GetContent(body, new ApiClientSerializationOptions(true, true));
using (var response = await _transport.PostAsync(uriString, content))
{
var stream = await response.Content.ReadAsStreamAsync();
Expand Down Expand Up @@ -189,7 +191,7 @@ public virtual async Task<GetCollectionPropertiesResponse> GetCollectionProperti
/// <returns></returns>
public virtual async Task<RenameCollectionResponse> RenameCollectionAsync(string collectionName, RenameCollectionBody body)
{
var content = GetContent(body, true, false);
var content = GetContent(body, new ApiClientSerializationOptions(true, false));
using (var response = await _transport.PutAsync(_collectionApiPath + "/" + WebUtility.UrlEncode(collectionName) + "/rename", content))
{
if (response.IsSuccessStatusCode)
Expand Down Expand Up @@ -228,9 +230,11 @@ public virtual async Task<GetCollectionRevisionResponse> GetCollectionRevisionAs
/// <param name="collectionName"></param>
/// <param name="body"></param>
/// <returns></returns>
public virtual async Task<PutCollectionPropertyResponse> PutCollectionPropertyAsync(string collectionName, PutCollectionPropertyBody body)
public virtual async Task<PutCollectionPropertyResponse> PutCollectionPropertyAsync(
string collectionName,
PutCollectionPropertyBody body)
{
var content = GetContent(body, true, true);
var content = GetContent(body, new ApiClientSerializationOptions(true, true));
using (var response = await _transport.PutAsync(_collectionApiPath + "/" + collectionName + "/properties", content))
{
if (response.IsSuccessStatusCode)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ArangoDBNetStandard.CollectionApi.Models;
using ArangoDBNetStandard.Serialization;
using System.Threading.Tasks;

namespace ArangoDBNetStandard.CollectionApi
Expand Down
5 changes: 3 additions & 2 deletions arangodb-net-standard/CursorApi/CursorApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public virtual async Task<CursorResponse<T>> PostCursorAsync<T>(
/// </summary>
/// <param name="postCursorBody">Object encapsulating options and parameters of the query.</param>
/// <returns></returns>
public virtual async Task<CursorResponse<T>> PostCursorAsync<T>(PostCursorBody postCursorBody)
public virtual async Task<CursorResponse<T>> PostCursorAsync<T>(
PostCursorBody postCursorBody)
{
var content = GetContent(postCursorBody, true, true);
var content = GetContent(postCursorBody, new ApiClientSerializationOptions(true, true));
using (var response = await _client.PostAsync(_cursorApiPath, content))
{
if (response.IsSuccessStatusCode)
Expand Down
4 changes: 3 additions & 1 deletion arangodb-net-standard/CursorApi/ICursorApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ArangoDBNetStandard.CursorApi.Models;
using ArangoDBNetStandard.Serialization;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down Expand Up @@ -37,7 +38,8 @@ Task<CursorResponse<T>> PostCursorAsync<T>(
/// </summary>
/// <param name="postCursorBody">Object encapsulating options and parameters of the query.</param>
/// <returns></returns>
Task<CursorResponse<T>> PostCursorAsync<T>(PostCursorBody postCursorBody);
Task<CursorResponse<T>> PostCursorAsync<T>(
PostCursorBody postCursorBody);

/// <summary>
/// Deletes an existing cursor and frees the resources associated with it.
Expand Down
2 changes: 1 addition & 1 deletion arangodb-net-standard/DatabaseApi/DatabaseApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public DatabaseApiClient(IApiClientTransport client, IApiClientSerialization ser
/// <returns></returns>
public virtual async Task<PostDatabaseResponse> PostDatabaseAsync(PostDatabaseBody request)
{
var content = GetContent(request, true, true);
var content = GetContent(request, new ApiClientSerializationOptions(true, true));
using (var response = await _client.PostAsync(_databaseApiPath, content))
{
if (response.IsSuccessStatusCode)
Expand Down
47 changes: 32 additions & 15 deletions arangodb-net-standard/DocumentApi/DocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ public DocumentApiClient(IApiClientTransport client, IApiClientSerialization ser
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
string collectionName,
T document,
PostDocumentsQuery query = null)
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);
if (query != null)
{
uriString += "?" + query.ToQueryString();
}
var content = GetContent(document, false, false);
var content = GetContent(document, serializationOptions);
using (var response = await _client.PostAsync(uriString, content))
{
if (response.IsSuccessStatusCode)
Expand All @@ -82,18 +85,21 @@ public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
/// <param name="collectionName"></param>
/// <param name="documents"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentsResponse<T>> PostDocumentsAsync<T>(
string collectionName,
IList<T> documents,
PostDocumentsQuery query = null)
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);
if (query != null)
{
uriString += "?" + query.ToQueryString();
}
var content = GetContent(documents, false, false);
var content = GetContent(documents, serializationOptions);
using (var response = await _client.PostAsync(uriString, content))
{
if (response.IsSuccessStatusCode)
Expand All @@ -119,18 +125,21 @@ public virtual async Task<PostDocumentsResponse<T>> PostDocumentsAsync<T>(
/// <param name="collectionName"></param>
/// <param name="documents"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PutDocumentsResponse<T>> PutDocumentsAsync<T>(
string collectionName,
IList<T> documents,
PutDocumentsQuery query = null)
PutDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);
if (query != null)
{
uri += "?" + query.ToQueryString();
}
var content = GetContent(documents, false, false);
var content = GetContent(documents, serializationOptions);
using (var response = await _client.PutAsync(uri, content))
{
if (response.IsSuccessStatusCode)
Expand All @@ -157,20 +166,22 @@ public virtual async Task<PutDocumentsResponse<T>> PutDocumentsAsync<T>(
/// <typeparam name="T"></typeparam>
/// <param name="documentId"></param>
/// <param name="doc"></param>
/// <param name="opts"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PutDocumentResponse<T>> PutDocumentAsync<T>(
string documentId,
T doc,
PutDocumentQuery opts = null)
PutDocumentQuery opts = null,
ApiClientSerializationOptions serializationOptions = null)
{
ValidateDocumentId(documentId);
string uri = _docApiPath + "/" + documentId;
if (opts != null)
{
uri += "?" + opts.ToQueryString();
}
var content = GetContent(doc, false, false);
var content = GetContent(doc, serializationOptions);
using (var response = await _client.PutAsync(uri, content))
{
if (response.IsSuccessStatusCode)
Expand Down Expand Up @@ -251,7 +262,7 @@ public virtual async Task<List<T>> GetDocumentsAsync<T>(
{
string uri = $"{_docApiPath}/{WebUtility.UrlEncode(collectionName)}?onlyget=true";

var content = GetContent(selectors, false, true);
var content = GetContent(selectors, new ApiClientSerializationOptions(false, true));

using (var response = await _client.PutAsync(uri, content))
{
Expand Down Expand Up @@ -394,7 +405,7 @@ public virtual async Task<DeleteDocumentsResponse<T>> DeleteDocumentsAsync<T>(
{
uri += "?" + query.ToQueryString();
}
var content = GetContent(selectors, false, false);
var content = GetContent(selectors, new ApiClientSerializationOptions(false, false));
using (var response = await _client.DeleteAsync(uri, content))
{
if (response.IsSuccessStatusCode)
Expand Down Expand Up @@ -434,18 +445,21 @@ public virtual async Task<DeleteDocumentsResponse<T>> DeleteDocumentsAsync<T>(
/// <param name="collectionName"></param>
/// <param name="patches"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PatchDocumentsResponse<U>> PatchDocumentsAsync<T, U>(
string collectionName,
IList<T> patches,
PatchDocumentsQuery query = null)
PatchDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);
if (query != null)
{
uri += "?" + query.ToQueryString();
}
var content = GetContent(patches, false, false);
var content = GetContent(patches, serializationOptions);
using (var response = await _client.PatchAsync(uri, content))
{
if (response.IsSuccessStatusCode)
Expand Down Expand Up @@ -510,19 +524,22 @@ public virtual async Task<PatchDocumentResponse<U>> PatchDocumentAsync<T, U>(
/// <param name="documentId"></param>
/// <param name="body"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PatchDocumentResponse<U>> PatchDocumentAsync<T, U>(
string documentId,
T body,
PatchDocumentQuery query = null)
PatchDocumentQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
ValidateDocumentId(documentId);
string uriString = _docApiPath + "/" + documentId;
if (query != null)
{
uriString += "?" + query.ToQueryString();
}
var content = GetContent(body, false, false);
var content = GetContent(body, serializationOptions);
using (var response = await _client.PatchAsync(uriString, content))
{
if (response.IsSuccessStatusCode)
Expand Down
Loading