source: binary-improvements2/WebServer/src/WebAPI/APIs/Item.cs@ 432

Last change on this file since 432 was 432, checked in by alloc, 19 months ago

Added API "item"

File size: 1.7 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3using Webserver.Permissions;
4
5namespace Webserver.WebAPI.APIs {
6 [UsedImplicitly]
7 internal class Item : AbsRestApi {
8 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject ("name");
9 private static readonly byte[] jsonKeyLocalizedName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("localizedName");
10 private static readonly byte[] jsonKeyIsBlock = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isBlock");
11
12 private readonly byte[] allItemsSerialized;
13
14 public Item (Web _parent) {
15 JsonWriter writer = new JsonWriter ();
16
17 writer.WriteBeginArray ();
18
19 int written = 0;
20 for (int i = 0; i < ItemClass.list.Length; i++) {
21 ItemClass item = ItemClass.list [i];
22 if (item == null) {
23 continue;
24 }
25
26 if (written > 0) {
27 writer.WriteValueSeparator ();
28 }
29
30 written++;
31
32 string name = item.Name;
33 string localizedName = item.GetLocalizedItemName ();
34 bool isBlock = item.IsBlock ();
35
36 writer.WriteRaw (jsonKeyName);
37 writer.WriteString (name);
38
39 writer.WriteRaw (jsonKeyLocalizedName);
40 writer.WriteString (localizedName);
41
42 writer.WriteRaw (jsonKeyIsBlock);
43 writer.WriteBoolean (isBlock);
44
45 writer.WriteEndObject ();
46 }
47
48 writer.WriteEndArray ();
49
50 allItemsSerialized = writer.ToUtf8ByteArray ();
51 }
52
53 protected override void HandleRestGet (RequestContext _context) {
54 PrepareEnvelopedResult (out JsonWriter writer);
55 writer.WriteRaw (allItemsSerialized);
56 SendEnvelopedResult (_context, ref writer);
57 }
58
59 public override int DefaultPermissionLevel () {
60 return AdminWebModules.PermissionLevelGuest;
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.