import 'dart:convert'; import 'package:http/http.dart' as http; class API { // todo : rework with auth static String instance = "http://192.168.2.20:8080"; Future getRequest(String url) async { var resp = await http.get(Uri.parse(url), headers: { 'Access-Control-Allow-Origin': '*', }); return resp.body; } Future postRequest(String url, Map data) async { var resp = await http.post(Uri.parse(url), headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, body: jsonEncode(data)); return resp.body; } Future> getItems() async { var resp = jsonDecode(await getRequest("$instance/items")); var lst = resp["items"]; return lst as List; } Future> getUniqueField( String item, String variant, String field) async { var resp = jsonDecode( await getRequest("$instance/item/$item/$variant/unique?field=$field")); List ret = []; for (var e in resp as List) { ret.add(e); } return ret; } Future> getLocations() async { var resp = jsonDecode(await getRequest("$instance/locations")); return resp as List; } Future getItem(String item) async { return Item(jsonDecode(await getRequest("$instance/item/$item"))); } Future getTransaction(String id) async { return Transaction( jsonDecode(await getRequest("$instance/transaction/$id"))); } Future> getInventory(String item) async { var resp = jsonDecode(await getRequest("$instance/item/$item/inventory")) as List; return resp.map((x) => Transaction(x)).toList(); } Future> getInventoryOfVariant( String item, String variant) async { var resp = jsonDecode(await getRequest("$instance/item/$item/$variant/inventory")) as List; return resp.map((x) => Transaction(x)).toList(); } Future supplyItem(String item, String variant, String price, String? origin, String? location) async { if (origin!.isEmpty) { origin = null; } if (location!.isEmpty) { location = null; } var req = await postRequest("$instance/supply", { "item": item, "variant": variant, "price": price, "origin": origin, "location": location }); var resp = jsonDecode(req); print(resp); return resp["uuid"]; } Future consumeItem( String transaction, String destination, String price) async { await postRequest("$instance/demand", {"uuid": transaction, "destination": destination, "price": price}); } String getImageURL(String item) { return "$instance/$item/image"; } } class Item { late String id; late String name; late String category; late Map variants; Item(Map json) { id = json["uuid"]; name = json["name"]; category = json["category"]; variants = {}; json["variants"].forEach((key, value) { variants[key] = ItemVariant(value); }); } } class ItemVariant { late String item; late String variant; late String name; int? min; int? expiry; ItemVariant(Map json) { item = json["item"]; variant = json["variant"]; name = json["name"]; min = json["min"]; expiry = json["expiry"]; } } class Price { late double value; late String currency; Price(Map json) { value = json["value"]; currency = json["currency"]; } } class Transaction { late String uuid; late String item; late String variant; late Price price; late String? origin; late int timestamp; late ConsumeInfo? consumed; late bool expired; Transaction(Map json) { uuid = json["uuid"]; item = json["item"]; variant = json["variant"]; price = Price(json["price"]); origin = json["origin"]; timestamp = json["timestamp"]; expired = json["expired"]; consumed = json["consumed"] != null ? ConsumeInfo(json["consumed"]) : null; } } class ConsumeInfo { late String destination; late Price price; late int timestamp; ConsumeInfo(Map json) { destination = json["destination"]; price = Price(json["price"]); timestamp = json["timestamp"]; } }