Advertisement
joaopaulofcc

[SC] [DBAPI] apiClient.dart

Nov 25th, 2020
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.77 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:path/path.dart';
  4. import '../models/dog.dart';
  5. import 'package:http/http.dart' as http;
  6.  
  7. class ApiClient {
  8.   String _url = "https://5fb5817d36e2fa00166a462d.mockapi.io";
  9.  
  10.   Future<Dog> insertDog(Dog dog) async {
  11.     final response = await http.post(
  12.       join(_url, 'dogs'),
  13.  
  14.       headers: <String, String>{
  15.         'Content-Type': 'application/json; charset=UTF-8',
  16.       },
  17.  
  18.       body: jsonEncode(<String, dynamic>{
  19.         'name': dog.name,
  20.         'age': dog.age,
  21.       }),
  22.     );
  23.  
  24.     if (response.statusCode == 201) {
  25.       return Dog.fromMap(jsonDecode(response.body));
  26.     }
  27.     else {
  28.       throw Exception('Falha ao salvar dado na API');
  29.     }
  30.   }
  31.  
  32.   Future<Dog> updateDog(Dog dog) async {
  33.     final response = await http.put(
  34.       join(_url, 'dogs/${dog.id}'),
  35.  
  36.       headers: <String, String>{
  37.         'Content-Type': 'application/json; charset=UTF-8',
  38.       },
  39.  
  40.       body: jsonEncode(<String, dynamic>{
  41.         'name': dog.name,
  42.         'age': dog.age,
  43.       }),
  44.     );
  45.  
  46.     if (response.statusCode == 200) {
  47.       return Dog.fromMap(jsonDecode(response.body));
  48.     }
  49.     else {
  50.       throw Exception('Falha ao atualizar um dado na API!');
  51.     }
  52.   }
  53.  
  54.   Future<Dog> getDog(String id) async {
  55.     final response = await http.get(
  56.         join(_url, 'dogs/$id'));
  57.  
  58.     if (response.statusCode == 200) {
  59.       return Dog.fromMap(jsonDecode(response.body));
  60.     }
  61.     else {
  62.       throw Exception('Falha ao carregar dados da API!');
  63.     }
  64.   }
  65.  
  66.   Future<List<Dog>> getDogs() async {
  67.     final response = await http.get(
  68.         join(_url, 'dogs'));
  69.  
  70.     List<Dog> listDogs;
  71.  
  72.     if (response.statusCode == 200) {
  73.       final List dogs = jsonDecode(response.body);
  74.       listDogs = List.generate(dogs.length, (i) {
  75.         return Dog.fromMap(dogs[i]);
  76.       });
  77.       return listDogs;
  78.     }
  79.     else {
  80.       throw Exception('Falha ao carregar dados da API!');
  81.     }
  82.   }
  83.  
  84.   Future deleteDog(String id) async {
  85.     final response = await http.delete(
  86.       join(_url, 'dogs/$id'),
  87.  
  88.       headers: <String, String>{
  89.         'Content-Type': 'application/json; charset=UTF-8',
  90.       },
  91.     );
  92.  
  93.     if (response.statusCode != 200) {
  94.       throw Exception('Falha ao deletar o registro Dog com id=$id!');
  95.     }
  96.   }
  97.  
  98.   Future deleteAllDogs() async {
  99.     final List<Dog> dogs = await getDogs();
  100.  
  101.     for (Dog dog in dogs) {
  102.       final response = await http.delete(
  103.         join(_url, 'dogs/${dog.id}'),
  104.  
  105.         headers: <String, String>{
  106.           'Content-Type': 'application/json; charset=UTF-8',
  107.         },
  108.       );
  109.  
  110.       if (response.statusCode != 200) {
  111.         throw Exception('Falha ao deletar o registro Dog com id=${dog.id}!');
  112.       }
  113.     }
  114.   }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement