Initial Commit
This commit is contained in:
144
web_native/lib/api/api_provider.dart
Normal file
144
web_native/lib/api/api_provider.dart
Normal file
@@ -0,0 +1,144 @@
|
||||
import 'dart:async';
|
||||
|
||||
// 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:meta/meta.dart';
|
||||
import 'package:graphql_flutter/graphql_flutter.dart';
|
||||
import 'package:imagini/graphql/imagini_graphql.dart';
|
||||
|
||||
class APIProvider{
|
||||
String _server, _accessToken, _refreshToken;
|
||||
|
||||
GraphQLClient _client;
|
||||
HttpLink httpLink;
|
||||
// CookieLink cookieLink;
|
||||
static const String _GRAPHQL_ENDPOINT = "/query";
|
||||
|
||||
APIProvider({
|
||||
@required String server,
|
||||
String accessToken,
|
||||
String refreshToken
|
||||
}) {
|
||||
_server = server;
|
||||
_accessToken = accessToken;
|
||||
_refreshToken = refreshToken;
|
||||
httpLink = HttpLink(_server + _GRAPHQL_ENDPOINT,
|
||||
httpClient: getCookieClient(),
|
||||
);
|
||||
|
||||
// cookieLink = CookieLink(_updateAccessToken, _updateRefreshToken);
|
||||
_client = GraphQLClient(
|
||||
cache: GraphQLCache(),
|
||||
link: httpLink,
|
||||
);
|
||||
}
|
||||
|
||||
// void _updateAccessToken(_accessToken) {
|
||||
// print("Updating Access Token: $_accessToken");
|
||||
// this._accessToken = _accessToken;
|
||||
// }
|
||||
|
||||
// void _updateRefreshToken(_refreshToken) {
|
||||
// print("Updating Refresh Token: $_accessToken");
|
||||
// this._refreshToken = _refreshToken;
|
||||
// }
|
||||
|
||||
Future<Login$Query$AuthResponse> login([
|
||||
String username,
|
||||
String password,
|
||||
]) async {
|
||||
assert(
|
||||
(username != null && password != null)
|
||||
);
|
||||
|
||||
QueryResult response = await _client.query(
|
||||
QueryOptions(
|
||||
document: LoginQuery().document,
|
||||
variables: {
|
||||
"user": username,
|
||||
"password": password
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
final loginResponse = Login$Query.fromJson(response.data);
|
||||
return loginResponse.login;
|
||||
}
|
||||
|
||||
Future<Me$Query$User> me() async {
|
||||
QueryResult response = await _client.query(
|
||||
QueryOptions(
|
||||
document: MeQuery().document,
|
||||
)
|
||||
);
|
||||
|
||||
final meResponse = Me$Query.fromJson(response.data);
|
||||
return meResponse.me;
|
||||
}
|
||||
|
||||
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() {}
|
||||
}
|
||||
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.');
|
||||
12
web_native/lib/api/imagini_api_repository.dart
Normal file
12
web_native/lib/api/imagini_api_repository.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:imagini/api/api_provider.dart';
|
||||
import 'package:imagini/graphql/imagini_graphql.dart';
|
||||
|
||||
class ImaginiAPIRepository {
|
||||
APIProvider _apiProvider;
|
||||
|
||||
ImaginiAPIRepository(this._apiProvider);
|
||||
|
||||
Stream<Login$Query$AuthResponse> login(String user, password) {
|
||||
return Stream.fromFuture(_apiProvider.login(user, password));
|
||||
}
|
||||
}
|
||||
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