Update the env proto and fix auth
This commit is contained in:
		
							
								
								
									
										17
									
								
								lib/api/grpc/creds.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								lib/api/grpc/creds.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
import 'dart:html';
 | 
			
		||||
 | 
			
		||||
class SoftplayerCreds {
 | 
			
		||||
  final String uuid;
 | 
			
		||||
  final String token;
 | 
			
		||||
 | 
			
		||||
  SoftplayerCreds({
 | 
			
		||||
    required this.token,
 | 
			
		||||
    required this.uuid,
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class SoftplayerCredsHelpers {
 | 
			
		||||
  String localStorageEntry(String key) => window.localStorage[key]!;
 | 
			
		||||
  SoftplayerCreds fromLocalStorage() => SoftplayerCreds(
 | 
			
		||||
      token: localStorageEntry("token"), uuid: localStorageEntry("uuid"));
 | 
			
		||||
}
 | 
			
		||||
@@ -1,37 +1,52 @@
 | 
			
		||||
import 'dart:html';
 | 
			
		||||
 | 
			
		||||
import 'package:grpc/grpc_web.dart';
 | 
			
		||||
import 'package:softplayer_dart_proto/main.dart';
 | 
			
		||||
import 'package:softplayer_web/api/grpc/creds.dart';
 | 
			
		||||
 | 
			
		||||
class EnvironmentLocalData {
 | 
			
		||||
  EnvironmentLocalData({
 | 
			
		||||
    required this.uuid,
 | 
			
		||||
    required this.token,
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  String uuid;
 | 
			
		||||
  String token;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class EnvironmentsGrpc {
 | 
			
		||||
  final GrpcWebClientChannel channel;
 | 
			
		||||
  late EnvironmentsClient envStub;
 | 
			
		||||
  EnvironmentsGrpc({
 | 
			
		||||
    required this.channel,
 | 
			
		||||
  });
 | 
			
		||||
  // final GrpcWebClientChannel channel;
 | 
			
		||||
  final EnvironmentsClient envStub;
 | 
			
		||||
 | 
			
		||||
  void init() {
 | 
			
		||||
    envStub = EnvironmentsClient(channel);
 | 
			
		||||
  // Init the grpc channel for environments
 | 
			
		||||
  EnvironmentsGrpc(channel) : envStub = EnvironmentsClient(channel);
 | 
			
		||||
 | 
			
		||||
  // Get environments from the API
 | 
			
		||||
  Future<EnvironmentFull> get(String name, SoftplayerCreds creds) async {
 | 
			
		||||
    final request = GetOptions(
 | 
			
		||||
      name: EnvironmentName(name: name),
 | 
			
		||||
      ownerId: OwnerId(uuid: creds.uuid),
 | 
			
		||||
      token: Token(token: creds.token),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      final response = await envStub.get(request);
 | 
			
		||||
      return response;
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      rethrow;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Stream<List<String>> list() async* {
 | 
			
		||||
    List<String> envs = [];
 | 
			
		||||
  Stream<List<EnvironmentFull>> list(SoftplayerCreds creds) async* {
 | 
			
		||||
    List<EnvironmentFull> envs = [];
 | 
			
		||||
    try {
 | 
			
		||||
      await for (var feature in envStub.list(Empty(),
 | 
			
		||||
          options: CallOptions(metadata: {
 | 
			
		||||
            "uuid": window.localStorage["uuid"]!,
 | 
			
		||||
            "token": window.localStorage["token"]!,
 | 
			
		||||
          }))) {
 | 
			
		||||
        envs.add(feature.data.name);
 | 
			
		||||
      await for (var feature in envStub.list(
 | 
			
		||||
        ListOptions(
 | 
			
		||||
          ownerId: OwnerId(uuid: creds.uuid),
 | 
			
		||||
          token: Token(token: creds.token),
 | 
			
		||||
        ),
 | 
			
		||||
      )) {
 | 
			
		||||
        envs.add(EnvironmentFull(
 | 
			
		||||
          data: feature.data,
 | 
			
		||||
          name: feature.name,
 | 
			
		||||
        ));
 | 
			
		||||
      }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      rethrow;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user