Created
January 21, 2019 20:12
-
-
Save felangel/675d156c0513cb57fdb584048a291dbf to your computer and use it in GitHub Desktop.
[github_search] Github Client
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
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
import 'package:common_github_search/common_github_search.dart'; | |
class GithubClient { | |
final String baseUrl; | |
final http.Client httpClient; | |
GithubClient({ | |
http.Client httpClient, | |
this.baseUrl = "https://api.github.com/search/repositories?q=", | |
}) : this.httpClient = httpClient ?? http.Client(); | |
Future<SearchResult> search(String term) async { | |
final response = await httpClient.get(Uri.parse("$baseUrl$term")); | |
final results = json.decode(response.body); | |
if (response.statusCode == 200) { | |
return SearchResult.fromJson(results); | |
} else { | |
throw SearchResultError.fromJson(results); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment