-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from Sevitas/112_new_class_format
112 new class format
- Loading branch information
Showing
20 changed files
with
1,607 additions
and
76 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
src/Kentico.Kontent.ModelGenerator.Core/ContentTypeJObjectHelper.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,54 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Kentico.Kontent.ModelGenerator.Core | ||
{ | ||
public static class ContentTypeJObjectHelper | ||
{ | ||
public static string GetElementIdFromContentType(JObject managementContentType, string elementCodename) | ||
{ | ||
if (!managementContentType.TryGetValue("elements", out var elements)) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', couldn't find {nameof(elements)}."); | ||
} | ||
|
||
if (elements is not { Type: JTokenType.Array }) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', {nameof(elements)} has invalid type."); | ||
} | ||
|
||
var element = elements.ToObject<List<JObject>>().FirstOrDefault(el => | ||
{ | ||
if (!el.TryGetValue("codename", out var codename)) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', couldn't find {nameof(codename)}."); | ||
} | ||
|
||
if (codename is not { Type: JTokenType.String }) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', {nameof(elements)} has invalid type."); | ||
} | ||
|
||
return codename.ToObject<string>() == elementCodename; | ||
}); | ||
|
||
if (element == null) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', missing element."); | ||
} | ||
|
||
if (!element.TryGetValue("id", out var elementId)) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', couldn't find {nameof(elementId)}."); | ||
} | ||
|
||
if (elementId is not { Type: JTokenType.String }) | ||
{ | ||
throw new InvalidIdException($"Unable to create a valid Id for '{elementCodename}', {nameof(elementId)} has invalid type."); | ||
} | ||
|
||
return elementId.ToObject<string>(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Kentico.Kontent.ModelGenerator.Core/IManagementClient.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,12 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Kentico.Kontent.ModelGenerator.Core.Configuration; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Kentico.Kontent.ModelGenerator.Core | ||
{ | ||
public interface IManagementClient | ||
{ | ||
Task<IList<JObject>> GetAllContentTypesAsync(CodeGeneratorOptions options); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Kentico.Kontent.ModelGenerator.Core/InvalidIdException.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,11 @@ | ||
using System; | ||
|
||
namespace Kentico.Kontent.ModelGenerator.Core | ||
{ | ||
public class InvalidIdException : Exception | ||
{ | ||
public InvalidIdException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/Kentico.Kontent.ModelGenerator.Core/ManagementClient.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,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Kentico.Kontent.ModelGenerator.Core.Configuration; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Kentico.Kontent.ModelGenerator.Core | ||
{ | ||
public class ManagementClient : IManagementClient | ||
{ | ||
private const int MilisecondsDelay = 1000; | ||
private readonly HttpClient _httpClient; | ||
|
||
public ManagementClient(HttpClient httpClient) | ||
{ | ||
_httpClient = httpClient; | ||
} | ||
|
||
public async Task<IList<JObject>> GetAllContentTypesAsync(CodeGeneratorOptions options) | ||
{ | ||
if (!options.ContentManagementApi) | ||
{ | ||
return null; | ||
} | ||
|
||
var contentTypes = new List<JObject>(); | ||
string continuationToken = null; | ||
do | ||
{ | ||
_httpClient.DefaultRequestHeaders.Clear(); | ||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", options.ManagementOptions.ApiKey); | ||
if (continuationToken != null) | ||
{ | ||
_httpClient.DefaultRequestHeaders.Add("x-continuation", continuationToken); | ||
} | ||
|
||
var response = await _httpClient.GetAsync(new Uri($"https://manage.kontent.ai/v2/projects/{options.ManagementOptions.ProjectId}/types"), HttpCompletionOption.ResponseContentRead); | ||
|
||
var responseStream = await response.Content.ReadAsStreamAsync(); | ||
var responseObject = await JObject.ReadFromAsync(new JsonTextReader(new StreamReader(responseStream))); | ||
|
||
continuationToken = responseObject["pagination"]["continuation_token"].ToObject<string>(); | ||
|
||
contentTypes.AddRange(responseObject["types"].ToObject<List<JObject>>()); | ||
|
||
await Task.Delay(MilisecondsDelay); | ||
} | ||
while (continuationToken != null); | ||
|
||
return contentTypes; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.