Rename, GraphQL Flutter Cookie Basics, & GraphQL Flutter Model Gen
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
// ignore: uri_does_not_exist
|
||||
import 'cookie_client_stub.dart'
|
||||
// ignore: uri_does_not_exist
|
||||
if (dart.library.html) 'browser_cookie_client.dart'
|
||||
// ignore: uri_does_not_exist
|
||||
if (dart.library.io) 'io_cookie_client.dart';
|
||||
|
||||
import 'package:imagini/models/api/response/LoginResponse.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:graphql_flutter/graphql_flutter.dart';
|
||||
|
||||
import 'package:imagini/models/api/response/login_response.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";
|
||||
GraphQLClient _client;
|
||||
HttpLink httpLink;
|
||||
// CookieLink cookieLink;
|
||||
static const String _GRAPHQL_ENDPOINT = "/query";
|
||||
|
||||
APIProvider({
|
||||
@required String server,
|
||||
@@ -23,9 +29,27 @@ class APIProvider{
|
||||
_server = server;
|
||||
_accessToken = accessToken;
|
||||
_refreshToken = refreshToken;
|
||||
httpLink = HttpLink(_server + _GRAPHQL_ENDPOINT,
|
||||
httpClient: getCookieClient(),
|
||||
);
|
||||
|
||||
// cookieLink = CookieLink(_updateAccessToken, _updateRefreshToken);
|
||||
_client = GraphQLClient(
|
||||
cache: GraphQLCache(),
|
||||
link: httpLink,
|
||||
);
|
||||
}
|
||||
|
||||
// Endpoint: /api/v1/Login
|
||||
// void _updateAccessToken(_accessToken) {
|
||||
// print("Updating Access Token: $_accessToken");
|
||||
// this._accessToken = _accessToken;
|
||||
// }
|
||||
|
||||
// void _updateRefreshToken(_refreshToken) {
|
||||
// print("Updating Refresh Token: $_accessToken");
|
||||
// this._refreshToken = _refreshToken;
|
||||
// }
|
||||
|
||||
Future<LoginResponse> login([
|
||||
String username,
|
||||
String password,
|
||||
@@ -34,37 +58,28 @@ class APIProvider{
|
||||
(username != null && password != null) ||
|
||||
(_accessToken != null && _refreshToken != null)
|
||||
);
|
||||
String loginQuery = """
|
||||
query login(\$user: String!, \$password: String!, \$deviceID: ID) {
|
||||
login(user: \$user, password: \$password, deviceID: \$deviceID) {
|
||||
result,
|
||||
device {
|
||||
id
|
||||
}
|
||||
}
|
||||
}""";
|
||||
|
||||
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));
|
||||
}
|
||||
QueryResult response = await _client.query(
|
||||
QueryOptions(
|
||||
document: gql(loginQuery),
|
||||
variables: {
|
||||
"user": "admin",
|
||||
"password": "admin"
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// 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));
|
||||
return LoginResponse.fromJson(jsonDecode("{}"));
|
||||
}
|
||||
|
||||
void logout() {
|
||||
30
web_native/lib/api/browser_cookie_client.dart
Normal file
30
web_native/lib/api/browser_cookie_client.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:http/browser_client.dart';
|
||||
import "package:http/http.dart";
|
||||
|
||||
BaseClient getCookieClient() => ClientWithCookies();
|
||||
|
||||
class ClientWithCookies extends BrowserClient {
|
||||
String _accessToken = "asdasdasd";
|
||||
String _refreshToken;
|
||||
|
||||
@override
|
||||
Future<StreamedResponse> send(BaseRequest request) async {
|
||||
request.headers.addAll({
|
||||
'X-Imagini-AccessToken': _accessToken,
|
||||
'X-Imagini-RefreshToken': _refreshToken,
|
||||
});
|
||||
|
||||
return super.send(request).then((response) {
|
||||
if (response.headers.containsKey("x-imagini-accesstoken")) {
|
||||
this._accessToken = response.headers["x-imagini-accesstoken"];
|
||||
}
|
||||
if (response.headers.containsKey("x-imagini-refreshtoken")) {
|
||||
this._refreshToken = response.headers["x-imagini-refreshtoken"];
|
||||
}
|
||||
|
||||
print("Access Token: $_accessToken");
|
||||
print("Refresh Token: $_refreshToken");
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
4
web_native/lib/api/cookie_client_stub.dart
Normal file
4
web_native/lib/api/cookie_client_stub.dart
Normal file
@@ -0,0 +1,4 @@
|
||||
import 'package:http/http.dart';
|
||||
|
||||
BaseClient getCookieClient() => throw UnsupportedError(
|
||||
'Cannot create a client without dart:html or dart:io.');
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:imagini/api/APIProvider.dart';
|
||||
import 'package:imagini/models/api/response/LoginResponse.dart';
|
||||
import 'package:imagini/api/api_provider.dart';
|
||||
import 'package:imagini/models/api/response/login_response.dart';
|
||||
|
||||
class ImaginiAPIRepository {
|
||||
APIProvider _apiProvider;
|
||||
28
web_native/lib/api/io_cookie_client.dart
Normal file
28
web_native/lib/api/io_cookie_client.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:http/io_client.dart';
|
||||
import "package:http/http.dart";
|
||||
|
||||
BaseClient getCookieClient() => IOClientWithCookies();
|
||||
|
||||
class IOClientWithCookies extends IOClient {
|
||||
String _accessToken;
|
||||
String _refreshToken;
|
||||
|
||||
@override
|
||||
Future<IOStreamedResponse> send(BaseRequest request) async {
|
||||
// String cookie = await getCookie();
|
||||
// String getCookieString(String _) => cookie;
|
||||
// request.headers.update('cookie', getCookieString);
|
||||
return super.send(request).then((response) {
|
||||
if (response.headers.containsKey("x-imagini-accesstoken")) {
|
||||
this._accessToken = response.headers["x-imagini-accesstoken"];
|
||||
}
|
||||
if (response.headers.containsKey("x-imagini-refreshtoken")) {
|
||||
this._refreshToken = response.headers["x-imagini-refreshtoken"];
|
||||
}
|
||||
|
||||
print("Access Token: $_accessToken");
|
||||
print("Refresh Token: $_refreshToken");
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user