From 1a9e53a9ed685ac3f902930f6298769d0e256509 Mon Sep 17 00:00:00 2001 From: Darkhan Nausharipov <31556582+nausharipov@users.noreply.github.com> Date: Mon, 3 Apr 2023 20:06:11 +0600 Subject: [PATCH] CloudFunctionsTobClient refactoring (#451) * _makeRequest * _makeRequest comments --------- Co-authored-by: darkhan.nausharipov --- .../client/cloud_functions_client.dart | 125 ++++++++++-------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/learning/tour-of-beam/frontend/lib/repositories/client/cloud_functions_client.dart b/learning/tour-of-beam/frontend/lib/repositories/client/cloud_functions_client.dart index 30f61670d822..9be881abcedd 100644 --- a/learning/tour-of-beam/frontend/lib/repositories/client/cloud_functions_client.dart +++ b/learning/tour-of-beam/frontend/lib/repositories/client/cloud_functions_client.dart @@ -32,42 +32,76 @@ import '../models/get_sdks_response.dart'; import '../models/get_user_progress_response.dart'; import 'client.dart'; -// TODO(nausharipov): add repository and handle exceptions +enum RequestMethod { + post, + get, +} + class CloudFunctionsTobClient extends TobClient { + Future _makeRequest({ + required String path, + required RequestMethod method, + Map queryParameters = const {}, + dynamic body, + }) async { + final token = await GetIt.instance.get().getToken(); + final uri = Uri.parse('$cloudFunctionsBaseUrl$path') + .replace(queryParameters: queryParameters); + final headers = token != null + ? {HttpHeaders.authorizationHeader: 'Bearer $token'} + : null; + + http.Response response; + switch (method) { + case RequestMethod.post: + response = await http.post( + uri, + headers: headers, + body: body, + ); + break; + case RequestMethod.get: + response = await http.get( + uri, + headers: headers, + ); + break; + } + return jsonDecode(utf8.decode(response.bodyBytes)); + } + @override Future getSdks() async { - final json = await http.get( - Uri.parse( - '$cloudFunctionsBaseUrl/getSdkList', - ), + final map = await _makeRequest( + method: RequestMethod.get, + path: 'getSdkList', ); - - final map = jsonDecode(utf8.decode(json.bodyBytes)) as Map; return GetSdksResponse.fromJson(map); } @override Future getContentTree(String sdkId) async { - final json = await http.get( - Uri.parse( - '$cloudFunctionsBaseUrl/getContentTree?sdk=$sdkId', - ), + final map = await _makeRequest( + method: RequestMethod.get, + path: 'getContentTree', + queryParameters: { + 'sdk': sdkId, + }, ); - - final map = jsonDecode(utf8.decode(json.bodyBytes)) as Map; final response = GetContentTreeResponse.fromJson(map); return ContentTreeModel.fromResponse(response); } @override Future getUnitContent(String sdkId, String unitId) async { - final json = await http.get( - Uri.parse( - '$cloudFunctionsBaseUrl/getUnitContent?sdk=$sdkId&id=$unitId', - ), + final map = await _makeRequest( + method: RequestMethod.get, + path: 'getUnitContent', + queryParameters: { + 'sdk': sdkId, + 'id': unitId, + }, ); - - final map = jsonDecode(utf8.decode(json.bodyBytes)) as Map; return UnitContentModel.fromJson(map); } @@ -77,42 +111,34 @@ class CloudFunctionsTobClient extends TobClient { if (token == null) { return null; } - final json = await http.get( - Uri.parse( - '$cloudFunctionsBaseUrl/getUserProgress?sdk=$sdkId', - ), - headers: { - HttpHeaders.authorizationHeader: 'Bearer $token', + final map = await _makeRequest( + method: RequestMethod.get, + path: 'getUserProgress', + queryParameters: { + 'sdk': sdkId, }, ); - final map = jsonDecode(utf8.decode(json.bodyBytes)) as Map; final response = GetUserProgressResponse.fromJson(map); return response; } @override Future postUnitComplete(String sdkId, String id) async { - final token = await GetIt.instance.get().getToken(); - await http.post( - Uri.parse( - '$cloudFunctionsBaseUrl/postUnitComplete?sdk=$sdkId&id=$id', - ), - headers: { - HttpHeaders.authorizationHeader: 'Bearer $token', + await _makeRequest( + method: RequestMethod.post, + path: 'postUnitComplete', + queryParameters: { + 'sdk': sdkId, + 'id': id, }, ); } @override Future postDeleteUserProgress() async { - final token = await GetIt.instance.get().getToken(); - await http.post( - Uri.parse( - '$cloudFunctionsBaseUrl/postDeleteProgress', - ), - headers: { - HttpHeaders.authorizationHeader: 'Bearer $token', - }, + await _makeRequest( + method: RequestMethod.post, + path: 'postDeleteProgress', ); } @@ -122,17 +148,12 @@ class CloudFunctionsTobClient extends TobClient { required String sdkId, required String unitId, }) async { - final token = await GetIt.instance.get().getToken(); - if (token == null) { - return; - } - - await http.post( - Uri.parse( - '$cloudFunctionsBaseUrl/postUserCode?sdk=$sdkId&id=$unitId', - ), - headers: { - HttpHeaders.authorizationHeader: 'Bearer $token', + await _makeRequest( + path: 'postUserCode', + method: RequestMethod.post, + queryParameters: { + 'sdk': sdkId, + 'id': unitId, }, body: jsonEncode({ 'files': snippetFiles.map((file) => file.toJson()).toList(),