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;
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 {
50 await Response.SendResponse(new BadRequestException());
51 return;
52 }
53
54 ContentResponse Content = await Request.DecodeDataAsync();
55
56 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Form))
57 {
58 await Response.SendResponse(new BadRequestException());
59 return;
60 }
61
62 if (!Form.TryGetValue("AutomaticBackups", out object Obj) || !CommonTypes.TryParse(Obj.ToString().Trim(), out bool AutomaticBackups))
63 {
64 Response.StatusCode = 400;
65 Response.StatusMessage = "Bad Request";
66 Response.ContentType = PlainTextCodec.DefaultContentType;
67 await Response.Write("Automatic backups value invalid.");
68 await Response.SendResponse();
69 return;
70 }
71
72 if (!Form.TryGetValue("BackupTime", out Obj) || !TimeSpan.TryParse(Obj.ToString().Trim(), out TimeSpan BackupTime))
73 {
74 Response.StatusCode = 400;
75 Response.StatusMessage = "Bad Request";
76 Response.ContentType = PlainTextCodec.DefaultContentType;
77 await Response.Write("Backup time invalid.");
78 await Response.SendResponse();
79 return;
80 }
81
82 if (!Form.TryGetValue("KeepDays", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepDays) || KeepDays < 0)
83 {
84 Response.StatusCode = 400;
85 Response.StatusMessage = "Bad Request";
86 Response.ContentType = PlainTextCodec.DefaultContentType;
87 await Response.Write("Invalid number of days specified.");
88 await Response.SendResponse();
89 return;
90 }
91
92 if (!Form.TryGetValue("KeepMonths", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepMonths) || KeepMonths < 0)
93 {
94 Response.StatusCode = 400;
95 Response.StatusMessage = "Bad Request";
96 Response.ContentType = PlainTextCodec.DefaultContentType;
97 await Response.Write("Invalid number of months specified.");
98 await Response.SendResponse();
99 return;
100 }
101
102 if (!Form.TryGetValue("KeepYears", out Obj) || !int.TryParse(Obj.ToString().Trim(), out int KeepYears) || KeepYears < 0)
103 {
104 Response.StatusCode = 400;
105 Response.StatusMessage = "Bad Request";
106 Response.ContentType = PlainTextCodec.DefaultContentType;
107 await Response.Write("Invalid number of years specified.");
108 await Response.SendResponse();
109 return;
110 }
111
112 await Export.SetAutomaticBackupsAsync(AutomaticBackups);
113 await Export.SetBackupTimeAsync(BackupTime);
114 await Export.SetKeepDaysAsync(KeepDays);
115 await Export.SetKeepMonthsAsync(KeepMonths);
116 await Export.SetKeepYearsAsync(KeepYears);
117
118 Response.StatusCode = 200;
119 Response.StatusMessage = "OK";
120 await Response.SendResponse();
121 }
122 }
123}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Static class managing data export.
Definition: Export.cs:18
static async Task SetKeepDaysAsync(long Value)
For how many days backups are kept.
Definition: Export.cs:391
static async Task SetKeepMonthsAsync(long Value)
For how many months the monthly backups are kept.
Definition: Export.cs:417
static async Task SetAutomaticBackupsAsync(bool Value)
If automatic backups are activated
Definition: Export.cs:365
static async Task SetBackupTimeAsync(TimeSpan Value)
Time of day to start performing backups.
Definition: Export.cs:469
static async Task SetKeepYearsAsync(long Value)
For how many years the yearly backups are kept.
Definition: Export.cs:443
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
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:3868
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:22
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
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.