Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UpdateBackupSettings.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
7
9{
14 {
19 : base("/UpdateBackupSettings")
20 {
21 }
22
26 public override bool HandlesSubPaths => false;
27
31 public override bool UserSessions => true;
32
36 public bool AllowsPOST => true;
37
44 public async Task POST(HttpRequest Request, HttpResponse Response)
45 {
46 Gateway.AssertUserAuthenticated(Request, "Admin.Data.Backup");
47
48 if (!Request.HasData)
49 throw new BadRequestException();
50
51 object Obj = await Request.DecodeDataAsync();
52
53 if (!(Obj is Dictionary<string, object> Form))
54 throw new BadRequestException();
55
56 if (!Form.TryGetValue("AutomaticBackups", out Obj) || !CommonTypes.TryParse(Obj.ToString().Trim(), out bool AutomaticBackups))
57 {
58 Response.StatusCode = 400;
59 Response.StatusMessage = "Bad Request";
60 Response.ContentType = PlainTextCodec.DefaultContentType;
61 await Response.Write("Automatic backups value invalid.");
62 await Response.SendResponse();
63 return;
64 }
65
66 if (!Form.TryGetValue("BackupTime", out Obj) || !TimeSpan.TryParse(Obj.ToString().Trim(), out TimeSpan BackupTime))
67 {
68 Response.StatusCode = 400;
69 Response.StatusMessage = "Bad Request";
70 Response.ContentType = PlainTextCodec.DefaultContentType;
71 await Response.Write("Backup time invalid.");
72 await Response.SendResponse();
73 return;
74 }
75
76 if (!Form.TryGetValue("KeepDays", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepDays) || KeepDays < 0)
77 {
78 Response.StatusCode = 400;
79 Response.StatusMessage = "Bad Request";
80 Response.ContentType = PlainTextCodec.DefaultContentType;
81 await Response.Write("Invalid number of days specified.");
82 await Response.SendResponse();
83 return;
84 }
85
86 if (!Form.TryGetValue("KeepMonths", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepMonths) || KeepMonths < 0)
87 {
88 Response.StatusCode = 400;
89 Response.StatusMessage = "Bad Request";
90 Response.ContentType = PlainTextCodec.DefaultContentType;
91 await Response.Write("Invalid number of months specified.");
92 await Response.SendResponse();
93 return;
94 }
95
96 if (!Form.TryGetValue("KeepYears", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepYears) || KeepYears < 0)
97 {
98 Response.StatusCode = 400;
99 Response.StatusMessage = "Bad Request";
100 Response.ContentType = PlainTextCodec.DefaultContentType;
101 await Response.Write("Invalid number of years specified.");
102 await Response.SendResponse();
103 return;
104 }
105
106 await Export.SetAutomaticBackupsAsync(AutomaticBackups);
107 await Export.SetBackupTimeAsync(BackupTime);
108 await Export.SetKeepDaysAsync(KeepDays);
109 await Export.SetKeepMonthsAsync(KeepMonths);
110 await Export.SetKeepYearsAsync(KeepYears);
111
112 Response.StatusCode = 200;
113 Response.StatusMessage = "OK";
114 await Response.SendResponse();
115 }
116 }
117}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Static class managing data export.
Definition: Export.cs:19
static async Task SetKeepDaysAsync(long Value)
For how many days backups are kept.
Definition: Export.cs:392
static async Task SetKeepMonthsAsync(long Value)
For how many months the monthly backups are kept.
Definition: Export.cs:418
static async Task SetAutomaticBackupsAsync(bool Value)
If automatic backups are activated
Definition: Export.cs:366
static async Task SetBackupTimeAsync(TimeSpan Value)
Time of day to start performing backups.
Definition: Export.cs:470
static async Task SetKeepYearsAsync(long Value)
For how many years the yearly backups are kept.
Definition: Export.cs:444
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3041
Web Resource for updating backup settings.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
UpdateBackupSettings()
Web Resource for updating backup settings.
override bool UserSessions
If the resource uses user sessions.
override bool HandlesSubPaths
If the resource handles sub-paths.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
async Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
POST Interface for HTTP resources.