Finally - A Mobile App Basic Implementation
This commit is contained in:
3
web_native/packages/imagini_api/lib/imagini_api.dart
Normal file
3
web_native/packages/imagini_api/lib/imagini_api.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
library imagini_api;
|
||||
|
||||
export 'src/imagini_api.dart';
|
||||
136
web_native/packages/imagini_api/lib/src/imagini_api.dart
Normal file
136
web_native/packages/imagini_api/lib/src/imagini_api.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
enum ImaginiState { unknown, authenticated, unauthenticated }
|
||||
|
||||
class ImaginiAPI{
|
||||
String _server, _accessToken, _refreshToken;
|
||||
final _controller = StreamController<ImaginiState>();
|
||||
|
||||
ImaginiAPI({
|
||||
@required String server,
|
||||
String accessToken,
|
||||
String refreshToken
|
||||
}) {
|
||||
_server = server;
|
||||
_accessToken = accessToken;
|
||||
_refreshToken = refreshToken;
|
||||
}
|
||||
|
||||
Stream<ImaginiState> get status async* {
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
yield ImaginiState.unauthenticated;
|
||||
yield* _controller.stream;
|
||||
}
|
||||
|
||||
// Endpoint: /api/v1/Login
|
||||
Future<void> login([
|
||||
String username,
|
||||
String password,
|
||||
]) async {
|
||||
assert(
|
||||
(username != null && password != null) ||
|
||||
(_accessToken != null && _refreshToken != null)
|
||||
);
|
||||
|
||||
http.Response response = await http.post(
|
||||
Uri.encodeFull(_server + "/api/v1/Login"),
|
||||
body: jsonEncode(<String, String>{
|
||||
'user': username,
|
||||
'password': password,
|
||||
}),
|
||||
).timeout(Duration(seconds: 10));
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
// Fuck
|
||||
return;
|
||||
}
|
||||
|
||||
// Success
|
||||
String setCookieVal = response.headers["set-cookie"];
|
||||
List<Cookie> allCookies = setCookieVal.split(',')
|
||||
.map((cookie) => Cookie.fromSetCookieValue(cookie)).toList();
|
||||
|
||||
Cookie accessToken = allCookies.firstWhere((cookie) => cookie.name == "AccessToken");
|
||||
Cookie refreshToken = allCookies.firstWhere((cookie) => cookie.name == "RefreshToken");
|
||||
|
||||
_accessToken = accessToken.toString();
|
||||
_refreshToken = refreshToken.toString();
|
||||
|
||||
_controller.add(ImaginiState.authenticated);
|
||||
|
||||
print("Status: ${response.statusCode.toString()}");
|
||||
print("Body: ${response.body.toString()}");
|
||||
print("AccessToken: ${accessToken.toString()}");
|
||||
print("RefreshToken: ${refreshToken.toString()}");
|
||||
}
|
||||
|
||||
void logout() {
|
||||
_controller.add(ImaginiState.unauthenticated);
|
||||
}
|
||||
|
||||
Future<String> mediaItems([
|
||||
String startDate,
|
||||
String endDate,
|
||||
String albumID,
|
||||
List<String> tagID,
|
||||
String type, // TODO: Make enum
|
||||
int page,
|
||||
]) async {
|
||||
// Query:
|
||||
// /api/v1/MediaItems
|
||||
// Derive Params:
|
||||
// startDate:
|
||||
// &createdAt=>2020-10-10T10:10:10
|
||||
// endDate:
|
||||
// &createdAt=<2020-10-10T10:10:10
|
||||
// albumID:
|
||||
// &albumID=9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d
|
||||
// tagID:
|
||||
// &tagID=9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d,9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d
|
||||
// type:
|
||||
// &type=Photos
|
||||
// &type=Videos
|
||||
// page:
|
||||
// &page=4
|
||||
|
||||
// Returns:
|
||||
// {
|
||||
//
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> tags([
|
||||
int page
|
||||
]) async {
|
||||
// Query:
|
||||
// /api/v1/Tags
|
||||
// Derive Params:
|
||||
// page:
|
||||
// &page=4
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> albums([
|
||||
int page
|
||||
]) async {
|
||||
// Query:
|
||||
// /api/v1/Albums
|
||||
// Derive Params:
|
||||
// page:
|
||||
// &page=4
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> me() async {
|
||||
return null;
|
||||
}
|
||||
|
||||
void dispose() => _controller.close();
|
||||
}
|
||||
Reference in New Issue
Block a user