Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfianlosari/ecc62c497d7a240e64147008c0dd0c3f to your computer and use it in GitHub Desktop.
Save alfianlosari/ecc62c497d7a240e64147008c0dd0c3f to your computer and use it in GitHub Desktop.
API Class part 2
import 'package:github_search/repo.dart';
import 'package:date_format/date_format.dart';
import 'dart:convert' show json, utf8;
import 'dart:io';
import 'dart:async';
class Api {
static final HttpClient _httpClient = HttpClient();
static final String _url = "api.github.com";
static Future<List<Repo>> getRepositoriesWithSearchQuery(String query) async {
final uri = Uri.https(_url, '/search/repositories', {
'q': query,
'sort': 'stars',
'order': 'desc',
'page': '0',
'per_page': '25'
});
final jsonResponse = await _getJson(uri);
if (jsonResponse == null) {
return null;
}
if (jsonResponse['errors'] != null) {
return null;
}
if (jsonResponse['items'] == null) {
return List();
}
return Repo.mapJSONStringToList(jsonResponse['items']);
}
static 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