Skip to content

Instantly share code, notes, and snippets.

@ruan65
Created June 20, 2021 08:02
Show Gist options
  • Save ruan65/7c0f99bffccb5b675b87bffe6e3a8dad to your computer and use it in GitHub Desktop.
Save ruan65/7c0f99bffccb5b675b87bffe6e3a8dad to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:grpc/grpc.dart';
import 'generated/umka.pbgrpc.dart';
class UmkaTerminalClient {
late final ClientChannel channel;
late final UmkaClient stub;
UmkaTerminalClient() {
channel = ClientChannel(
'127.0.0.1',
port: 5555,
options: ChannelOptions(credentials: ChannelCredentials.insecure()),
);
stub = UmkaClient(channel);
}
Future<Question> getQuestion(Student student) async {
final question = await stub.getQuestion(student);
print('Received question: $question');
return question;
}
Future<void> sendAnswer(Student student, Question question) async {
final answer = Answer()
..question = question
..student = student;
print('Enter your answer: ');
answer.text = stdin.readLineSync()!;
final evaluation = await stub.sendAnswer(answer);
print('Evaluation for the answer: ${answer.text} '
'\non the question ${question.text}:'
'\n$evaluation');
}
Future<void> callService(Student student) async {
final question = await getQuestion(student);
await sendAnswer(student, question);
await channel.shutdown();
}
}
Future<void> main(List<String> args) async {
final clientApp = UmkaTerminalClient();
final student = Student()
..id = 42
..name = 'Alice Bobich';
await clientApp.callService(student);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment