Created
August 8, 2021 21:29
-
-
Save guilhermecarvalhocarneiro/38301f061473d95093987796de1b138a to your computer and use it in GitHub Desktop.
Classe de acesso à API com nullsafety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Service do model Paciente | |
/// | |
/// Os Métodos padrões gerados são: | |
/// fecthAll() -> Recupera a lista de Paciente da API. | |
/// detail() -> Recupera os detalhes de Paciente da API. | |
/// post() -> Salva os dados de uma instância do Paciente na API. | |
/// put() -> Atualiza os dados de uma instância do Paciente na API. | |
/// delete() -> Deleta os dados de uma instância do Paciente na API. | |
/// | |
/// Os métodos de acesso à API devem ser implementados no nessa classe. | |
/// [Travar o arquivo] | |
/// Caso deseje "travar" o arquivo para não ser parseado novamente | |
/// pelo manage do Django adicione um # antes da palavra abaixo | |
/// FileLocked | |
import 'package:dio/dio.dart'; | |
import 'package:flutter/material.dart'; | |
import 'dart:convert'; | |
import 'model.dart'; | |
import '../../../utils/config.dart'; | |
import '../../../utils/custom_dio.dart'; | |
import '../../../utils/util.dart'; | |
class PacienteService { | |
String _uri = "usuario/api/v1/paciente/"; | |
PacienteModel _paciente = PacienteModel(nome: ""); | |
List<PacienteModel> _pacienteList = List<PacienteModel>.empty(); | |
PacienteService({PacienteModel? paciente, List<PacienteModel>? pacienteList}) { | |
if (paciente != null) _paciente = paciente; | |
} | |
Future<List<PacienteModel>> fetch({String? url}) async { | |
try { | |
_uri = "${Config.uri}usuario/api/v1/paciente/"; | |
_pacienteList.clear(); | |
final CustomDio _dio = CustomDio(_uri); | |
final dataResponse = await _dio.getHttp(uri: _uri); | |
if (dataResponse != null) { | |
final String _next = dataResponse["next"]; | |
final String _previous = dataResponse["previous"]; | |
final _dataItems = dataResponse["results"]; | |
for (var data in _dataItems) { | |
PacienteModel _paciente = PacienteModel.fromMap(data); | |
_paciente.nextUrl = _next; | |
_paciente.previousUrl = _previous; | |
_pacienteList.add(_paciente); | |
} | |
_success("ok"); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _pacienteList; | |
} | |
Future<List<PacienteModel>> getMore(String uri) async { | |
try { | |
final CustomDio _dio = CustomDio(uri); | |
final dataResponse = await _dio.getHttp(uri: uri); | |
if (dataResponse != null) { | |
final String _next = dataResponse["next"]; | |
final String _previous = dataResponse["previous"]; | |
final _dataItems = dataResponse["results"]; | |
for (var data in _dataItems) { | |
PacienteModel _paciente = PacienteModel.fromMap(data); | |
_paciente.nextUrl = _next; | |
_paciente.previousUrl = _previous; | |
_pacienteList.add(_paciente); | |
} | |
_success("ok"); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _pacienteList; | |
} | |
Future<PacienteModel> detail(PacienteModel paciente) async { | |
_uri = "${Config.uri}usuario/api/v1/paciente/${paciente.id}/"; | |
try { | |
final CustomDio _dio = CustomDio(_uri); | |
final data = await _dio.getHttp(uri: _uri); | |
if (data != null) { | |
_paciente = PacienteModel.fromMap(data); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _paciente; | |
} | |
Future<PacienteModel> post(PacienteModel paciente) async { | |
try { | |
final CustomDio _dio = CustomDio(_uri); | |
final _data = json.encode(paciente.toMap()); | |
final data = await _dio.postHttp(_data); | |
if (data != null) { | |
_paciente = PacienteModel.fromMap(data); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _paciente; | |
} | |
Future<PacienteModel> put(PacienteModel paciente) async { | |
_uri = "${Config.uri}usuario/api/v1/paciente/${paciente.id}/"; | |
try { | |
final CustomDio _dio = CustomDio(_uri); | |
final _data = json.encode(paciente.toMap()); | |
final data = await _dio.putHttp(_data, paciente.id.toString(), uri: _uri); | |
if (data != null) { | |
_paciente = PacienteModel.fromMap(data); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _paciente; | |
} | |
Future<PacienteModel> patch(PacienteModel paciente) async { | |
_uri = "${Config.uri}usuario/api/v1/paciente/${paciente.id}/"; | |
try { | |
final CustomDio _dio = CustomDio(_uri); | |
final _data = json.encode(paciente.toMap()); | |
final data = await _dio.patchHttp(_data, uri: _uri); | |
if (data != null) { | |
_paciente = PacienteModel.fromMap(data); | |
} | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
} | |
return _paciente; | |
} | |
Future<bool> delete(PacienteModel paciente) async { | |
_uri = "${Config.uri}usuario/api/v1/paciente/${paciente.id}/"; | |
try { | |
final CustomDio _dio = CustomDio(_uri); | |
final _data = json.encode(paciente.toMap()); | |
final data = await _dio.deleteHttp(_data, paciente.id.toString(), uri: _uri); | |
if (data != null) { | |
return true; | |
} | |
return false; | |
} catch (error, exception) { | |
_error(error.toString(), exception.toString()); | |
return false; | |
} | |
} | |
void _success(String message, {int? statusCode}) async { | |
try { | |
DebugPrint.print("DebugSuccess: $message, Status: $statusCode"); | |
} catch (e) { | |
_error("$e", ""); | |
} | |
} | |
void _error(String error, String exception) async { | |
try { | |
DebugPrint.error("DebugErro: PacienteService -> $error | $exception"); | |
} catch (error) { | |
DebugPrint.error("DebugErro: PacienteService ->$error"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment