Skip to content
Snippets Groups Projects
Commit ff8c011f authored by Yuki's avatar Yuki
Browse files

Task 2 part 2a: Get more data with `Uri.https`

parent 38003e32
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ import 'package:assignment_1_base_project/db/character.dart'; ...@@ -8,7 +8,7 @@ import 'package:assignment_1_base_project/db/character.dart';
class API { class API {
// Note: the documentation for the API can be found here: https://anapioficeandfire.com/Documentation // Note: the documentation for the API can be found here: https://anapioficeandfire.com/Documentation
final String _charactersRoute = "https://anapioficeandfire.com/api/characters"; // final String _charactersRoute = "https://anapioficeandfire.com/api/characters";
Map<String, dynamic> extractFields(Map<String, dynamic> json) { Map<String, dynamic> extractFields(Map<String, dynamic> json) {
return { return {
...@@ -26,15 +26,28 @@ class API { ...@@ -26,15 +26,28 @@ class API {
List<Character> _characters = []; List<Character> _characters = [];
try { try {
var uri = Uri.parse(_charactersRoute); // var uri = Uri.parse(_charactersRoute);
var uri = Uri.https(
"anapioficeandfire.com", // Authority domain
'/api/characters', // Unencode Path
{
// Query Parameters
'pageSize': '50', // pageSize (Default : 10, Max : 50)
'page': '1' // page (Default: 1)
},
);
///// DEBUG /////
// print("//// DEBUG --- API START --- // _charactersRoute : ${_charactersRoute}");
var response = await http.get(uri); var response = await http.get(uri);
// print("//// DEBUG // response.body.runtimeType:${response.body.runtimeType}"); // String ///// DEBUG /////
// print(//// DEBUG // response.body); // // print("//// DEBUG // response.body.runtimeType: ${response.body.runtimeType}"); // String
// // print("//// DEBUG // ${response.body}");
if (response.statusCode == 200) { if (response.statusCode == 200) {
final jsonList = jsonDecode(response.body); final jsonList = jsonDecode(response.body);
// print("jsonList.runtimeType:${jsonList.runtimeType}"); // List<dynamic> // print("//// DEBUG // jsonList.runtimeType:${jsonList.runtimeType}"); // List<dynamic>
// print("//// DEBUG // jsonList.length:${jsonList.length}"); // 10 print("//// DEBUG // jsonList.length: ${jsonList.length} record(s) got "); // Query records number
_characters = Character.listFromJson(jsonList); _characters = Character.listFromJson(jsonList);
} else { } else {
...@@ -44,8 +57,10 @@ class API { ...@@ -44,8 +57,10 @@ class API {
print("Exceptions error: ( ${e} )"); print("Exceptions error: ( ${e} )");
} }
// print("//// DEBUG // _characters.length: ${_characters.length} - number of valid data"); ///// DEBUG /////
// print("//// DEBUG // _characters:${_characters}"); // print("//// DEBUG --- API Result --- // _characters:${_characters}");
print(
"//// DEBUG --- API Result --- // _characters.length: ${_characters.length} - number of valid data saved"); // Query records number
return _characters; return _characters;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment