diff --git a/lib/api.dart b/lib/api.dart index c380f573..ec237327 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -6,16 +6,24 @@ import 'package:path/path.dart' as path; import 'package:http/http.dart' as http; +/** + * InvenTree API - Access to the InvenTree REST interface. + * + * InvenTree implements token-based authentication, which is + * initialised using a username:password combination. + */ + + class InvenTreeAPI { + // Endpoint for requesting an API token static const _URL_GET_TOKEN = "user/token/"; + static const _URL_GET_VERSION = ""; // Ensure we only ever create a single instance of the API class static final InvenTreeAPI _api = new InvenTreeAPI._internal(); - factory InvenTreeAPI() { - return _api; - } + factory InvenTreeAPI() { return _api; } InvenTreeAPI._internal(); @@ -77,6 +85,8 @@ class InvenTreeAPI { url = url.substring(1); } + + return path.join(_base_url, url); } diff --git a/lib/inventree_object.dart b/lib/inventree_object.dart new file mode 100644 index 00000000..f5df15d8 --- /dev/null +++ b/lib/inventree_object.dart @@ -0,0 +1,144 @@ +import 'api.dart'; + +import 'dart:convert'; + +import 'package:path/path.dart' as path; +import 'package:http/http.dart' as http; + + +/** + * The InvenTreeObject class provides a base-level object + * for interacting with InvenTree data. + */ +class InvenTreeObject { + + // Override the endpoint URL for each subclass + String _URL = ""; + String _name = "InvenTree"; + + // JSON data which defines this object + Map _data = {}; + + // Accessor for the API + var api = InvenTreeAPI(); + + // Default empty object constructor + InvenTreeObject() { + _data.clear(); + } + + // Construct an InvenTreeObject from a JSON data object + InvenTreeObject.fromJson(Map json) { + + // Store the json object + _data = json; + + } + + int get pk { + return _data['pk'] ?? -1; + } + + // Create a new object from JSON data (not a constructor!) + InvenTreeObject _createFromJson(Map json) { + print("creating new object"); + + var obj = InvenTreeObject.fromJson(json); + + return obj; + } + + String get url{ return path.join(_URL, pk.toString()); } + + // Return list of objects from the database, with optional filters + Future list({Map filters}) async { + + if (filters == null) { + filters = {}; + } + + print("Listing endpoint: " + _URL); + + // TODO - Add "timeout" + // TODO - Add error catching + + InvenTreeAPI().get(_URL, params:filters).then((http.Response response) { + + final data = json.decode(response.body); + + for (var d in data) { + print(d); + + var obj = _createFromJson(d); + + if (obj is InvenTreePart) { + print("Part -> " + obj.name + obj.description); + } else { + print("Not part :'("); + print(obj.runtimeType); + } + } + + // var obj = _createFromJson(data); + + }); + } + + + // Provide a listing of objects at the endpoint + // TODO - Static function which returns a list of objects (of this class) + + // TODO - Define a 'delete' function + + // TODO - Define a 'save' / 'update' function + + +} + + +class InvenTreePart extends InvenTreeObject { + + @override + String _URL = "part/"; + + @override + String _name = "part"; + + String get name { + return _data['name'] ?? ''; + } + + String get description { + return _data['description'] ?? ''; + } + + int get categoryId { + return _data['category'] as int ?? -1; + } + + String get categoryName { + return _data['category__name'] ?? ''; + } + + InvenTreePart() : super(); + + InvenTreePart.fromJson(Map json) : super.fromJson(json) { + + } + + @override + InvenTreeObject _createFromJson(Map json) { + + var part = InvenTreePart.fromJson(json); + + print("creating new part!"); + print(json); + + return part; + + } + + // TODO - Is there a way of making this "list" function generic to the InvenTreeObject class? + // In an ideal world it would return a list of + //List list() +} \ No newline at end of file