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/api/api_provider.dart
2021-04-05 20:40:23 -04:00

142 lines
3.9 KiB
Dart

import 'dart:async';
import "package:imagini/api/auth_client/base_auth_client.dart";
import 'package:flutter/material.dart' show BoxFit, Center, SizedBox;
import 'package:imagini/api/auth_client/auth_client.dart'
if (dart.library.html) 'package:imagini/api/auth_client/browser_auth_client.dart'
if (dart.library.io) 'package:imagini/api/auth_client/io_auth_client.dart';
import 'package:imagini/core/storage_client/base_storage_client.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:imagini/graphql/imagini_graphql.dart';
class APIProvider{
static const String _GRAPHQL_ENDPOINT = "/query";
BaseStorageClient _storage;
BaseAuthClient _authClient;
GraphQLClient _client;
HttpLink httpLink;
APIProvider(BaseStorageClient storage) {
_storage = storage;
}
Future<void> init() async {
// Initialize Server
String _server = await _storage.get("server");
if (_server == null) {
_server = "http://localhost";
await _storage.set("server", _server);
}
// Initialize Auth Client
_authClient = getAuthClient(_storage);
// Initialize HTTP Link
httpLink = HttpLink(_server + _GRAPHQL_ENDPOINT,
httpClient: _authClient,
);
// Initialize GraphQL Client
_client = GraphQLClient(
cache: GraphQLCache(),
link: httpLink,
);
}
Future<QueryResult> login(
String username,
String password,
String server,
) async {
assert(
(username != null && password != null && server != null)
);
// Initialize New Connection
await _storage.set("server", server);
await init();
QueryResult response = await _client.query(
QueryOptions(
document: LoginQuery().document,
variables: {
"user": username,
"password": password
},
)
);
return response;
}
CachedNetworkImage getImage(String fileName, int width, int height) {
String server = _authClient.getServer();
String fullURL = "$server/media/$fileName?width=$width";
print(fullURL);
return CachedNetworkImage(
imageUrl: fullURL,
placeholder: (context, url) => Center(
child: SizedBox(
width: width.toDouble() / 3.5,
height: height.toDouble() / 3.5,
child: new PlatformCircularProgressIndicator(),
),
),
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
httpHeaders: _authClient.getHeaders(),
fit: BoxFit.contain,
);
}
// Future<CachedNetworkImage> getImage(String fileName, int width, int height) 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=$width";
// print(fullURL);
// return CachedNetworkImage(
// imageUrl: fullURL,
// placeholder: (context, url) => Center(
// child: SizedBox(
// width: width.toDouble() / 3.5,
// height: height.toDouble() / 3.5,
// child: new PlatformCircularProgressIndicator(),
// ),
// ),
// imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
// httpHeaders: {
// "X-Imagini-AccessToken": accessToken,
// "X-Imagini-RefreshToken": refreshToken,
// },
// fit: BoxFit.contain,
// );
// }
Future<QueryResult> me() async {
QueryResult response = await _client.query(
QueryOptions(
document: MeQuery().document,
)
);
return response;
}
Future<QueryResult> mediaItems(Page page) async {
QueryResult response = await _client.query(
QueryOptions(
document: MediaItemsQuery().document,
variables: {
"page": page,
},
)
);
return response;
}
void dispose() {}
}