diff options
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/instance.dart | 27 | ||||
-rw-r--r-- | lib/models/login.dart | 66 | ||||
-rw-r--r-- | lib/models/user.dart | 61 |
3 files changed, 154 insertions, 0 deletions
diff --git a/lib/models/instance.dart b/lib/models/instance.dart new file mode 100644 index 0000000..b01b052 --- /dev/null +++ b/lib/models/instance.dart @@ -0,0 +1,27 @@ +import "package:http/http.dart" as http; +import "dart:convert"; + +class Instance { + // TODO: add other fields + final String version; + + Instance({ + required this.version, + }); + + factory Instance.fromJson(Map<String, dynamic> json) { + return Instance( + version: json["Version"], + ); + } +} + +Future<Instance> getInstance(String instanceURL) async { + final res = await http.get(Uri.parse("$instanceURL/info")); + + final json = jsonDecode(res.body); + + // TODO: handle errors + + return Instance.fromJson(json); +}
\ No newline at end of file diff --git a/lib/models/login.dart b/lib/models/login.dart new file mode 100644 index 0000000..6412921 --- /dev/null +++ b/lib/models/login.dart @@ -0,0 +1,66 @@ +import "package:http/http.dart" as http; +import "dart:convert"; + +import "package:openbills/models/user.dart"; + +enum LoginMethod { email, username } + +extension LoginMethodString on LoginMethod { + String get value { + switch(this) { + case LoginMethod.username: + return "username"; + case LoginMethod.email: + return "email"; + } + } +} + +class LoginRes { + final User user; + final String authToken; + final String refreshToken; + + LoginRes({ + required this.user, + required this.authToken, + required this.refreshToken, + }); + + factory LoginRes.fromJson(Map<String, dynamic> json) { + return LoginRes( + user: User.fromJson(json["data"]), + authToken: json["auth_token"], + refreshToken: json["refresh_token"], + ); + } +} + +Future<LoginRes> userSignIn(String accountName, method, password) async { + final res = await http.post( + Uri.parse("https://openbills.vidhukant.com/api/auth/signin"), + headers: <String, String> { + "Content-Type": "application/json; charset=UTF-8", + }, + body: jsonEncode(<String, String> { + "AccountName": accountName, + "Method": method, + "Password": password, + }) + ); + + final json = jsonDecode(res.body); + + if (res.statusCode != 200) { + switch(res.statusCode) { + case 404: + throw "This user does not exist"; + case 500: + throw "Internal Server Error"; + default: + throw json["error"]; + } + } + + return LoginRes.fromJson(json); +} diff --git a/lib/models/user.dart b/lib/models/user.dart new file mode 100644 index 0000000..07c2c09 --- /dev/null +++ b/lib/models/user.dart @@ -0,0 +1,61 @@ +import 'package:http/http.dart' as http; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import "dart:convert"; + +class User { + final int id; + String? createdAt; + String? updatedAt; + String? username; + String? email; + bool? isVerified; + + User({ + required this.id, + this.createdAt, + this.updatedAt, + this.username, + this.email, + this.isVerified + }); + + factory User.fromJson(Map<String, dynamic> json) { + return User( + id: json["ID"], + createdAt: json["CreatedAt"], + updatedAt: json["UpdatedAt"], + username: json["Username"], + email: json["Email"], + isVerified: json["IsVerified"] + ); + } +} + +Future<User> userSignUp(User newUser, String password) async { + final res = await http.post( + Uri.parse("https://openbills.vidhukant.com/api/auth/signup"), + headers: <String, String> { + "Content-Type": "application/json; charset=UTF-8", + }, + body: jsonEncode(<String, String> { + "Username": newUser.username as String, + "Password": password, + "Email": newUser.email as String, + }), + ); + + final json = jsonDecode(res.body); + + if (res.statusCode != 200) { + switch(res.statusCode) { + case 404: + throw "This user does not exist"; + case 500: + throw "Internal Server Error"; + default: + throw json["error"]; + } + } + + return User.fromJson(json["data"]); +} |