update api

This commit is contained in:
JMARyA 2024-09-15 01:59:14 +02:00
parent 8a8462e1ea
commit 700b620c76
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -50,12 +50,14 @@ class API {
return resp.body; return resp.body;
} }
// /items
Future<List<dynamic>> getItems() async { Future<List<dynamic>> getItems() async {
var resp = jsonDecode(await getRequest("$instance/items")); var resp = jsonDecode(await getRequest("$instance/items"));
var lst = resp["items"]; var lst = resp["items"];
return lst as List<dynamic>; return lst as List<dynamic>;
} }
// /item/<item>/<variant>/unique?<field>
Future<List<String>> getUniqueField( Future<List<String>> getUniqueField(
String item, String variant, String field) async { String item, String variant, String field) async {
var resp = jsonDecode( var resp = jsonDecode(
@ -83,13 +85,40 @@ class API {
jsonDecode(await getRequest("$instance/transaction/$id"))); jsonDecode(await getRequest("$instance/transaction/$id")));
} }
Future<List<Transaction>> 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<dynamic>;
return resp.map((x) => Transaction(x)).toList();
}
Future<List<Transaction>> 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<dynamic>;
return resp.map((x) => Transaction(x)).toList();
}
// /item/<item_id>/inventory?<origin>
Future<List<Transaction>> getInventory(String item) async { Future<List<Transaction>> getInventory(String item) async {
// todo : add origin param
var resp = jsonDecode(await getRequest("$instance/item/$item/inventory")) var resp = jsonDecode(await getRequest("$instance/item/$item/inventory"))
as List<dynamic>; as List<dynamic>;
return resp.map((x) => Transaction(x)).toList(); return resp.map((x) => Transaction(x)).toList();
} }
// /item/<item_id>/<variant_id>/inventory
Future<List<Transaction>> getInventoryOfVariant( Future<List<Transaction>> getInventoryOfVariant(
String item, String variant) async { String item, String variant) async {
var resp = var resp =
@ -99,6 +128,7 @@ class API {
return resp.map((x) => Transaction(x)).toList(); return resp.map((x) => Transaction(x)).toList();
} }
// /supply
Future<String> supplyItem(String item, String variant, String price, Future<String> supplyItem(String item, String variant, String price,
String? origin, String? location) async { String? origin, String? location) async {
if (origin!.isEmpty) { if (origin!.isEmpty) {
@ -117,17 +147,18 @@ class API {
"location": location "location": location
}); });
var resp = jsonDecode(req); var resp = jsonDecode(req);
print(resp);
return resp["uuid"]; return resp["uuid"];
} }
// /demand
Future<void> consumeItem( Future<void> consumeItem(
String transaction, String destination, String price) async { String transaction, String destination, String price) async {
await postRequest("$instance/demand", await postRequest("$instance/demand",
{"uuid": transaction, "destination": destination, "price": price}); {"uuid": transaction, "destination": destination, "price": price});
} }
// /item/<item_id>/<variant_id>/stat
Future<ItemVariantStat> getStat(String item, String variant) async { Future<ItemVariantStat> getStat(String item, String variant) async {
return ItemVariantStat( return ItemVariantStat(
jsonDecode(await getRequest("$instance/item/$item/$variant/stat"))); jsonDecode(await getRequest("$instance/item/$item/$variant/stat")));
@ -136,6 +167,102 @@ class API {
String getImageURL(String item) { String getImageURL(String item) {
return "$instance/$item/image"; return "$instance/$item/image";
} }
// /item/<item_id>/<variant_id>/price_history?<origin>
Future<List<Price>> 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<dynamic>;
return resp.map((x) => Price(x)).toList();
}
// Flows
// /flows
Future<Map<String, FlowInfo>> getFlows() async {
var resp =
jsonDecode(await getRequest("$instance/flows")) as Map<String, dynamic>;
return resp.map((key, value) => MapEntry(key, FlowInfo(value)));
}
// /flow/<id>/info
Future<FlowInfo> getFlowInfo(String id) async {
return FlowInfo(jsonDecode(await getRequest("$instance/flow/$id/info")));
}
// /flow/<id>
Future<String> startFlow(String id, {List<String>? input}) async {
var resp =
jsonDecode(await postRequest("$instance/flow/$id", {"input": input}));
return resp["uuid"];
}
// /flow/<id>/end
Future<List<String>?> endFlow(String id, {Map<String, int>? produced}) async {
var resp = jsonDecode(
await postRequest("$instance/$id/end", {"produced": produced}));
if (produced != null) {
return resp["produced"] as List<String>;
}
return null;
}
// /flow/<id>/continue
Future<String> continueFlow(String id, {List<String>? input}) async {
var resp = jsonDecode(
await postRequest("$instance/flow/$id/continue", {"input": input}));
return resp["uuid"];
}
Future<List<Transaction>> getExpiredItems() async {
var resp = jsonDecode(await getRequest("$instance/items/expired"))
as List<dynamic>;
return resp.map((x) => Transaction(x)).toList();
}
Future<List<MinItem>> getItemsUnderMin() async {
var resp =
jsonDecode(await getRequest("$instance/items/min")) as List<dynamic>;
return resp.map((x) => MinItem(x)).toList();
}
Future<void> moveTransaction(String id, String new_location) async {
jsonDecode(await postRequest(
"$instance/transaction/$id/move", {"to": new_location}));
}
Future<Location> 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<String> depends;
late String? next;
late List<String>? produces;
FlowInfo(Map<String, dynamic> json) {
id = json["id"];
name = json["name"];
depends = json["depends"] as List<String>;
next = json["next"];
produces = json["produces"];
}
} }
class Item { class Item {
@ -225,3 +352,34 @@ class ItemVariantStat {
total_price = json["total_price"]; total_price = json["total_price"];
} }
} }
class Location {
late String id;
late String name;
late String? parent;
late LocationCondition? conditions;
Location(Map<String, dynamic> json) {
id = json["id"];
name = json["name"];
parent = json["parent"];
conditions = json["conditions"] != null ? (json["conditions"]) : null;
}
}
class LocationCondition {
late String temperature;
LocationCondition(Map<String, dynamic> json) {
temperature = json["temperature"];
}
}
class MinItem {
late String item_variant;
late int need;
MinItem(Map<String, dynamic> json) {
item_variant = json["item_variant"];
need = json["need"];
}
}