Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
54 set up grpc client (#59)
Browse files Browse the repository at this point in the history
* Rename `lib/model` -> `lib/models` and `networking/apis` -> `networking/api`

* Update error handling to work with gRPC codes

* Setup proto dir structure

* Implement grpc_handler.dart

* Add (de-)serialization from/to proto

* Implement `fetchUser()` gRPC method in `user_handler.dart` and update `user_viewmodel.dart`

* Add doc

* Fix args & linting in model_generator.dart

* Update dependencies

* Update README.md

* Update `routes.dart` to automatically configure root url

* Update doc

* Add `setup_grpc.sh`
  • Loading branch information
carlobortolan authored Nov 26, 2023
1 parent f4c4a60 commit 7b0b295
Show file tree
Hide file tree
Showing 41 changed files with 3,395 additions and 169 deletions.
44 changes: 30 additions & 14 deletions lib/base/helpers/model_generator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:math';
import 'package:gocast_mobile/model/course/bookmark_model.dart';
import 'package:gocast_mobile/model/course/course_model.dart';
import 'package:gocast_mobile/model/user/user_model.dart';
import 'package:gocast_mobile/model/user/user_settings_model.dart';
import 'package:gocast_mobile/models/course/bookmark_model.dart';
import 'package:gocast_mobile/models/course/course_model.dart';
import 'package:gocast_mobile/models/user/user_model.dart';
import 'package:gocast_mobile/models/user/user_settings_model.dart';

class ModelGenerator {
static User generateRandomUser() {
Expand All @@ -14,24 +14,38 @@ class ModelGenerator {
final matriculationNumber = 'M$id';
final lrzId = 'L$id';
final role = random.nextInt(3) + 1;
final password = 'password$id';
final courses = List.generate(
random.nextInt(5) + 1,
(index) => Course(id: index, name: 'Course$index'),
(index) => Course(
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final administeredCourses = List.generate(
random.nextInt(3) + 1,
(index) => Course(id: index, name: 'Administered Course$index'),
(index) => Course(
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final pinnedCourses = List.generate(
random.nextInt(3) + 1,
(index) => Course(id: index, name: 'Pinned Course$index'),
(index) => Course(
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final settings = List.generate(
random.nextInt(3) + 1,
(index) => UserSetting(
id: index,
userId: id,
userID: random.nextInt(1000),
type: UserSettingType.values[random.nextInt(3)],
value: 'Value$index',
),
Expand All @@ -40,9 +54,12 @@ class ModelGenerator {
random.nextInt(3) + 1,
(index) => Bookmark(
id: index,
userId: id,
title: 'Bookmark$index',
url: 'https://example.com/bookmark$index',
userID: id,
description: '',
hours: 1,
minutes: 2,
seconds: 3,
streamID: 4,
),
);

Expand All @@ -52,9 +69,8 @@ class ModelGenerator {
lastName: lastName,
email: email,
matriculationNumber: matriculationNumber,
lrzId: lrzId,
lrzID: lrzId,
role: role,
password: password,
courses: courses,
administeredCourses: administeredCourses,
pinnedCourses: pinnedCourses,
Expand Down
26 changes: 26 additions & 0 deletions lib/base/networking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Information
The `api` directory contains handlers like `api_handler.dart`, `auth_handler.dart`, and `user_handler.dart` which are responsible for handling API calls related to different functionalities of the application. The `gocast` and `google` directories contain protobuf files generated from the `.proto` files which are used for serializing structured data.

The `api/gocast` directory contains protobuf files for the GoCast API, and the `api/google` directory contains protobuf files for Google's well-known types. These protobuf files are used by the `grpc_handler.dart` to make API calls._


> __NOTE__: _Currently using HTTP for the login, but ideally all other endpoints are called using gRPC. To get an idea how this looks like and what the endpoint definitions are check out our ***[Postman Collection](https://documenter.getpostman.com/view/31343920/2s9YeBdszX)*** and out current ***[protofile](https://github.com/TUM-Dev/gocast/blob/IPraktikum-dev/api_v2/api_v2.proto)***._
# Setup

To setup the protobuf files, follow the steps below:

Make sure to have protoc installed:

```
brew install grpc protobuf
```

```
dart pub global activate protoc_plugin
export PATH="$PATH:$HOME/.pub-cache/bin"
curl -o proto/gocast/api_v2.proto https://raw.githubusercontent.com/TUM-Dev/gocast/IPraktikum-36-user-endpoints/api_v2/api_v2.proto
protoc --dart_out=grpc:lib/base/networking/api -I./proto google/protobuf/timestamp.proto google/protobuf/empty.proto proto/gocast/api_v2.proto
```

> __Source__: https://github.com/TUM-Dev/Campus-Flutter#updating-the-proto-files-of-the
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:gocast_mobile/model/error_model.dart';
import 'package:gocast_mobile/models/error_model.dart';

class ApiHandler {
/// Handles an HTTP response.
Expand Down Expand Up @@ -41,7 +41,7 @@ class ApiHandler {
/// - Other: [AppError.unknownError] is thrown
static void handleHttpStatus(int? statusCode, String? apiMessage) {
if (statusCode == null) {
throw AppError.unknownError();
throw AppError.unknownError("Status code is null");
}

if (statusCode >= 100 && statusCode < 400) {
Expand All @@ -61,7 +61,7 @@ class ApiHandler {
case 500:
throw AppError.internalServerError();
default:
throw AppError.unknownError();
throw AppError.unknownError("Status code: $statusCode");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart' as webview;
import 'package:gocast_mobile/base/networking/apis/api_handler.dart';
import 'package:gocast_mobile/model/error_model.dart';
import 'package:gocast_mobile/model/token_model.dart';
import 'package:gocast_mobile/base/networking/api/api_handler.dart';
import 'package:gocast_mobile/models/error_model.dart';
import 'package:gocast_mobile/models/token_model.dart';
import 'package:gocast_mobile/routes.dart';

/// Handles authentication for the application.
Expand All @@ -20,7 +20,7 @@ class AuthHandler {
String username,
String password,
) async {
const url = Routes.basicLogin;
var url = Routes.basicLogin;
final cookieJar = CookieJar();
final dio = Dio(
BaseOptions(
Expand Down
Loading

0 comments on commit 7b0b295

Please sign in to comment.