134 lines
3.1 KiB
Dart
134 lines
3.1 KiB
Dart
|
import 'dart:io';
|
||
|
import 'dart:async';
|
||
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:meta/meta.dart';
|
||
|
import 'package:http/http.dart' as http;
|
||
|
|
||
|
import 'package:imagini/models/api/response/LoginResponse.dart';
|
||
|
|
||
|
class APIProvider{
|
||
|
String _server, _accessToken, _refreshToken;
|
||
|
|
||
|
static const String _LOGIN_API = "/api/v1/Login";
|
||
|
static const String _MEDIA_ITEMS_API = "/api/v1/MediaItems";
|
||
|
static const String _ALBUMS_API = "/api/v1/Albums";
|
||
|
static const String _TAGS_API = "/api/v1/Tags";
|
||
|
|
||
|
APIProvider({
|
||
|
@required String server,
|
||
|
String accessToken,
|
||
|
String refreshToken
|
||
|
}) {
|
||
|
_server = server;
|
||
|
_accessToken = accessToken;
|
||
|
_refreshToken = refreshToken;
|
||
|
}
|
||
|
|
||
|
// Endpoint: /api/v1/Login
|
||
|
Future<LoginResponse> login([
|
||
|
String username,
|
||
|
String password,
|
||
|
]) async {
|
||
|
assert(
|
||
|
(username != null && password != null) ||
|
||
|
(_accessToken != null && _refreshToken != null)
|
||
|
);
|
||
|
|
||
|
http.Response response = await http.post(
|
||
|
Uri.encodeFull(_server + _LOGIN_API),
|
||
|
body: jsonEncode(<String, String>{
|
||
|
'user': username,
|
||
|
'password': password,
|
||
|
}),
|
||
|
).timeout(Duration(seconds: 10));
|
||
|
|
||
|
if (response.statusCode != 200) {
|
||
|
// Fuck
|
||
|
return LoginResponse.fromJson(jsonDecode(response.body));
|
||
|
}
|
||
|
|
||
|
// 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();
|
||
|
|
||
|
print("Status: ${response.statusCode.toString()}");
|
||
|
print("Body: ${response.body.toString()}");
|
||
|
print("AccessToken: ${accessToken.toString()}");
|
||
|
print("RefreshToken: ${refreshToken.toString()}");
|
||
|
|
||
|
return LoginResponse.fromJson(jsonDecode(response.body));
|
||
|
}
|
||
|
|
||
|
void logout() {
|
||
|
}
|
||
|
|
||
|
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() {}
|
||
|
}
|