Skip to content

Commit b300e38

Browse files
committed
Try printing response (does not work)
1 parent e502a68 commit b300e38

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Text;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCoreClientExample.Models;
4+
5+
namespace Example;
6+
7+
/// <summary>
8+
/// Prints the specified people, their assigned todo-items, and its tags.
9+
/// </summary>
10+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
11+
internal sealed class PeopleMessageFormatter
12+
{
13+
public static void PrintPeople(PersonCollectionResponseDocument? peopleResponse)
14+
{
15+
string message = WritePeople(peopleResponse);
16+
Console.WriteLine(message);
17+
}
18+
19+
private static string WritePeople(PersonCollectionResponseDocument? peopleResponse)
20+
{
21+
if (peopleResponse == null)
22+
{
23+
return "The HTTP response hasn't changed, so no response body was returned.";
24+
}
25+
26+
var builder = new StringBuilder();
27+
builder.AppendLine($"Found {peopleResponse.Data.Count} people:");
28+
29+
foreach (PersonDataInResponse person in peopleResponse.Data)
30+
{
31+
WritePerson(person, peopleResponse.Included!, builder);
32+
}
33+
34+
return builder.ToString();
35+
}
36+
37+
private static void WritePerson(PersonDataInResponse person, ICollection<DataInResponse> includes, StringBuilder builder)
38+
{
39+
ToManyTodoItemInResponse assignedTodoItems = person.Relationships!.AssignedTodoItems!;
40+
41+
builder.AppendLine($" Person {person.Id}: {person.Attributes!.DisplayName} with {assignedTodoItems.Data!.Count} assigned todo-items:");
42+
WriteRelatedTodoItems(assignedTodoItems.Data, includes, builder);
43+
}
44+
45+
private static void WriteRelatedTodoItems(IEnumerable<TodoItemIdentifier> todoItemIdentifiers, ICollection<DataInResponse> includes, StringBuilder builder)
46+
{
47+
foreach (TodoItemIdentifier todoItemIdentifier in todoItemIdentifiers)
48+
{
49+
TodoItemDataInResponse includedTodoItem = includes.OfType<TodoItemDataInResponse>().Single(include => include.Id == todoItemIdentifier.Id);
50+
ToManyTagInResponse tags = includedTodoItem.Relationships!.Tags!;
51+
52+
builder.AppendLine($" TodoItem {includedTodoItem.Id}: {includedTodoItem.Attributes!.Description} with {tags.Data!.Count} tags:");
53+
WriteRelatedTags(tags.Data, includes, builder);
54+
}
55+
}
56+
57+
private static void WriteRelatedTags(IEnumerable<TagIdentifier> tagIdentifiers, ICollection<DataInResponse> includes, StringBuilder builder)
58+
{
59+
foreach (TagIdentifier tagIdentifier in tagIdentifiers)
60+
{
61+
TagDataInResponse includedTag = includes.OfType<TagDataInResponse>().Single(include => include.Id == tagIdentifier.Id);
62+
builder.AppendLine($" Tag {includedTag.Id}: {includedTag.Attributes!.Name}");
63+
}
64+
}
65+
}

src/Examples/OpenApiLiblabClientExample/output/csharp/Example/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This file was generated by liblab | https://liblab.com/
22

3+
using Example;
34
using JsonApiDotNetCoreClientExample;
45
using JsonApiDotNetCoreClientExample.Config;
56
using Environment = JsonApiDotNetCoreClientExample.Http.Environment;
@@ -14,4 +15,7 @@
1415

1516
var response = await client.People.GetPersonCollectionAsync();
1617

18+
// Commented out because it does not work due to broken inheritance.
19+
//PeopleMessageFormatter.PrintPeople(response);
20+
1721
Console.WriteLine(response);

0 commit comments

Comments
 (0)