From 6e807cbed17943e990e1f86fb8a7779582bd43e1 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 25 Jun 2019 22:38:11 +1000 Subject: [PATCH] Make the API a singleton class Also add some simple URL validation --- lib/api.dart | 50 +++++++++++++++++++++++++++++++++++++---- lib/login_settings.dart | 11 ++++++--- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 76237405..628eb92d 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -6,14 +6,47 @@ import 'package:path/path.dart' as path; import 'package:http/http.dart' as http; -class InventreeAPI { +class InvenTreeAPI { - InventreeAPI(this._username, this._password) { - _secureToken(); + // Ensure we only ever create a single instance of the API class + static final InvenTreeAPI _api = new InvenTreeAPI._internal(); + + factory InvenTreeAPI() { + return _api; } + InvenTreeAPI._internal(); + + void connect(String address, String username, String password) async { + + address = address.trim(); + username = username.trim(); + + if (!address.endsWith("api/") || !address.endsWith("api")) { + address = path.join(address, "api"); + } + + if (!address.endsWith('/')) { + address = address + '/'; + } + + _base_url = address; + _username = username; + _password = password; + + _connected = false; + + print("Connecting to " + address + " -> " + username + ":" + password); + + await _tryConnection(); + + await _secureToken(); + } + + bool _connected = false; + // Base URL for InvenTree API - String _base_url = "http://10.0.0.7:8000/api/"; + String _base_url = "http://127.0.0.1:8000/api/"; String _username = ""; String _password = ""; @@ -30,9 +63,18 @@ class InventreeAPI { return _token.isNotEmpty; } + // Request the raw /api/ endpoing to see if there is an InvenTree server listening + void _tryConnection() async { + final response = get(""); + } + // Request an API token from the server. // A valid username/password combination must be provided void _secureToken() async { + + if (_token.isNotEmpty) { + print("Discarding old token - " + _token); + } _token = ""; var _url = _makeUrl("user/token/"); diff --git a/lib/login_settings.dart b/lib/login_settings.dart index f143438e..5f6bffef 100644 --- a/lib/login_settings.dart +++ b/lib/login_settings.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'api.dart'; + class InvenTreeLoginSettingsWidget extends StatefulWidget { @override @@ -22,6 +24,10 @@ class _InvenTreeLoginSettingsState extends State { return 'Server cannot be empty'; } + if (!value.startsWith("http:") && !value.startsWith("https:")) { + return 'Server must start with http[s]'; + } + return null; } @@ -48,9 +54,6 @@ class _InvenTreeLoginSettingsState extends State { @override Widget build(BuildContext context) { - print("here we are"); - - print("Server: " + _addr); final Size screenSize = MediaQuery.of(context).size; @@ -135,6 +138,8 @@ class _InvenTreeLoginSettingsState extends State { await prefs.setString('server', _addr); await prefs.setString('username', _user); await prefs.setString('password', _pass); + + InvenTreeAPI().connect(_addr, _user, _pass); } } } \ No newline at end of file