aboutsummaryrefslogtreecommitdiff
path: root/cmnext-cli/src/Site/CMNextEndpointDefintion.cs
blob: 60667791be1f80d77b494aa0089dd9344ae79d23 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Package: CMNext.Cli
* File: Program.cs 
*
* CMNext.Cli is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* CMNext.Cli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with CMNext.Cli. If not, see http://www.gnu.org/licenses/.
*/


using RestSharp;

using System;
using System.Net;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using VNLib.Utils.Extensions;
using VNLib.Utils.Logging;
using VNLib.Net.Rest.Client.Construction;

using CMNext.Cli.Exceptions;
using CMNext.Cli.Security;
using System.Diagnostics;

namespace CMNext.Cli.Site
{
    public interface ICMNextEndpointMap
    {
        string ChannelPath { get; }

        string PostPath { get; }

        string ContentPath { get; }

        string LoginPath { get; }
    }
    

    public sealed class CMNextEndpointDefintion(ICMNextEndpointMap Endpoints, IAuthAdapter Auth, ILogProvider Logger) : IRestEndpointDefinition
    {
        public void BuildRequest(IRestSiteAdapter site, IRestEndpointBuilder builder)
        {
            builder.WithEndpoint<ListChannelRequest>()
                .WithUrl(Endpoints.ChannelPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithAccessDeniedHandler("You do not have the required permissions to list channels. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<GetChannelRequest>()
                .WithUrl(Endpoints.ChannelPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithQuery("id", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to get a channel. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<SetChannelRequest>()
                .WithUrl(Endpoints.ChannelPath)
                .AcceptJson()
                .WithMethod(Method.Patch)
                .WithBody(r => r.Channel)
                .WithAccessDeniedHandler("You do not have the required permissions to update a channel. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<CreateChannelRequest>()
                .WithUrl(Endpoints.ChannelPath)
                .AcceptJson()
                .WithMethod(Method.Post)
                .WithBody(r => r.Channel)
                .WithAccessDeniedHandler("You do not have the required permissions to create a channel. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<DeleteChannelRequest>()
                .WithUrl(Endpoints.ChannelPath)
                .AcceptJson()
                .WithMethod(Method.Delete)
                .WithQuery("channel", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to delete a channel. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            //Setup post endpoints
            builder.WithEndpoint<ListPostMetaRequest>()
                .WithUrl(Endpoints.PostPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithQuery("channel", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to list all posts. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<GetPostMetaRequest>()
                .WithUrl(Endpoints.PostPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("post", p => p.PostId)
                .WithAccessDeniedHandler("You do not have the required permissions to get a post. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<SetPostMetaRequest>()
                .WithUrl(Endpoints.PostPath)
                .AcceptJson()
                .WithMethod(Method.Patch)
                .WithBody(r => r.Post)
                .WithQuery("channel", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to modify a post. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<CreatePostMetaRequest>()
                .WithUrl(Endpoints.PostPath)
                .AcceptJson()
                .WithMethod(Method.Post)
                .WithBody(r => r.Post)
                .WithQuery("channel", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to create a post. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<DeletePostMetaRequest>()               
                .WithUrl(Endpoints.PostPath)
                .AcceptJson()
                .WithMethod(Method.Delete)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("post", p => p.PostId)
                .WithAccessDeniedHandler("You do not have the required permissions to delete a post. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            //Setup content endpoints
            builder.WithEndpoint<ListContentRequest>()                
                .WithUrl(Endpoints.ContentPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithQuery("channel", p => p.ChannelId)
                .WithAccessDeniedHandler("You do not have the required permissions to list all content. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<SetContentMetaRequest>()               
               .WithUrl(Endpoints.ContentPath)
               .AcceptJson()
               .WithMethod(Method.Patch)
               .WithQuery("channel", p => p.ChannelId)
               .WithBody(r => r.Content)
               .WithAccessDeniedHandler("You do not have the required permissions to modify content metadata. Access denied")
               .WithAuth(Auth)
               .WithLogger(Logger);

            builder.WithEndpoint<GetContentLinkRequest>()
                .WithUrl(Endpoints.ContentPath)
                .AcceptJson()
                .WithMethod(Method.Get)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("id", p => p.ContentId)
                .WithQuery("getlink", "true")
                .WithAccessDeniedHandler("You do not have the required permissions to get content. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<DeleteContentRequest>()
                .WithUrl(Endpoints.ContentPath)
                .AcceptJson()
                .WithMethod(Method.Delete)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("id", p => p.ContentId)
                .WithAccessDeniedHandler("You do not have the required permissions to delete content. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<DeleteBulkContentRequest>()
                .WithUrl(Endpoints.ContentPath)
                .AcceptJson()
                .WithMethod(Method.Delete)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("ids", p => string.Join(',', p.ContentIds))
                .WithAccessDeniedHandler("You do not have the required permissions to delete content. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            builder.WithEndpoint<UploadFileRequest>()
                .WithUrl(Endpoints.ContentPath)
                .AcceptJson()
                .WithMethod(Method.Put)
                .WithQuery("channel", p => p.ChannelId)
                .WithQuery("id", p => p.ContentId!) //Allowed to be null, it will be ignored
                .WithHeader("X-Content-Name", p => p.Name)
                .WithModifier((r, req) => req.AddFile("file", r.LocalFile.FullName))    //Add the file from its fileinfo
                .WithAccessDeniedHandler("You do not have the required permissions to upload content. Access denied")
                .WithAuth(Auth)
                .WithLogger(Logger);

            //Setup server poke endpoint
        }
    }

    

    internal static class EndpointExtensions
    {
        /// <summary>
        /// Specifes that the desired response Content-Type is of application/json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRestRequestBuilder<T> AcceptJson<T>(this IRestRequestBuilder<T> builder)
        {
            return builder.WithHeader("Accept", "application/json");
        }

        public static IRestRequestBuilder<T> WithAccessDeniedHandler<T>(this IRestRequestBuilder<T> builder, string message)
        {
            return builder.OnResponse((_, res) =>
            {
                if (res.StatusCode == HttpStatusCode.Forbidden)
                {
                    throw new CMNextPermissionException(message);
                }
            });
        }

        public static IRestRequestBuilder<T> WithBody<T, TBody>(this IRestRequestBuilder<T> builder, Func<T, TBody> body) where TBody : class 
        {
            return builder.WithModifier((t, req) => req.AddJsonBody(body(t)));
        }

        public static IRestRequestBuilder<T> WithLogger<T>(this IRestRequestBuilder<T> builder, ILogProvider logger)
        {
            builder.WithModifier((t, req) =>
            {
                Debug.Assert(req.CookieContainer != null);
                string[] cookies = req.CookieContainer!.GetAllCookies().Select(c => $"{c.Name}={c.Value}").ToArray();
                string cookie = string.Join("\n", cookies);

                //List all headers
                string[] headers = req.Parameters.Where(p => p.Type == ParameterType.HttpHeader)
                                        .Select(p => $"{p.Name}: {p.Value}")
                                        .ToArray();

                string h = string.Join("\n", headers);

                logger.Verbose("Sending: {0} {1} HTTP/1.1\n{2}\n{3}\n{4}", req.Method, req.Resource, h, cookie, t);
            });

            builder.OnResponse((_, res) => 
            {
                string[] cookies = res.Cookies!.Select(c => $"{c.Name}={c.Value}").ToArray();
                string cookie = string.Join("\n", cookies);

                //list response headers
                string[]? headers = res.Headers?.Select(h => $"{h.Name}: {h.Value}").ToArray();
                string h = string.Join("\n", headers ?? []);


                logger.Verbose("Received: {0} {1} {2} -> {3} bytes \n{4}\n{5}\n{6}",
                    res.Request.Resource,
                    (int)res.StatusCode, 
                    res.StatusCode.ToString(), 
                    res.RawBytes?.Length,
                    h,
                    cookie, 
                    res.Content
                );
            });

            return builder;
        }

        /// <summary>
        /// Specifies the authentication adapter for the endpoint
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buider"></param>
        /// <param name="adapter">The auth adapter to set for the endpoint</param>
        /// <returns></returns>
        public static IRestRequestBuilder<T> WithAuth<T>(this IRestRequestBuilder<T> buider, IAuthAdapter adapter)
        {
            //Specify adapter for desired endpoint
            adapter.SetModifiersForEndpoint(buider);
            return buider;
        }
        
        public static PendingRequest<T> BeginRequest<T>(this IRestSiteAdapter site, T request) 
            => new (site, request);

        public sealed class PendingRequest<T>(IRestSiteAdapter Adapter, T request)
        {

            private readonly LinkedList<Action<T>> _beforeExecChain = new();

            public PendingRequest<T> BeforeRequest(Action<T> beforeRequest)
            {
                _beforeExecChain.AddLast(beforeRequest);
                return this;
            }

            public Task<RestResponse> ExecAsync(CancellationToken cancellation)
            {
                _beforeExecChain.TryForeach(p => p.Invoke(request));
                return Adapter.ExecuteAsync(request, cancellation);
            }

            public Task<RestResponse<TJson>> ExecAsync<TJson>(CancellationToken cancellation)
            {
                return Adapter.ExecuteAsync<T, TJson>(request, cancellation);
            }
        }
    }
}