Last active
August 10, 2019 08:50
-
-
Save juliuscanute/46122ca173128fddc3a343e078ff788d to your computer and use it in GitHub Desktop.
[Accessing API Endpoint] #api #flutter #dart
This file contains hidden or 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
//Syntax | |
class Api { | |
final HttpClient _httpClient = HttpClient(); | |
final String _url = 'urltoaccess.com'; | |
//GET /$path/B | |
Future<TypeToConvert> callType1( | |
String path, String param1, String param2, String param3) async { | |
final uri = Uri.https(_url, '/$path/B', | |
{'param1': param1, 'param2': param2, 'param3': param3}); | |
final jsonResponse = await _getJson(uri); | |
if (jsonResponse == null || jsonResponse['status'] == null) { | |
print('Error retrieving conversion.'); | |
return null; | |
} else if (jsonResponse['status'] == 'error') { | |
print(jsonResponse['message']); | |
return null; | |
} | |
return jsonResponse['params']; | |
} | |
//Helper Method to make network request | |
Future<Map<String, dynamic>> _getJson(Uri uri) async { | |
try { | |
final httpRequest = await _httpClient.getUrl(uri); | |
final httpResponse = await httpRequest.close(); | |
if (httpResponse.statusCode != HttpStatus.OK) { | |
return null; | |
} | |
final responseBody = await httpResponse.transform(utf8.decoder).join(); | |
return json.decode(responseBody); | |
} on Exception catch (e) { | |
print('$e'); | |
return null; | |
} | |
} | |
} | |
//Example | |
class Api { | |
final HttpClient _httpClient = HttpClient(); | |
final String _url = 'flutter.udacity.com'; | |
Future<List> getUnits(String category) async { | |
final uri = Uri.https(_url, '/$category'); | |
final jsonResponse = await _getJson(uri); | |
if (jsonResponse == null || jsonResponse['units'] == null) { | |
print('Error retrieving units.'); | |
return null; | |
} | |
return jsonResponse['units']; | |
} | |
Future<double> convert( | |
String category, String amount, String fromUnit, String toUnit) async { | |
final uri = Uri.https(_url, '/$category/convert', | |
{'amount': amount, 'from': fromUnit, 'to': toUnit}); | |
final jsonResponse = await _getJson(uri); | |
if (jsonResponse == null || jsonResponse['status'] == null) { | |
print('Error retrieving conversion.'); | |
return null; | |
} else if (jsonResponse['status'] == 'error') { | |
print(jsonResponse['message']); | |
return null; | |
} | |
return jsonResponse['conversion'].toDouble(); | |
} | |
Future<Map<String, dynamic>> _getJson(Uri uri) async { | |
try { | |
final httpRequest = await _httpClient.getUrl(uri); | |
final httpResponse = await httpRequest.close(); | |
if (httpResponse.statusCode != HttpStatus.OK) { | |
return null; | |
} | |
final responseBody = await httpResponse.transform(utf8.decoder).join(); | |
return json.decode(responseBody); | |
} on Exception catch (e) { | |
print('$e'); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment