From dd04d4491075599e83852d3a20ed2587a35dd741 Mon Sep 17 00:00:00 2001 From: leynier Date: Sun, 30 May 2021 06:29:02 -0400 Subject: [PATCH] feat: Implement apklis_http_web_api --- packages/apklis_http_web_api/.metadata | 10 + packages/apklis_http_web_api/LICENSE | 21 ++ packages/apklis_http_web_api/README.md | 44 +++++ .../assets/response_example.json | 180 ++++++++++++++++++ .../lib/apklis_http_web_api.dart | 36 ++++ packages/apklis_http_web_api/pubspec.yaml | 19 ++ .../test/apklis_http_web_api_test.dart | 52 +++++ packages/apklis_http_web_api/test/utils.dart | 11 ++ 8 files changed, 373 insertions(+) create mode 100644 packages/apklis_http_web_api/.metadata create mode 100644 packages/apklis_http_web_api/LICENSE create mode 100644 packages/apklis_http_web_api/README.md create mode 100644 packages/apklis_http_web_api/assets/response_example.json create mode 100644 packages/apklis_http_web_api/lib/apklis_http_web_api.dart create mode 100644 packages/apklis_http_web_api/pubspec.yaml create mode 100644 packages/apklis_http_web_api/test/apklis_http_web_api_test.dart create mode 100644 packages/apklis_http_web_api/test/utils.dart diff --git a/packages/apklis_http_web_api/.metadata b/packages/apklis_http_web_api/.metadata new file mode 100644 index 0000000..cb69e6e --- /dev/null +++ b/packages/apklis_http_web_api/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: 02c026b03cd31dd3f867e5faeb7e104cce174c5f + channel: stable + +project_type: package diff --git a/packages/apklis_http_web_api/LICENSE b/packages/apklis_http_web_api/LICENSE new file mode 100644 index 0000000..7f1462f --- /dev/null +++ b/packages/apklis_http_web_api/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Flutter Cuba Contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/apklis_http_web_api/README.md b/packages/apklis_http_web_api/README.md new file mode 100644 index 0000000..2f78225 --- /dev/null +++ b/packages/apklis_http_web_api/README.md @@ -0,0 +1,44 @@ +

Apklis Http Web API

+ +

+ + License: MIT + + + Test + + + Version + + + Melos + + + Lint + +

+ +

+ + Last commit + + + GitHub commit activity + + + Github Stars + + + Github Forks + + + Github Watchers + + + GitHub contributors + +

+ +--- + +Implementación concreta del paquete [apklis_web_api](https://pub.dev/packages/apklis_web_api) para interactuar con la API de [Apklis](https://apklis.cu) utilizando el paquete [http](https://pub.dev/packages/http). diff --git a/packages/apklis_http_web_api/assets/response_example.json b/packages/apklis_http_web_api/assets/response_example.json new file mode 100644 index 0000000..d4dd5d4 --- /dev/null +++ b/packages/apklis_http_web_api/assets/response_example.json @@ -0,0 +1,180 @@ +{ + "count": 1, + "next": null, + "previous": null, + "facets": {}, + "results": [ + { + "icon": "https://archive.apklis.cu/application/icon/club.postdata.covid19cuba-v14.png", + "background": "", + "package_name": "club.postdata.covid19cuba", + "name": "Covid19 Cuba Data", + "video_url": "", + "video_img": "", + "description": "

Proyecto para conocer los reportes diarios, estadísticas, etc. respecto a la Covid-19 en Cuba.

Sitio Web: https://covid19cubadata.github.io

Autores:

- MatCom: http://www.matcom.uh.cu

- Postdata.club: https://www.postdata.club

- Juventud Técnica: https://medium.com/juventud-técnica

Los datos se actualizan a partir de la información oficial del MINSAP informados por las autoridades al día siguiente.

Canal de Telegram: https://t.me/covid19cubadata

Grupo de Telegram: https://t.me/covid19cubadatachat

", + "updated": "2020-07-20T14:03:06+00:00", + "sale_count": 0, + "download_count": 354838, + "price": 0.0, + "rating": 4.066666666666666, + "sponsored": 0, + "with_db": false, + "reviews_star_1": 24, + "reviews_star_2": 5, + "reviews_star_3": 6, + "reviews_star_4": 17, + "reviews_star_5": 98, + "releases_count": 14, + "reviews_count": 150, + "categories": [ + { + "id": 3, + "name": "Utilidades", + "icon": "utils", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/Utilidades_b48JMjB.png" + }, + { + "id": 50, + "name": "Internet", + "icon": "internet", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/Internet_feq0fhU.png" + }, + { + "id": 46, + "name": "Medicina", + "icon": "medicine", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/Medicina.png" + }, + { + "id": 44, + "name": "Mapas y navegación", + "icon": "maps", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/MapasNavegaci%C3%B3n.png" + }, + { + "id": 6, + "name": "Noticias y revistas", + "icon": "news_magazines", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/NoticiasRevistas.png" + }, + { + "id": 5, + "name": "Salud y bienestar", + "icon": "health", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/SaludBienestar.png" + }, + { + "id": 1, + "name": "Cuba", + "icon": "cubans", + "group": "Applications", + "icon_url": "https://archive.apklis.cu/category/Aplicaciones_y_juegos_cubanos.png" + } + ], + "size": 13842621, + "developer": { + "username": "leynier", + "first_name": "Leynier", + "last_name": "Gutiérrez González", + "fullname": "Leynier Gutiérrez González", + "avatar": "https://archive.apklis.cu/user/avatar/avatar_zF8cIFo.png", + "background": null, + "apps": 3, + "is_active": true, + "description": "leynier.github.io" + }, + "last_release": { + "abi": [ + { + "abi": "armeabi-v7a" + } + ], + "no_abi": false, + "version_name": "v1.0.0", + "package_name": "club.postdata.covid19cuba", + "app_name": "Covid19 Cuba Data", + "version_sdk_name": "Jelly Bean 4.1.x", + "version_target_sdk_name": "Pie 9.0", + "permissions": [ + { + "icon": "", + "description": "", + "name": "Get tasks" + }, + { + "icon": "", + "description": "", + "name": "Internet" + }, + { + "icon": "", + "description": "", + "name": "Receive boot completed" + }, + { + "icon": "", + "description": "", + "name": "Vibrate" + }, + { + "icon": "", + "description": "", + "name": "Wake lock" + } + ], + "screenshots": [ + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-37.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-42.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-47.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-50.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-52.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-55.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-39-58.jpg" + }, + { + "description": "", + "img": "https://archive.apklis.cu/application/screenshot/photo_2020-07-20_05-40-01.jpg" + } + ], + "changelog": "

1. Reducción del tamaño de la aplicación a la mitad del tamaño de la versión anterior.

2. Nuevas imágenes y descripciones al carrusel inicial de bienvenida.

3. Separación de hombres y mujeres en la gráfica de Distribución por rangos etarios en la sección de gráficas General de Cuba.

4. Gráficas de casos sintomáticos/asintomáticos en la sección de gráficas General de Cuba.

5. Gráficas de la evolución de las personas en vigilancia en la sección de gráficas General de Cuba.

6. Gráficas de la evolución de las personas hospitalizadas en la sección de gráficas General de Cuba.

7. Sección de gráficos de casos Fallecidos en Cuba.

8. Más fuentes de noticias como Juventud Técnica, la Agencia Cubana de Noticias, Cubadebate, entre otras.

9. Fráfica de radar de comparación de varios parámetros de Cuba con otros países en la sección de Mundo.

10. Sección de boletines del Centro de Estudios Demográficos (CEDEM) de la Universidad de La Habana enfocados en la temática de la COVID-19.

11. Sección con las medidas de cada fase de la etapa de recuperación post Covid-19, así como la fase en la que se encuentran las provincias

12. Sección de descargas con las bases de datos utilizadas en diferentes formatos.

13. Sección con los protocolos de actuación, con alcance nacional, que contribuyen a la prevención, control y manejo de los casos relacionados con la enfermedad de la Covid-19.

14. Registro manual de las personas con que tuvo contacto (además de la fecha y el lugar) para mejorar y ayudar la labor de las autoridades satinarías en la lucha contra la pandemia.

15. Sección de Atención a la Población con los números telefónicos de cada provincia para la Covid-19.

16. Configuración de proxy para los usuarios que utilizan una red corporativa, estudiantil, etc. en la que se necesite configurar un proxy.

17. Configuración de las notificaciones por tipo de notificación y por horario del día.

", + "version_code": 14, + "published": "2020-07-20T14:03:06.180774Z", + "sha256": "ffbca10a07c2e0f922dc31d1470ec823df0b395fc0c79f3d4e06efd669288cf1", + "size": "13.20 MB", + "icon": "https://archive.apklis.cu/application/icon/club.postdata.covid19cuba-v14.png", + "public": true, + "beta": false, + "version_sdk": "Jelly Bean 4.1.x", + "version_target_sdk": "Pie 9.0", + "deleted": false + }, + "announced": false + } + ] +} \ No newline at end of file diff --git a/packages/apklis_http_web_api/lib/apklis_http_web_api.dart b/packages/apklis_http_web_api/lib/apklis_http_web_api.dart new file mode 100644 index 0000000..5fcafec --- /dev/null +++ b/packages/apklis_http_web_api/lib/apklis_http_web_api.dart @@ -0,0 +1,36 @@ +library apklis_http_web_api; + +import 'dart:convert'; + +import 'package:apklis_web_api/apklis_api_result.dart'; +import 'package:apklis_web_api/apklis_web_api.dart'; +import 'package:apklis_web_api/models/models.dart'; +import 'package:http/http.dart'; + +class ApklisHttpWebApi extends ApklisWebApi { + final BaseClient httpClient; + + ApklisHttpWebApi(this.httpClient); + + @override + Future getByUri(Uri uri) async { + try { + final response = await httpClient.get(uri); + if (response.statusCode == 200) { + var text = ''; + try { + text = utf8.decode(response.bodyBytes); + } on FormatException { + text = response.body; + } + final json = jsonDecode(text) as Map; + final model = ApklisModel.fromJson(json); + return ApklisApiResult.success(model); + } else { + return ApklisApiResult.failure(response.reasonPhrase!); + } + } catch (e) { + return ApklisApiResult.failure(e.toString()); + } + } +} diff --git a/packages/apklis_http_web_api/pubspec.yaml b/packages/apklis_http_web_api/pubspec.yaml new file mode 100644 index 0000000..9c2bc1b --- /dev/null +++ b/packages/apklis_http_web_api/pubspec.yaml @@ -0,0 +1,19 @@ +name: apklis_http_web_api +description: Implementación concreta del paquete apklis_web_api para interactuar con la API de Apklis utilizando el paquete http. +version: 0.0.0 +homepage: https://github.com/fluttercuba/apklis-dart-flutter + +environment: + sdk: ">=2.12.0 <3.0.0" + +dependencies: + apklis_web_api: ^0.0.1+2 + http: ^0.13.3 + +dev_dependencies: + build_runner: ^2.0.4 + freezed: ^0.14.2 + json_serializable: ^4.1.3 + lint: ^1.5.3 + mocktail: ^0.1.4 + test: ^1.17.5 diff --git a/packages/apklis_http_web_api/test/apklis_http_web_api_test.dart b/packages/apklis_http_web_api/test/apklis_http_web_api_test.dart new file mode 100644 index 0000000..9e4b2a1 --- /dev/null +++ b/packages/apklis_http_web_api/test/apklis_http_web_api_test.dart @@ -0,0 +1,52 @@ +import 'package:apklis_http_web_api/apklis_http_web_api.dart'; +import 'package:apklis_web_api/apklis_web_api.dart'; +import 'package:http/http.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:test/test.dart'; + +import 'utils.dart'; + +class MockHttpClient extends Mock implements BaseClient {} + +void main() { + test('check request success', () async { + final apps = ['club.postdata.covid19cuba']; + final httpClient = MockHttpClient(); + final api = ApklisHttpWebApi(httpClient); + final uri = ApklisWebApi.buildUri(apps); + final file = await getProjectFile('assets/response_example.json'); + final text = await file.readAsString(); + expect(text, isNotEmpty); + when(() => httpClient.get(uri)).thenAnswer( + (_) => Future.value(Response(text, 200)), + ); + final model = await api.get(['club.postdata.covid19cuba']); + model.when( + success: (result) { + expect(result.results.isNotEmpty, true); + }, + failure: (error) { + throw Exception('Result should be success. Error: $error'); + }, + ); + }); + + test('check request failure', () async { + final apps = ['club.postdata.covid19cuba']; + final httpClient = MockHttpClient(); + final api = ApklisHttpWebApi(httpClient); + final uri = ApklisWebApi.buildUri(apps); + when(() => httpClient.get(uri)).thenAnswer( + (_) => Future.value(Response('', 400, reasonPhrase: 'Invalid request')), + ); + final model = await api.get(['club.postdata.covid19cuba']); + model.when( + success: (result) { + throw Exception('Result should be failure.'); + }, + failure: (error) { + expect(error.isNotEmpty, true); + }, + ); + }); +} diff --git a/packages/apklis_http_web_api/test/utils.dart b/packages/apklis_http_web_api/test/utils.dart new file mode 100644 index 0000000..6751344 --- /dev/null +++ b/packages/apklis_http_web_api/test/utils.dart @@ -0,0 +1,11 @@ +import 'dart:io'; + +/// Get a stable path to a test resource by scanning up to the project root. +Future getProjectFile(String path) async { + var dir = Directory.current; + while ( + !await dir.list().any((entity) => entity.path.endsWith('pubspec.yaml'))) { + dir = dir.parent; + } + return File('${dir.path}/$path'); +}