This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/web_native/lib/services/api/api.dart
2021-01-23 19:28:26 -05:00

86 lines
2.2 KiB
Dart

import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
// import 'package:flutter/foundation.dart' show kIsWeb;
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// if(kIsWeb) {
// // Use web storage
// } else {
// // Use flutter_secure_storage
// }
// final _storage = FlutterSecureStorage();
class ImaginiAPI {
static const _baseUrl = "http://10.0.20.170:8484";
// static const String _GET_ALBUMS = '/albums';
Future<String> exampleApi() async {
http.Response response = await http.get(
Uri.encodeFull("https://www.example.com/api"),
);
print("Status: ${response.statusCode.toString()}");
print("Respone: ${response.body.toString()}");
return response.body;
}
Future<Map<String, String>> loginAPI(String user, String password) async {
http.Response response = await http.post(
Uri.encodeFull(_baseUrl + "/api/v1/Login"),
body: jsonEncode(<String, String>{
'user': user,
'password': password,
}),
).timeout(Duration(seconds: 10));
// TODO:
// - StatusCode:
// - 405 (StatusMethodNotAllowed)
// - 400 (StatusBadRequest)
// - 401 (StatusUnauthorized)
// -
// -
// -
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");
print("Status: ${response.statusCode.toString()}");
print("Body: ${response.body.toString()}");
print("AccessToken: ${accessToken.toString()}");
print("RefreshToken: ${refreshToken.toString()}");
return response.headers;
}
Future<String> mediaItemsAPI(String albumID, String tagID) async {
return null;
}
Future<String> tagsAPI() async {
return null;
}
Future<String> albumsAPI() async {
return null;
}
Future<String> meAPI() async {
return null;
}
// API Calls:
// - Login
// - MediaItems
// - Tags
// - Albums
// - Me
}