aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/PaginatedResource.Api.cs
blob: 9a7b141a9fda0fc05c58a6682eb9284baff2a3b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Text.Json;
using System.Text.Json.Serialization;
using Refit;

namespace ColorPalette
{
    public sealed partial class PaginatedResource
    {
        private IResourceApi _ResourceApi;
        private IResourceApi ResourceApi
        {
            get
            {
                _ResourceApi ??= Service?.Create<IResourceApi>();
                return _ResourceApi;
            }
        }

        internal override void ResetConnection()
        {
            _ResourceApi = null;
        }

        internal interface IResourceApi
        {
            [Get("/{path}?page={page}")]
            Task<ResourcePage> RefreshAsync(
                [AliasAs("path")] string path, [AliasAs("page")] int page);

            [Post("/{path}")]
            Task<JsonElement> AddAsync(
                [AliasAs("path")] string path, [Body] ResourceData body);

            [Put("/{path}/{id}")]
            Task<JsonElement> UpdateAsync(
                [AliasAs("path")] string path, [AliasAs("id")] int id, [Body] ResourceData body);

            [Delete("/{path}/{id}")]
            Task<string> RemoveAsync(
                [AliasAs("path")] string path, [AliasAs("id")] int id);
        }

        internal class ResourcePage
        {
            [JsonPropertyName("page")]
            public int CurrentPage { get; set; }

            [JsonPropertyName("per_page")]
            public int ResourcesPerPage { get; set; }

            [JsonPropertyName("total_pages")]
            public int TotalPages { get; set; }

            [JsonPropertyName("total")]
            public int TotalResources { get; set; }

            [JsonPropertyName("data")]
            public List<JsonElement> Data { get; set; }
        }
    }
}