From 700b620c76c52a5507d2456bfb6cbb205b8a0f21 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sun, 15 Sep 2024 01:59:14 +0200 Subject: [PATCH] update api --- lib/api.dart | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 1 deletion(-) diff --git a/lib/api.dart b/lib/api.dart index a3cd5f0..a00664b 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -50,12 +50,14 @@ class API { return resp.body; } + // /items Future> getItems() async { var resp = jsonDecode(await getRequest("$instance/items")); var lst = resp["items"]; return lst as List; } + // /item///unique? Future> getUniqueField( String item, String variant, String field) async { var resp = jsonDecode( @@ -83,13 +85,40 @@ class API { jsonDecode(await getRequest("$instance/transaction/$id"))); } + Future> getTransactionsOfLocation(String location, + {bool recursive = true}) async { + var url = "$instance/location/$location/inventory"; + + if (recursive) { + url += "?recursive=true"; + } + + var resp = jsonDecode(await getRequest(url)) as List; + + return resp.map((x) => Transaction(x)).toList(); + } + + Future> getConsumedItems(String item, String variant, + {String? destination}) async { + var url = "$instance/item/$item/$variant/demand"; + if (destination != null) { + url += "?destination=$destination"; + } + var resp = jsonDecode(await getRequest(url)) as List; + + return resp.map((x) => Transaction(x)).toList(); + } + + // /item//inventory? Future> getInventory(String item) async { + // todo : add origin param var resp = jsonDecode(await getRequest("$instance/item/$item/inventory")) as List; return resp.map((x) => Transaction(x)).toList(); } + // /item///inventory Future> getInventoryOfVariant( String item, String variant) async { var resp = @@ -99,6 +128,7 @@ class API { return resp.map((x) => Transaction(x)).toList(); } + // /supply Future supplyItem(String item, String variant, String price, String? origin, String? location) async { if (origin!.isEmpty) { @@ -117,17 +147,18 @@ class API { "location": location }); var resp = jsonDecode(req); - print(resp); return resp["uuid"]; } + // /demand Future consumeItem( String transaction, String destination, String price) async { await postRequest("$instance/demand", {"uuid": transaction, "destination": destination, "price": price}); } + // /item///stat Future getStat(String item, String variant) async { return ItemVariantStat( jsonDecode(await getRequest("$instance/item/$item/$variant/stat"))); @@ -136,6 +167,102 @@ class API { String getImageURL(String item) { return "$instance/$item/image"; } + + // /item///price_history? + Future> getPriceHistory(String item, String variant, + {String? origin}) async { + var url = "$instance/item/$item/$variant/price_history"; + + if (origin != null) { + url += "?origin=$origin"; + } + + var resp = jsonDecode(await getRequest(url)) as List; + + return resp.map((x) => Price(x)).toList(); + } + + // Flows + + // /flows + Future> getFlows() async { + var resp = + jsonDecode(await getRequest("$instance/flows")) as Map; + + return resp.map((key, value) => MapEntry(key, FlowInfo(value))); + } + + // /flow//info + Future getFlowInfo(String id) async { + return FlowInfo(jsonDecode(await getRequest("$instance/flow/$id/info"))); + } + + // /flow/ + Future startFlow(String id, {List? input}) async { + var resp = + jsonDecode(await postRequest("$instance/flow/$id", {"input": input})); + + return resp["uuid"]; + } + + // /flow//end + Future?> endFlow(String id, {Map? produced}) async { + var resp = jsonDecode( + await postRequest("$instance/$id/end", {"produced": produced})); + + if (produced != null) { + return resp["produced"] as List; + } + + return null; + } + + // /flow//continue + Future continueFlow(String id, {List? input}) async { + var resp = jsonDecode( + await postRequest("$instance/flow/$id/continue", {"input": input})); + + return resp["uuid"]; + } + + Future> getExpiredItems() async { + var resp = jsonDecode(await getRequest("$instance/items/expired")) + as List; + return resp.map((x) => Transaction(x)).toList(); + } + + Future> getItemsUnderMin() async { + var resp = + jsonDecode(await getRequest("$instance/items/min")) as List; + + return resp.map((x) => MinItem(x)).toList(); + } + + Future moveTransaction(String id, String new_location) async { + jsonDecode(await postRequest( + "$instance/transaction/$id/move", {"to": new_location})); + } + + Future getLocation(String id) async { + var resp = jsonDecode(await getRequest("$instance/location/$id")); + return Location(resp); + } +} + +class FlowInfo { + late String id; + late String name; + late List depends; + late String? next; + late List? produces; + + FlowInfo(Map json) { + id = json["id"]; + name = json["name"]; + depends = json["depends"] as List; + next = json["next"]; + produces = json["produces"]; + } } class Item { @@ -225,3 +352,34 @@ class ItemVariantStat { total_price = json["total_price"]; } } + +class Location { + late String id; + late String name; + late String? parent; + late LocationCondition? conditions; + + Location(Map json) { + id = json["id"]; + name = json["name"]; + parent = json["parent"]; + conditions = json["conditions"] != null ? (json["conditions"]) : null; + } +} + +class LocationCondition { + late String temperature; + + LocationCondition(Map json) { + temperature = json["temperature"]; + } +} + +class MinItem { + late String item_variant; + late int need; + MinItem(Map json) { + item_variant = json["item_variant"]; + need = json["need"]; + } +}