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

Several adjustments needed #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Kontent.Ai.Management.Tests/CodeSamples/Readme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using Xunit;

Expand Down Expand Up @@ -73,7 +74,7 @@ public void CreateManagementClient()
{
EnvironmentId = "cbbe2d5c-17c6-0128-be26-e997ba7c1619",
ApiKey = "ew0...1eo"
});
}, new HttpClient());
}

[Fact]
Expand Down
15 changes: 8 additions & 7 deletions Kontent.Ai.Management/ManagementClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Kontent.Ai.Management.Configuration;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Kontent.Ai.Management.Configuration;
using Kontent.Ai.Management.Models.Shared;
using Kontent.Ai.Management.Modules.ActionInvoker;
using Kontent.Ai.Management.Modules.HttpClient;
using Kontent.Ai.Management.Modules.ModelBuilders;
using Kontent.Ai.Management.Modules.ResiliencePolicy;
using Kontent.Ai.Management.Modules.UrlBuilder;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Kontent.Ai.Management;

Expand All @@ -27,7 +27,8 @@ public sealed partial class ManagementClient : IManagementClient
/// Initializes a new instance of the <see cref="ManagementClient"/> class for managing content of the specified environment.
/// </summary>
/// <param name="ManagementOptions">The settings of the Kontent.ai environment.</param>
public ManagementClient(ManagementOptions ManagementOptions)
/// <param name="httpClient"></param>
public ManagementClient(ManagementOptions ManagementOptions, System.Net.Http.HttpClient httpClient)
{
ArgumentNullException.ThrowIfNull(ManagementOptions);

Expand All @@ -49,7 +50,7 @@ public ManagementClient(ManagementOptions ManagementOptions)

_urlBuilder = new EndpointUrlBuilder(ManagementOptions);
_actionInvoker = new ActionInvoker(
new ManagementHttpClient(new DefaultResiliencePolicyProvider(ManagementOptions.MaxRetryAttempts), ManagementOptions.EnableResilienceLogic),
new ManagementHttpClient(new Modules.HttpClient.HttpClient(httpClient), new DefaultResiliencePolicyProvider(ManagementOptions.MaxRetryAttempts), ManagementOptions.EnableResilienceLogic),
new MessageCreator(ManagementOptions.ApiKey));
_modelProvider = ManagementOptions.ModelProvider ?? new ModelProvider();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Kontent.Ai.Management.Models.Shared;
using Kontent.Ai.Management.Models.Shared;
using Newtonsoft.Json;

namespace Kontent.Ai.Management.Models.Items;

/// <summary>
/// Represents the content item create model.
/// </summary>
public sealed class ContentItemCreateModel
public class ContentItemCreateModel
{
/// <summary>
/// Gets or sets the name of the content item.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Kontent.Ai.Management.Modules.ActionInvoker;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using Kontent.Ai.Management.Modules.ActionInvoker;
using Newtonsoft.Json;

namespace Kontent.Ai.Management.Models.Shared;

/// <summary>
/// Represents identifier of asset with renditions.
/// </summary>
[JsonConverter(typeof(AssetWithRenditionsReferenceConverter))]
public sealed class AssetWithRenditionsReference
public class AssetWithRenditionsReference
{
private readonly IList<Reference> _renditions;

Expand Down Expand Up @@ -60,4 +60,4 @@ public AssetWithRenditionsReference(Reference assetReference, IEnumerable<Refere
_assetReference = assetReference;
_renditions = renditionReferences?.ToList() ?? new List<Reference>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Kontent.Ai.Management.Models.Shared;
using System;
using Kontent.Ai.Management.Models.Shared;
using Kontent.Ai.Management.Modules.ActionInvoker;
using Newtonsoft.Json;
using System;

namespace Kontent.Ai.Management.Models.Types.Elements;

Expand All @@ -27,7 +27,7 @@ public abstract class ElementMetadataBase
/// Gets or sets the element's internal ID.
/// </summary>
[JsonProperty("id")]
public Guid Id { get; private set; }
public Guid Id { get; set; }

/// <summary>
/// Gets or sets the element's codename.
Expand Down
14 changes: 9 additions & 5 deletions Kontent.Ai.Management/Modules/ActionInvoker/ActionInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Kontent.Ai.Management.Modules.HttpClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Kontent.Ai.Management.Modules.HttpClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Kontent.Ai.Management.Modules.ActionInvoker;

Expand All @@ -19,12 +19,12 @@ internal class ActionInvoker : IActionInvoker
private readonly JsonSerializerSettings _serializeSettings = new()
{
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter> { new DecimalObjectConverter(), new StringEnumConverter() }
Converters = [new DecimalObjectConverter(), new StringEnumConverter()]
};

private readonly JsonSerializerSettings _deserializeSettings = new()
{
Converters = new List<JsonConverter> { new DynamicObjectJsonConverter() }
Converters = [new DynamicObjectJsonConverter()]
};


Expand Down Expand Up @@ -60,6 +60,10 @@ public async Task<TResponse> InvokeMethodAsync<TPayload, TResponse>(string endpo
public async Task<TResponse> InvokeReadOnlyMethodAsync<TResponse>(string endpointUrl, HttpMethod method, Dictionary<string, string> headers = null)
{
var response = await _cmHttpClient.SendAsync(_messageCreator, endpointUrl, method, null, headers);
if (response == null)
{
return default;
}

return await ReadResultAsync<TResponse>(response);
}
Expand Down
9 changes: 9 additions & 0 deletions Kontent.Ai.Management/Modules/HttpClient/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ namespace Kontent.Ai.Management.Modules.HttpClient;

internal class HttpClient : IHttpClient
{
public HttpClient()
{

}
public HttpClient(System.Net.Http.HttpClient httpClient)
{
_baseClient = httpClient;
}

private readonly System.Net.Http.HttpClient _baseClient = new();

public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) => await _baseClient.SendAsync(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public async Task<HttpResponseMessage> SendAsync(
return response;
}

if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}

var responseContent = response.Content;

throw responseContent != null ?
Expand Down