mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-27 21:16:48 +00:00
Add API class
- Secure token based on username/password
This commit is contained in:
parent
f747692277
commit
3258903745
72
lib/api.dart
Normal file
72
lib/api.dart
Normal file
@ -0,0 +1,72 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
|
||||
class InventreeAPI {
|
||||
|
||||
InventreeAPI(this._username, this._password) {
|
||||
_secureToken();
|
||||
}
|
||||
|
||||
// Base URL for InvenTree API
|
||||
String _base_url = "http://10.0.0.7:8000/api/";
|
||||
|
||||
String _username = "";
|
||||
String _password = "";
|
||||
|
||||
// Authentication token (initially empty, must be requested)
|
||||
String _token = "";
|
||||
|
||||
// Construct an API URL
|
||||
String _makeUrl(String url) {
|
||||
return path.join(_base_url, url);
|
||||
}
|
||||
|
||||
bool _hasToken() {
|
||||
return _token.isNotEmpty;
|
||||
}
|
||||
|
||||
// Request an API token from the server.
|
||||
// A valid username/password combination must be provided
|
||||
void _secureToken() async {
|
||||
_token = "";
|
||||
|
||||
var _url = _makeUrl("user/token/");
|
||||
final response = await http.post(_url,
|
||||
body: {
|
||||
"username": _username,
|
||||
"password": _password
|
||||
});
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
print("Invalid status code:" + String.fromCharCode(response.statusCode));
|
||||
} else {
|
||||
var _json = json.decode(response.body);
|
||||
|
||||
if (_json["token"] != null) {
|
||||
_token = _json["token"];
|
||||
print("Received token: " + _token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> get(String url) async {
|
||||
var _url = _makeUrl(url);
|
||||
final response = await http.get(_url,
|
||||
headers: {
|
||||
HttpHeaders.authorizationHeader: "Token: " + _token
|
||||
}
|
||||
);
|
||||
|
||||
print("Making request to " + _url);
|
||||
print(response.statusCode);
|
||||
print(response.body);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'settings.dart';
|
||||
import 'api.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@ -57,6 +60,10 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
});
|
||||
}
|
||||
|
||||
void _login() {
|
||||
Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeSettingsWidget()));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// This method is rerun every time setState is called, for instance as done
|
||||
@ -89,8 +96,9 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
),
|
||||
new Divider(),
|
||||
new ListTile(
|
||||
title: new Text("Log In"),
|
||||
leading: new Icon(Icons.security),
|
||||
title: new Text("Settings"),
|
||||
leading: new Icon(Icons.settings),
|
||||
onTap: _login,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
36
lib/settings.dart
Normal file
36
lib/settings.dart
Normal file
@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class InvenTreeSettingsWidget extends StatefulWidget {
|
||||
// InvenTree settings view
|
||||
|
||||
@override
|
||||
_InvenTreeSettingsState createState() => _InvenTreeSettingsState();
|
||||
|
||||
}
|
||||
|
||||
|
||||
class _InvenTreeSettingsState extends State<InvenTreeSettingsWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("InvenTree Settings"),
|
||||
),
|
||||
body: Center(
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
title: Text("Server Settings"),
|
||||
subtitle: Text("Configure server and login settings"),
|
||||
onTap: _editServerSettings,
|
||||
),
|
||||
],
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void _editServerSettings() {
|
||||
|
||||
}
|
||||
}
|
14
pubspec.lock
14
pubspec.lock
@ -81,6 +81,20 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
http:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: http
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.12.0+2"
|
||||
http_parser:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http_parser
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.1.3"
|
||||
image:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -23,6 +23,7 @@ dependencies:
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^0.1.2
|
||||
http: ^0.12.0+2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
Loading…
x
Reference in New Issue
Block a user