2021-02-11 20:47:42 +00:00
|
|
|
import 'dart:async';
|
2021-02-21 17:31:03 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-02-11 20:47:42 +00:00
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
import 'package:imagini/api/cookie_client/cookie_client.dart'
|
|
|
|
if (dart.library.html) 'package:imagini/api/cookie_client/browser_cookie_client.dart'
|
|
|
|
if (dart.library.io) 'package:imagini/api/cookie_client/io_cookie_client.dart';
|
2021-02-11 20:47:42 +00:00
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
import 'package:imagini/core/storage_client/base_storage_client.dart';
|
2021-02-22 03:36:25 +00:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2021-02-11 20:47:42 +00:00
|
|
|
import 'package:graphql_flutter/graphql_flutter.dart';
|
|
|
|
import 'package:imagini/graphql/imagini_graphql.dart';
|
|
|
|
|
|
|
|
class APIProvider{
|
2021-02-20 19:10:25 +00:00
|
|
|
static const String _GRAPHQL_ENDPOINT = "/query";
|
2021-02-11 20:47:42 +00:00
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
BaseStorageClient _storage;
|
2021-02-11 20:47:42 +00:00
|
|
|
GraphQLClient _client;
|
|
|
|
HttpLink httpLink;
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
APIProvider(BaseStorageClient storage) {
|
|
|
|
_storage = storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> init() async {
|
|
|
|
String _server = await _storage.get("server");
|
|
|
|
|
2021-02-22 03:36:25 +00:00
|
|
|
// Initialize
|
|
|
|
if (_server == null)
|
|
|
|
_server = "http://localhost";
|
|
|
|
|
2021-02-11 20:47:42 +00:00
|
|
|
httpLink = HttpLink(_server + _GRAPHQL_ENDPOINT,
|
2021-02-20 19:10:25 +00:00
|
|
|
httpClient: getCookieClient(_storage),
|
2021-02-11 20:47:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
_client = GraphQLClient(
|
|
|
|
cache: GraphQLCache(),
|
|
|
|
link: httpLink,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
Future<QueryResult> login(
|
2021-02-11 20:47:42 +00:00
|
|
|
String username,
|
|
|
|
String password,
|
2021-02-20 19:10:25 +00:00
|
|
|
String server,
|
|
|
|
) async {
|
2021-02-11 20:47:42 +00:00
|
|
|
assert(
|
2021-02-20 19:10:25 +00:00
|
|
|
(username != null && password != null && server != null)
|
2021-02-11 20:47:42 +00:00
|
|
|
);
|
|
|
|
|
2021-02-22 03:36:25 +00:00
|
|
|
// Initialize New Connection
|
2021-02-20 19:10:25 +00:00
|
|
|
await _storage.set("server", server);
|
|
|
|
await init();
|
|
|
|
|
2021-02-11 20:47:42 +00:00
|
|
|
QueryResult response = await _client.query(
|
|
|
|
QueryOptions(
|
|
|
|
document: LoginQuery().document,
|
|
|
|
variables: {
|
|
|
|
"user": username,
|
|
|
|
"password": password
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
2021-02-20 19:10:25 +00:00
|
|
|
return response;
|
2021-02-11 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 03:36:25 +00:00
|
|
|
Future<CachedNetworkImage> getImage(String fileName, int derivedContentWidth) async {
|
|
|
|
String server = await _storage.get("server");
|
|
|
|
String accessToken = await _storage.get("accessToken");
|
|
|
|
String refreshToken = await _storage.get("refreshToken");
|
|
|
|
|
|
|
|
String fullURL = "$server/media/$fileName?width=$derivedContentWidth";
|
|
|
|
return CachedNetworkImage(
|
|
|
|
imageUrl: fullURL,
|
|
|
|
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
|
|
|
|
httpHeaders: {
|
|
|
|
"X-Imagini-AccessToken": accessToken,
|
|
|
|
"X-Imagini-RefreshToken": refreshToken,
|
|
|
|
},
|
2021-02-21 17:31:03 +00:00
|
|
|
fit: BoxFit.contain,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
Future<QueryResult> me() async {
|
2021-02-11 20:47:42 +00:00
|
|
|
QueryResult response = await _client.query(
|
|
|
|
QueryOptions(
|
|
|
|
document: MeQuery().document,
|
|
|
|
)
|
|
|
|
);
|
2021-02-20 19:10:25 +00:00
|
|
|
return response;
|
2021-02-11 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
Future<QueryResult> mediaItems() async {
|
|
|
|
QueryResult response = await _client.query(
|
|
|
|
QueryOptions(
|
|
|
|
document: MediaItemsQuery().document,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return response;
|
2021-02-11 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {}
|
|
|
|
}
|